

// Definition de la classe 'CheckBox'
// NB: L'ordre des methodes est important
function CheckBox(id)
{
	// Proprietes publiques
	this.Id = id;
	this.HtmlObject = $("#" + id);
	this.IsLocked = false;
		
	// Evenements publics
	this.Click = new Array();	// Arguments: string Id, string Value, bool IsChecked
	
	// Champs prives
	var _this = this;
	var _content = _this.HtmlObject.find("Content");
	var _unlockState = null;
	
	var OnClick = function()
	{
		if (_this.IsLocked)
			return ;
	
		var enabledChild = $(_this.HtmlObject.children()[0]);
		var isEnabled = (enabledChild.attr("class") == "Enabled");
		if (isEnabled)
		{
			var checkedChild = $(enabledChild.children()[0]);
			var isChecked = (checkedChild.attr("class") == "Checked");
		
			isChecked = !isChecked;
			checkedChild.attr("class", isChecked ? "Checked" : "Unchecked");
		}

		Tools_DispatchEvent(_this.Click, _this, { Id: id, IsChecked: isChecked, IsEnabled: isEnabled });
	}
	
	// Renvoit un objet avec les propriétés "IsChecked" et "IsEnabled"
	this.GetState = function()
	{
		if (_this.IsLocked)
			return _unlockState;

		var enabledChild = $(_this.HtmlObject.children()[0]);
		var isEnabled = (enabledChild.attr("class") == "Enabled");
		var checkedChild = $(enabledChild.children()[0]);
		var isChecked = (checkedChild.attr("class") == "Checked");
		return { IsEnabled: isEnabled, IsChecked: isChecked, IsLocked: false };
	}
	
	this.SetState = function(enabled, checked)
	{
		if (_this.IsLocked)
		{
			_unlockState.IsEnabled = enabled;
			_unlockState.IsChecked = checked;
			return ;
		}
	
		var enabledChild = $(_this.HtmlObject.children()[0]);
	
		if (typeof (enabled) != "undefined" && enabled != null)
			enabledChild.attr("class", enabled ? "Enabled" : "Disabled");
		
		if (typeof (checked) != "undefined" && checked != null)
		{
			var checkedChild = $(enabledChild.children()[0]);
			checkedChild.attr("class", checked ? "Checked" : "Unchecked");
		}
	}
	
	this.Lock = function()
	{
		if (_this.IsLocked)
			return ;
			
		_unlockState = _this.GetState();
		_unlockState.IsLocked = true;
		$(_this.HtmlObject.children()[0]).attr("class", "Disabled");
	
		_this.IsLocked = true;
	}
	
	this.Unlock = function()
	{
		if (_this.IsLocked == false)
			return ;
		
		_this.IsLocked = false;
		_this.SetState(_unlockState.IsEnabled, _unlockState.IsChecked);
		_unlockState = null;
	}

	this.SetContent = function(html)
	{
		_content.html(html);
	}
	
	this.OnLoad = function()
	{
		_this.HtmlObject.click(function()
		{
			if ($(this).attr("class").indexOf("OverBudget") < 0)
				OnClick();
		});
	}
}

