﻿function SelectDropDown(holderId, selectedItm, selectedIndexHiddenFieldId, selectedIndx, autoPostBackScriptFunction) {
	this.holder = $(holderId);
	this.current = $(selectedItm);
	this.options = [];
	this.selectedIndexHiddenField = $(selectedIndexHiddenFieldId);
	this.selectedIndexHiddenField.value = selectedIndx;
	this.selectedIndex = selectedIndx;
	this.autoPostBackScriptFunction = autoPostBackScriptFunction;
	
	this.keepOpen = false;
	this.onchange = null;
	this.selectedValue = 0;

	this.toggle = function() {
		this.keepOpen = false;
		this.holder.toggleClassName('active');
	}

	this.prehide = function() {
		this.keepOpen = false;
		window.setTimeout(this.hide.bind(this), 300);
	}

	this.cancelhide = function() {
		this.keepOpen = true;
	}

	this.hide = function() {
		if (!this.keepOpen)
			this.holder.removeClassName('active');
	}

	var ddWidth = 0;

	this.init = function() {
		Event.observe(this.holder, "click", this.toggle.bind(this));
		Event.observe(this.holder, "mouseout", this.prehide.bind(this));
		Event.observe(this.holder, "mouseover", this.cancelhide.bind(this));
		$(this.holder.select(".option")).each(this.initOptions.bind(this));
		this.selectItem(this.selectedIndex, true);
		this.holder.style.width = ddWidth + "px";
	}

	this.initOptions = function(item, indx) {
		if (item.id == selectedItm)
			return;
		var indx = this.options.length;
		Event.observe(item, "click", this.selectItem.bind(this, indx, false));
		this.options.push({ "elt": item, "indx": indx, value: 0 });
		this.current.innerHTML = item.innerHTML;
		ddWidth = ddWidth > this.holder.offsetWidth ? ddWidth : this.holder.offsetWidth;
	}

	this.selectItem = function(indx, isInitialization) {
		if (!this.options[indx])
			return;
		if (this.options[indx].elt.hasClassName("disabled"))
			return;

		for (var i = 0, l = this.options.length; i < l; i++)
			this.options[i].elt.removeClassName("selected");
		this.options[indx].elt.addClassName("selected");
		this.current.innerHTML = this.options[indx].elt.innerHTML;
		this.selectedIndex = indx;
		this.options[indx].value = this.selectedValue;
		this.selectedIndexHiddenField.value = indx;
		if (this.onchange && typeof (this.onchange).toLowerCase() == "function")
			this.onchange(this.options[indx]);
		if (!isInitialization && this.autoPostBackScriptFunction != null)
			this.autoPostBackScriptFunction(arg = this.options[indx].elt.href);

	}

}
SelectDropDown.init = function(holderId, selectedItm, selectedIndexHiddenFieldId, selectedItemIndex, autoPostBackScriptFunction) {
	SelectDropDown[holderId] = new SelectDropDown(holderId, selectedItm, selectedIndexHiddenFieldId, selectedItemIndex, autoPostBackScriptFunction);
	SelectDropDown[holderId].init();
};

// [AtlasScript]

if(typeof(Sys)!="undefined")
	Sys.Application.notifyScriptLoaded();

// [/AtlasScript]