
// Definition de la classe 'ComboBox'
// NB: L'ordre des methodes est important
function ComboBox(id)
{
	// Proprietes publiques
	this.Id = id;
	this.HtmlObject = $("#" + id);
	this.SelectedValue = eval("comboBox_" + id + "_SelectedValue");
	
	// Evenements publics
	this.SelectedValueChanged = new Array();	// Arguments: string Value
	this.Opened = new Array();					// Arguments: none
	this.Closed = new Array();					// Arguments: bool ValueChanged
	
	// Champs prives
	var _this = this;
	var _content = $("#" + id + "Content");
	var _items = $("#" + id + "Items");
	var _isOpened = false;
	var _metadatas = eval(id + '_datas');
	
	this.GetMetadatas = function(itemId)
	{
		if (_metadatas && itemId)
			for (var i = 0; i < _metadatas.length; i++)
			{
				var metas = _metadatas[i];
				if (metas.Id == itemId)
					return metas;
			}
		return null;
	}
	
	// Methodes publiques
	this.Open = function()
	{
		if (_isOpened)
			return ;
		_isOpened = true;
		
		_this.HtmlObject.css("z-index", "999");
		_items.css("display", "block");
		Tools_DispatchEvent(_this.Opened, _this, null);
	}

	this.Close = function(valueChanged)
	{
		if (_isOpened == false)
			return ;
		_isOpened = false;

		_this.HtmlObject.css("z-index", "0");
		_items.css("display", "none");
		Tools_DispatchEvent(_this.Closed, _this, { ValueChanged: valueChanged ? true : false });
	}

	var OnClick = function()
	{
		_this.Open();
	}

	var OnSelectedValueChanged = function(item, value)
	{
		var valueChanged = (_this.SelectedValue != value);

		_this.Close(valueChanged);

		// On verifie bien que la valeur a change
		// avant de lancer la machine a shadocks
		if (valueChanged == false)
			return ;
		_this.SelectedValue = value;
		
		// Supprime l'ancienne image et recree la nouvelle
		_content.empty();
		var currentImage = $(item).find("img:first");
		var currentSource = currentImage.attr("src");
		var currentAlt = currentImage.attr("alt");
		
		if ($(document.body).attr("class") == "IE6")
		{
			var html = $(item).html();
			var start = html.indexOf("src='") + 5;
			var end = html.substring(start).indexOf("'");
			currentSource = html.substring(start, start + end);
		}
				
		_content.html('<img src="' + currentSource + '" alt="' + currentAlt + '"'
						+ ($(document.body).attr("class") == "IE6" ? ' style="visibility:hidden"' : '') + '/>');
		if ($(document.body).attr("class") == "IE6")
			_content.find("img").load(function()
			{
				doFix(this, true);
				$(this).css("visibility", "visible");
			});
 		
 		// Signale aux autres que la valeur selectionnee a change
 		var args = { ItemId: $(item).attr("id"), Value: value };
		Tools_DispatchEvent(_this.SelectedValueChanged, _this, args);
	}
	
	var SetWidth = function()
	{
		_items.css("position", "relative");
		var width = _items.attr("offsetWidth")
		_items.css("position", "absolute");
			
		_this.HtmlObject.css("width", width + "px");
		_items.css("width", width + "px");
		_items.css("display", "none");
		_items.css("visibility", "visible");
	}
	
	this.OnLoad = function()
	{
		$(window).load(SetWidth);
		
		_this.HtmlObject.find("tr:first").click(OnClick);
		_this.HtmlObject.find(".ComboBoxItem").click(function(event)
		{
			if ($(this).attr("class").indexOf("OverBudget") < 0)
			{
				var value = _this.GetMetadatas($(this).attr("id")).Value;
				OnSelectedValueChanged(this, value);	
			}
			else _this.Close(true);
		});
	}
}

