﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("Renins");

Renins.AdvancedCheckBox = function(element) {
	Renins.AdvancedCheckBox.initializeBase(this, [element]);

	this._id = '';
	this._clientId = '';
	this._name = '';
	this._imgOnSrc = '';
	this._imgOffSrc = '';
	this._defaultInit = false;
	this._containerId = null;
	this._container = null;
	this._paintContainerId = '';
	this._paintContainer = null;
	this._imgAlt = '';
	this._paragraphInnerHtml = '';
	this._paragraphControl = null;
	this._img = null;
	this._input = null;
	//this.check(checked, true);
	this._checked = false;
	this._disabled = false;
	this._visible = true;
	this._isInitialized = false;
	this._clickDelegate = null;
	this._needRender = false;
	this._checkedCssClassForPaintContainer = null;
	this._disabledCssClassForPaintContainer = null;
	//this._disableDelegate = null;
}

Renins.AdvancedCheckBox.prototype = {
	get_id: function() { return this._id; },
	set_id: function(value) { this._id = value; },

	get_clientId: function() { return this._clientId; },
	set_clientId: function(value) { this._clientId = value; },

	get_name: function() { return this._name; },
	set_name: function(value) { this._name = value; },

	get_imgOnSrc: function() { return this._imgOnSrc; },
	set_imgOnSrc: function(value) { this._imgOnSrc = value; },

	get_imgOffSrc: function() { return this._imgOffSrc; },
	set_imgOffSrc: function(value) { this._imgOffSrc = value; },

	get_imgAlt: function() { return this._imgAlt; },
	set_imgAlt: function(value) { this._imgAlt = value; },

	get_defaultInit: function() { return this._defaultInit; },
	set_defaultInit: function(value) { this._defaultInit = value; },

	get_needRender: function() { return this._needRender; },
	set_needRender: function(value) { this._needRender = value; },

	get_containerId: function() { return this._containerId; },
	set_containerId: function(value) { this._containerId = value; },

	get_paintContainerId: function() { return this._paintContainerId; },
	set_paintContainerId: function(value) {
		this._paintContainerId = value;
		if (value != '') {
			this._paintContainer = $get(value);
		}
	},

	get_checkedCssClassForPaintContainer: function() { return this._checkedCssClassForPaintContainer; },
	set_checkedCssClassForPaintContainer: function(value) { this._checkedCssClassForPaintContainer = value; },

	get_disabledCssClassForPaintContainer: function() { return this._disabledCssClassForPaintContainer; },
	set_disabledCssClassForPaintContainer: function(value) { this._disabledCssClassForPaintContainer = value; },

	get_paragraphInnerHtml: function() { return this._paragraphInnerHtml; },
	set_paragraphInnerHtml: function(value) { this._paragraphInnerHtml = value; },

	get_container: function() {
		if (this.get_containerId() && !this._container) {
			this._container = $get(this.get_containerId());
		}
		return this._container;
	},

	set_container: function(value) { this._container = value; },

	render: function() {
		if (this.get_container()) {
			var div = document.createElement('div');
			div.style.display = 'none';
			var input = document.createElement('input');
			input.type = 'checkbox';
			//input.id = input.name = this._clientId + '_checkbox_input';
			this.set_input(input);
			div.appendChild(input);
			this.get_container().appendChild(div);
			var img = document.createElement('img');
			//img.id = img.name = this._clientId + '_checkbox_img';
			img.alt = this.get_imgAlt();
			this.set_img(img);
			this.get_container().appendChild(img);
			var p = document.createElement('p');
			//p.id = this._clientId + '_checkbox_p';
			p.innerHTML = this.get_paragraphInnerHtml();
			this.set_paragraphControl(p);
			this.get_container().appendChild(p);
			p = null;
			div = null;
			input = null;
			img = null;
		}
	},

	get_img: function() {
		if (!this._img) {
			this._img = $get(this._clientId + '_checkbox_img');
		}
		return this._img;
	},

	set_img: function(value) { this._img = value; },

	get_input: function() {
		if (!this._input) {
			this._input = $get(this._clientId + '_checkbox_input');
		}
		return this._input;
	},

	set_input: function(value) { this._input = value; },

	get_paragraphControl: function() {
		if (!this._paragraphControl) {
			this._paragraphControl = $get(this._clientId + '_checkbox_p');
		}
		return this._paragraphControl;
	},

	set_paragraphControl: function(value) { this._paragraphControl = value; },

	get_checked: function() { return this._checked; },
	set_checked: function(value) { this._checked = value; },

	check: function(value, softly) {
		this._checked = value;
		this.get_img().src = value ? this._imgOnSrc : this._imgOffSrc;
		this.get_input().checked = value;
		if (this._paintContainer && !this.get_disabled()) {
			this._paintContainer.className = value ? this._checkedCssClassForPaintContainer : '';
		}
		if (!softly) {
			this._onClick(Sys.EventArgs.Empty);
		}
	},

	get_disabled: function() { return this._disabled; },
	set_disabled: function(value) { this._disabled = value; },

	disable: function(value, softly) {
		this.get_img().disabled = value;
		this._disabled = value;
		this.check(false, softly);
		if (this._paintContainer) {
			this._paintContainer.className = value ? this._disabledCssClassForPaintContainer : '';
		}		
		this._onDisable(Sys.EventArgs.Empty);
		/*if (typeof (this._disableDelegate) == 'function') {
		this._disableDelegate(value);
		}*/
	},

	disableWithoutUncheck: function(value) {
		this.get_img().disabled = value;
		this._disabled = value;
		//this.check(false, softly);
		if (this._paintContainer) {
			this._paintContainer.className = value ? this._disabledCssClassForPaintContainer : '';
		}
		this._onDisable(Sys.EventArgs.Empty);
	},

	get_visible: function() { return this._visible; },
	set_visible: function(value) { this._visible = value; },

	visible: function(visible) {
		var display = visible ? "block" : "none";
		if (!this.get_container()) {
			this.get_img().style.display = this.get_paragraphControl().style.display = display;
		}
		else {
			this.get_container().style.display = display;
		}
	},

	add_click: function(handler) { this.get_events().addHandler("click", handler); },
	remove_click: function(handler) { this.get_events().removeHandler("click", handler); },

	add_disable: function(handler) { this.get_events().addHandler("disable", handler); },
	remove_disable: function(handler) { this.get_events().removeHandler("disable", handler); },

	_onClick: function(e) {
		var h = this.get_events().getHandler("click");
		if (h) h(this, e);
	},

	_onDisable: function(e) {
		var h = this.get_events().getHandler("disable");
		if (h) h(this, e);
	},

	_clickHandler: function() {
		if (!this.get_disabled()) {
			this.check(!this.get_checked(), false);
			/*this._checked = !this._checked;
			this._img.src = this._checked ? this._imgOnSrc : this._imgOffSrc;
			this._input.checked = this._checked;*/
		}
	},

	initialize: function() {
		Renins.AdvancedCheckBox.callBaseMethod(this, 'initialize');
		if (!this._clickDelegate) {
			this._clickDelegate = Function.createDelegate(this, this._clickHandler);
		}

		//Рендерим контрол, если он создается из javascript
		if (this.get_needRender()) {
			this.render();
		}

		// Add custom initialization here
		$addHandler(this.get_img(), "click", this._clickDelegate);
		this.visible(this.get_visible());
		if (this.get_defaultInit()) {
			this.init();
		}
	},

	init: function(softly) {
		softly = softly ? softly : false;
		if (!this._isInitialized) {
			if (this.get_disabled()) {
				this.disable(this.get_disabled(), softly);
			}
			else {
				this.check(this.get_checked(), softly);
			}
			this._isInitialized = true;
		}
	},

	dispose: function() {
		//Add custom dispose actions here
		$removeHandler(this.get_img(), "click", this._clickDelegate);
		Renins.AdvancedCheckBox.callBaseMethod(this, 'dispose');
	},

	getNameValue: function() {
		return this.get_name() + '=' + this.get_checked() + paramsSeparator;
	},

	click: function() {
		this._clickHandler();
	}
}
Renins.AdvancedCheckBox.registerClass('Renins.AdvancedCheckBox', Sys.UI.Control);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
