﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("DWR");

DWR.DropDownControl = function(element)
{
	//call base method
	DWR.DropDownControl.initializeBase(this, [element]);

	//properties
	this._Root = "";
	this._SkinID = "SnowSkin";

	this._SelectedIndex = -1;
	this._Options = new Object();
	this._Width = "100px";
	this._Disabled = false;
	this._PreviousValue = "";

	//members
	this._oDivMain = null;
	this._onClickDelegate = null;
	this._isPopup = false;
	this._isOverStyleApplied = false;
	this._onTdMouseOverDelegate = null;
	this._onTdMouseOutDelegate = null;
	this._onTdClickDelegate = null;
	this._onBlurDelegate = null;
	this._onChangeDelegate = null;
	this._needUpdate = true;
}

DWR.DropDownControl.prototype = {
	initialize: function()
	{
		DWR.DropDownControl.callBaseMethod(this, 'initialize');

		var oEl = this.GetTopmostLayoutEl();
		this._onBlurDelegate = Function.createDelegate(this, this.OnBlur);
		$addHandler(oEl, "mousedown", this._onBlurDelegate);
		this.BuildControlTree();

		var oValue = this._Options;
		this._Options = new Array();

		for (var oElement in oValue)
		{
			this._Options.push(oValue[oElement]);
		}

		if (this._Options.length > 0)
		{
			this.set_SelectedIndex(0);
			this._PreviousValue = this.get_SelectedValue();
		}

		if (this._onChangeDelegate == null)
		{
			this._onChangeDelegate = Function.createDelegate(this, this.changeHandler);
		}


		var path = this.getRoot() + "/css/DropDownControl.css";
		

		this.LoadScriptsAndStyles(path, "css");

	},

	// ------------------------ properties ----------------------------------------  
	get_Disabled: function()
	{
		return this._Disabled;
	},

	set_Disabled: function(value)
	{
		if (value)
		{
			//$get(this.get_ComboId()).className = "button_disabled";
			$get(this.get_ComboId()).style.display = "none";
			$get(this.get_MainTdId()).style.width = parseInt(this._Width);
			this.set_BackgroundColor("#ece9d8","#aca899");
			if ($get(this.get_MainDivId()).onclick)
			{
				//$removeHandler(this._oDivMain, "click", this._onClickDelegate);
				this._oDivMain.onclick = null;
			}
			this._Disabled = true;
		}
		else
		{
			//$get(this.get_ComboId()).className = "button"
			$get(this.get_ComboId()).style.display = "block";
			$get(this.get_MainTdId()).style.width = parseInt(this._Width) - 18;
			this.set_BackgroundColor("#ffffff","#000000");
			if (!$get(this.get_MainDivId()).onclick)
			{
				this._onClickDelegate = Function.createDelegate(this, this.OnTableClick);
				this._oDivMain.onclick = this._onClickDelegate;
				//$addHandler(this._oDivMain, "click", this._onClickDelegate);
			}
			
			this._Disabled = false;
		}
	},

	set_SelectedIndex: function(value)
	{
		if (value >= 0 && value < this._Options.length)
		{
			this._PreviousValue = this.get_SelectedValue();
			this._SelectedIndex = value;
			var cellMain = $get(this.get_MainTdId());
			
			if(this.get_SelectedValue().indexOf('notSelect') == -1)
				cellMain.innerHTML = remove_nbsps(this.get_SelectedValue());

			if (this._isPopup && this._needUpdate)
			{
				this.HidePopup(this.get_element());
				this.ShowPopup(this.get_element());
			}


			var oDivRowTmp = document.createElement("div");
			oDivRowTmp.innerHTML = this.get_SelectedValue();
			
			if(oDivRowTmp.childNodes[0].nodeValue != undefined)
				this.get_element().title = remove_nbsps(oDivRowTmp.childNodes[0].nodeValue);
			else
				this.get_element().title = remove_nbsps(oDivRowTmp.childNodes[0].innerHTML);
		}
	},

	set_Options: function(oValue)
	{
		if (null != oValue)
		{
			this._Options = new Array();

			for (var oElement in oValue)
			{
				this._Options.push(oValue[oElement]);
			}

			if (this._isPopup)
			{
				this.HidePopup(this.get_element());
				this.ShowPopup(this.get_element());
			}
		}
	},

	add_change: function(handler)
	{
		this.get_events().addHandler('change', handler);
	},
	remove_change: function(handler)
	{
		this.get_events().removeHandler('change', handler);
	},

	get_MainDivId: function()
	{
		return this.get_id() + "_mainDivId";
	},

	get_MainTdId: function()
	{
		return this.get_id() + "_mainTextTD";
	},

	get_ComboId: function()
	{
		return this.get_id() + "_comboButton";
	},

	get_PopupId: function()
	{
		return this.get_id() + "_popup";
	},

	get_Options: function()
	{
		return this._Options;
	},

	get_SelectedValue: function()
	{
		if (null != this._Options && this._SelectedIndex < this._Options.length)
		{
			return this._Options[this._SelectedIndex];
		}
		else
		{
			return null;
		}
	},
	
	get_SelectedValueText: function()
	{
		var result = null;
		try
		{
			var tmpDiv = document.createElement('div');
			tmpDiv.innerHTML = this.get_SelectedValue();
			if(tmpDiv.childNodes[0].nodeValue != undefined)
				result = remove_nbsps(tmpDiv.childNodes[0].nodeValue);
			else
				result = remove_nbsps(tmpDiv.childNodes[0].innerHTML);
		}
		catch(err)
		{
			result = "";
		}
		
		return result;
	},
	
	get_PreviousValue: function()
	{
		return this._PreviousValue;
	},

	get_SelectedIndex: function()
	{
		return this._SelectedIndex;
	},

	changeHandler: function(event)
	{
		var h = this.get_events().getHandler('change');
		if (h) h(this, Sys.EventArgs.Empty);
	},

	GetTopmostLayoutEl: function()
	{
		var oEl;
		if (document.compatMode == "CSS1Compat")
		{
			oEl = document.documentElement;
		}
		else
		{
			oEl = document.body;
		}
		return oEl;
	},

	get_Bounds: function(element)
	{
		var left = element.offsetLeft;
		var top = element.offsetTop;

		for (var parent = element.offsetParent; parent; parent = parent.offsetParent)
		{
			left += parent.offsetLeft - parent.scrollLeft;
			top += parent.offsetTop - parent.scrollTop;
		}

		return { left: left, top: top, width: element.offsetWidth, height: element.offsetHeight };
	},

	ShowPopup: function(parent)
	{
		var oMainDiv = this.get_element();
		var oDiv = this.GenerateTable();
		parent.appendChild(oDiv);

		this._IsPopup = true;
	},

	HidePopup: function(oDivParent)
	{
		var oDiv = $get(this.get_PopupId(), oDivParent);

		if (oDiv != null)
		{
			oDivParent.removeChild(oDiv);
		}

		this._IsPopup = false;
	},

	BuildControlTree: function()
	{
		this._oDivMain = document.createElement("div");
		this.get_element().appendChild(this._oDivMain);
		this._oDivMain.onselectstart = function() { return false; };

		this.get_element().style.width = parseInt(this._Width);// +3 + "px";
		this._oDivMain.style.width = parseInt(this._Width); // +3 + "px";
		this._oDivMain.className = "divMainDDC";
		this._oDivMain.id = this.get_MainDivId();

		var table = document.createElement("table");
		table.cellPadding = 0;
		table.cellSpacing = 0;
		table.style.width = "100%";
		table.style.height = "18px";

		this._onClickDelegate = Function.createDelegate(this, this.OnTableClick);
		this._oDivMain.onclick = this._onClickDelegate;
		//$addHandler(this._oDivMain, "click", this._onClickDelegate);

		var row = table.insertRow(0);
		var cell = row.insertCell(0);
		cell.style.width = "100%";

		var divText = document.createElement("div");
		divText.className = "divText";

		divText.innerHTML = "";
		divText.id = this.get_MainTdId();

		if (parseInt(this._Width))
		{
			divText.style.width = parseInt(this._Width) - 18 + "px";	
		}

		cell.appendChild(divText);

		cell = row.insertCell(1);

		var divButton = document.createElement("div");
		divButton.id = this.get_ComboId();
		divButton.className = "button";

		var delegate = Function.createDelegate(this, this.OnImageMouseOver);
		var callback = Function.createCallback(delegate, divButton);
		$addHandler(divButton, "mouseover", callback);

		delegate = Function.createDelegate(this, this.OnImageMouseOut);
		callback = Function.createCallback(delegate, divButton);
		$addHandler(divButton, "mouseout", callback);

		cell.appendChild(divButton);

		this._oDivMain.appendChild(table);
	},

	GenerateTable: function()
	{
		var oDiv = document.createElement("div");
		this.get_element().appendChild(oDiv);

		oDiv.className = "PopupMenu";
		oDiv.style.minWidth = parseInt(this._Width) + 18 + "px";
		oDiv.style.width = "auto";
		
		oDiv.id = this.get_PopupId();
		var oTable = document.createElement("table");

		oTable.cellPadding = 0;
		oTable.cellSpacing = 0;
		oTable.style.width = "100%";

		if (null != oTable)
		{
			for (i in this._Options)
			{
				var row = oTable.insertRow(i);
				row.style.border = "solid 0px";
				var cell = row.insertCell(0);

				var oDivRow = document.createElement("div");
				oDivRow.className = "divText";

				oDivRow.innerHTML = this._Options[i];
				
				if(oDivRow.childNodes[0].nodeValue != undefined)
					oDivRow.title = remove_nbsps(oDivRow.childNodes[0].nodeValue);
				else
					oDivRow.title = remove_nbsps(oDivRow.childNodes[0].innerHTML);
				
				cell.appendChild(oDivRow);
				cell.style.height = "18px";
				cell.style.borderWidth = "0px";

				var delegate = Function.createDelegate(this, this.OnTdMouseOver);
				var callback = Function.createCallback(delegate, oDivRow);
				$addHandler(cell, "mouseover", callback);

				delegate = Function.createDelegate(this, this.OnTdMouseOut);
				callback = Function.createCallback(delegate, oDivRow);
				$addHandler(cell, "mouseout", callback);

				this._onTdClickDelegate = Function.createDelegate(this, this.OnTdClick);
				$addHandler(cell, "click", this._onTdClickDelegate);

			}
		}

		oDiv.appendChild(oTable);
		return oDiv;
	},

	IsClickInParent: function(target)
	{
		while (target.tagName && target.parentNode != this.get_element())
		{
			target = target.parentNode;
		}

		return target.parentNode == this.get_element();
	},

	//-------------------------------- handlers -----------------------------------------
	OnBlur: function(args)
	{
		if (!this.IsClickInParent(args.target))
		{
			this.HidePopup(this.get_element());
		}
	},

	OnTdMouseOver: function(event, eventArgs)
	{
		eventArgs.className = "divText_slected";
	},

	OnTdMouseOut: function(event, eventArgs)
	{
		eventArgs.className = "divText";
	},

	OnTdClick: function(args)
	{
		var cellTarget = args.target.parentNode;
		this._needUpdate = false;
		
		var selectedIndex = -1;
		if(cellTarget.parentNode.rowIndex != undefined)
			selectedIndex = cellTarget.parentNode.rowIndex;
		else if(cellTarget.parentNode.parentNode.rowIndex != undefined)
			selectedIndex = cellTarget.parentNode.parentNode.rowIndex
			
		this.set_SelectedIndex(selectedIndex);
		this.fireEventChange();
		this._needUpdate = true;
		this.HidePopup(this.get_element());
	},
	
	fireEventChange: function()
	{
		if (null != this._onChangeDelegate)
		{
			this._onChangeDelegate(this);
		}
	},

	OnTableClick: function(args)
	{
		if (!this._Disabled)
		{
			var mainDiv = this.get_element();

			if (!this._IsPopup)
			{
				this.ShowPopup(mainDiv);
			}
			else
			{
				this.HidePopup(mainDiv);
			}
		}
	},

	OnImageMouseOver: function(event, eventArg)
	{
		eventArg.className = "button_over";
	},

	OnImageMouseOut: function(event, eventArg)
	{
		eventArg.className = "button";
	},

	OnSelectStart: function()
	{
		return false;
	},

	getRoot: function()
	{
		var root = "";
		if (this._Root == "")
		{
			var rootLoc = location.href.split('/');

			for (var i = 0; i < 4; i++)
			{
				rootLoc[i] += "/";
				root += rootLoc[i];
			}
		}
		else
		{
			root = this._Root;
		}
		return root;
	},

	LoadScriptsAndStyles: function(filename, filetype)
	{
		if (filetype == "css")
		{ //if filename is an external CSS file
			var fileref = document.createElement("link");
			fileref.setAttribute("rel", "stylesheet");
			fileref.setAttribute("type", "text/css");
			fileref.setAttribute("href", filename);
			fileref.setAttribute("id", "rel_DropDownControl");
		}
		if (typeof fileref != "undefined" && document.getElementById("rel_DropDownControl") == null)
		{
			document.getElementsByTagName("head")[0].appendChild(fileref)
		}
	},
	
	set_BackgroundColor : function(color,textColor)
	{
		$get(this.get_MainTdId()).style.backgroundColor = color;
		$get(this.get_MainTdId()).style.color = textColor;
	},



	//--------------------- dispose -----------------------------------------

	dispose: function()
	{
		var element = this.get_element();
		$clearHandlers(element);

		var oImg = $get(this.get_ComboId(), this.get_element());

		if (null != oImg)
		{
			$clearHandlers(oImg);
		}

		var oDiv = $get(this.get_PopupId(), this.get_element());
		var oTable = null;

		if (null != oDiv)
		{
			oTable = oDiv.childNodes[0];
		}

		if (null != oTable)
		{
			$clearHandlers(oTable);

			var i = 0;

			for (i = 0; i < oTable.rows.length; i++)
			{
				$clearHandlers(oTable.rows[i].cells[0]);
			}
		}
		$clearHandlers(element);

		this._onTableClickDelegate = null;
		this._onTdMouseOutDelegate = null;
		this._onTdMouseOverDelegate = null;
		this._onTdClickDelegate = null;
		this._onChangeDelegate = null;

		DWR.DropDownControl.callBaseMethod(this, 'dispose');
	}
}

DWR.DropDownControl.registerClass('DWR.DropDownControl', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
