﻿Litium = function(){}
Litium.WebControls = function(){}

Litium.WebControls.DropDownListObject = {};

Litium.WebControls.DropDownList = function(settings)
{
	Litium.WebControls.DropDownListObject.Settings = {ParentID: "#DropDownListParent", 
													  ListClass: "DropDownList",
													  ItemClass: "DropDownListItem", 
													  ItemHoverClass: "DropDownListItemHover", 
													  ItemLinkClass: "DropDownListItemLink", 
													  ItemLinkHoverClass: "DropDownListItemLinkHover", 
													  TextInputClass: "DropDownListInput",
													  TextInputDefaultText: "Select a link",
													  ButtonImageUrl: "/Styles/Images/Common/pixel.gif",
													  ButtonImageClass: "DropDownListButton",
													  ButtonContainerClass: "DropDownListButtonContianer",
													  NewWindow: false };
					 
	$.extend( Litium.WebControls.DropDownListObject.Settings, settings );
	
	Litium.WebControls.DropDownListObject.MenuActive = true;
	Litium.WebControls.DropDownListObject.Parent = $(Litium.WebControls.DropDownListObject.Settings.ParentID);
	
	Litium.WebControls.DropDownListObject.List = $(Litium.WebControls.DropDownListObject.Settings.ParentID + " > ul");
	$(Litium.WebControls.DropDownListObject.List).attr("class", Litium.WebControls.DropDownListObject.Settings.ListClass);
	$(Litium.WebControls.DropDownListObject.List).hide();
	
	Litium.WebControls.DropDownListObject.Items = $(Litium.WebControls.DropDownListObject.List).children("li");
	Litium.WebControls.DropDownListObject.FirstItem = $($(Litium.WebControls.DropDownListObject.Items).get(0));
	
	// IE6 is crap, so web haveto put the preceding elemnts in a separate div because the abs posed elements wont show
	$(Litium.WebControls.DropDownListObject.Parent).prepend("<div id=\"dropDownListIEContainer\"></div>");
	$(Litium.WebControls.DropDownListObject.Parent).children("#dropDownListIEContainer").append("<div id=\"dropDownListButton\"></div>");
	
	// Get the button
	Litium.WebControls.DropDownListObject.Button = $(Litium.WebControls.DropDownListObject.Parent).children("#dropDownListIEContainer").children("#dropDownListButton");
	
	// Append image so we get a nice button look
	$(Litium.WebControls.DropDownListObject.Button).attr("class", Litium.WebControls.DropDownListObject.Settings.ButtonContainerClass);
	var imageButton = $(Litium.WebControls.DropDownListObject.Button).append("<img alt=\"\" src=\"" + Litium.WebControls.DropDownListObject.Settings.ButtonImageUrl + "\" class=\"" + Litium.WebControls.DropDownListObject.Settings.ButtonImageClass + "\" />");
	
	// Append the textbox so IE crap can handle arrowbuttons
	$(Litium.WebControls.DropDownListObject.Parent).prepend("<input type=\"text\" readonly=\"readonly\" />");
	Litium.WebControls.DropDownListObject.FocusElement = $(Litium.WebControls.DropDownListObject.Parent).find("input").get(0);
	$(Litium.WebControls.DropDownListObject.FocusElement).attr("class", Litium.WebControls.DropDownListObject.Settings.TextInputClass);
	$(Litium.WebControls.DropDownListObject.FocusElement).val(Litium.WebControls.DropDownListObject.Settings.TextInputDefaultText);
	
	// Gets the first item
	Litium.WebControls.DropDownListObject.SelectedItem = Litium.WebControls.DropDownListObject.FirstItem;
	Litium.WebControls.DropDownListObject.SelectedIndex = 0;
	
	// Add the keydown event for handling arrowbuttons
	$(Litium.WebControls.DropDownListObject.Parent).keydown(function(e){ Litium.WebControls.DropDownList.OnKeyDown(e); });

	// Add index and class to items, and hover function and onclick event
	$.each(Litium.WebControls.DropDownListObject.Items, function(i, elem){
			$(elem).attr("index", i);
			$(elem).attr("class", Litium.WebControls.DropDownListObject.Settings.ItemClass);
			$(elem).find("a").attr("class", Litium.WebControls.DropDownListObject.Settings.ItemLinkClass); 
			$(elem).find("a").focus(function(){ Litium.WebControls.DropDownList.SetSelectedIndex(Number($(this).parent().attr("index"))); });
			
			$(elem).hover(
				function(){
					Litium.WebControls.DropDownList.SetSelectedIndex(Number($(this).attr("index")));
				},
				function(){
					Litium.WebControls.DropDownList.Deactivate(this);
				}
			);
			
			$(elem).click(function(e)
				{
					Litium.WebControls.DropDownList.OpenLink(Number($(this).attr("index")));
					return false;   
				}
			);
		} 
	);
    
    // Set button to expand menu
	$(Litium.WebControls.DropDownListObject.Button).click(function(e)
		{
			Litium.WebControls.DropDownList.ToggleMenu();
			e.stopPropagation();
			return false;
		}
	);
	
	// Set textbox to expand menu
	$(Litium.WebControls.DropDownListObject.FocusElement).click(function(e)
		{
			Litium.WebControls.DropDownList.ToggleMenu();
			e.stopPropagation();
			return false;
		}
	);
    
	$(document).click(function(e)
	{
		if (Litium.WebControls.DropDownListObject.MenuActive) Litium.WebControls.DropDownList.ToggleMenu();
	});
	
	$(window).scroll(function (e) { Litium.WebControls.DropDownList.Scroll(e); } );
	
	Litium.WebControls.DropDownList.ToggleMenu();
}

Litium.WebControls.DropDownList.OpenLink = function(index)
{
	if (Litium.WebControls.DropDownListObject.MenuActive)
	{
		if(Number(index) > -1)
		{
			$.each(Litium.WebControls.DropDownListObject.Items, function(i, elem)
				{
					if(i == index)
					{
						var url = $(elem).find("a").attr("href");
						if(url != "#")
						{
							if(!Litium.WebControls.DropDownListObject.Settings.NewWindow)
							{
								document.location.href = url;
								Litium.WebControls.DropDownList.ToggleMenu();   
							}
							else
							{
								var win = window.open(url,"_blank","");
								Litium.WebControls.DropDownList.ToggleMenu();
								win.focus();
							}
						}
					}
				}
			);
		}
		else
		{
			if(Number(Litium.WebControls.DropDownListObject.SelectedIndex) > -1)
				Litium.WebControls.DropDownList.OpenLink(Litium.WebControls.DropDownListObject.SelectedIndex);
		}
	}
}

Litium.WebControls.DropDownList.ToggleMenu = function()
{
	Litium.WebControls.DropDownListObject.MenuActive = !Litium.WebControls.DropDownListObject.MenuActive;

	if(Litium.WebControls.DropDownListObject.MenuActive){
		Litium.WebControls.DropDownList.SetSelectedIndex(0);
		$(Litium.WebControls.DropDownListObject.FocusElement).focus();
		$(Litium.WebControls.DropDownListObject.List).show();
	}
	else{
		Litium.WebControls.DropDownList.SetSelectedIndex(-1);
		$(Litium.WebControls.DropDownListObject.FocusElement).blur();
		$(Litium.WebControls.DropDownListObject.FocusElement).val(Litium.WebControls.DropDownListObject.Settings.TextInputDefaultText);
		$(Litium.WebControls.DropDownListObject.List).hide();
	}
	
	//$(Litium.WebControls.DropDownListObject.Items).each( function(){ $(this).toggle(); } );
}

Litium.WebControls.DropDownList.SetSelectedIndex = function(index)
{
	$.each(Litium.WebControls.DropDownListObject.Items, function(i, elem)
		{
			if(i == index){
				Litium.WebControls.DropDownList.Activate(elem);
			}
			else{
				Litium.WebControls.DropDownList.Deactivate(elem);
			}
		}
	);
}

Litium.WebControls.DropDownList.Activate = function(elem)
{
	$(elem).attr("class", Litium.WebControls.DropDownListObject.Settings.ItemHoverClass);
	$(elem).find("a").attr("class", Litium.WebControls.DropDownListObject.Settings.ItemLinkHoverClass);
	Litium.WebControls.DropDownListObject.SelectedItem = elem;
	Litium.WebControls.DropDownListObject.SelectedIndex =  Number($(elem).attr("index"));
	
	//$(Litium.WebControls.DropDownListObject.FocusElement).val($(elem).find("a").text());
}

Litium.WebControls.DropDownList.Deactivate = function(elem)
{
	$(elem).attr("class", Litium.WebControls.DropDownListObject.Settings.ItemClass);
	$(elem).find("a").attr("class", Litium.WebControls.DropDownListObject.Settings.ItemLinkClass);
}

Litium.WebControls.DropDownList.First = function()
{
	Litium.WebControls.DropDownList.SetSelectedIndex(0);
}

Litium.WebControls.DropDownList.Previouse = function()
{
	if(Litium.WebControls.DropDownListObject.SelectedIndex > 0)
		Litium.WebControls.DropDownList.SetSelectedIndex((Litium.WebControls.DropDownListObject.SelectedIndex - 1));
}

Litium.WebControls.DropDownList.Next = function()
{
	if(Litium.WebControls.DropDownListObject.SelectedIndex < (Litium.WebControls.DropDownListObject.Items.length - 1))
		Litium.WebControls.DropDownList.SetSelectedIndex((Litium.WebControls.DropDownListObject.SelectedIndex + 1));
}

Litium.WebControls.DropDownList.Last = function()
{
	Litium.WebControls.DropDownList.SetSelectedIndex((Litium.WebControls.DropDownListObject.Items.length - 1));
}

Litium.WebControls.DropDownList.Scroll = function(e)
{
	var delta = 0;
	
	if (!e) /* For IE. */
        e = window.event;
                
	if (e.wheelDelta) 
	{ /* IE/Opera. */
		delta = e.wheelDelta/120;
			/** In Opera 9, delta differs in sign as compared to IE. */
		if (window.opera)
			delta = -delta;
	} 
	else if (e.detail) 
	{ 
		/** Mozilla case. */
		/** In Mozilla, sign of delta is different than in IE.
		 * Also, delta is multiple of 3.
		 */
		delta = -e.detail/3;
	}
	/** If delta is nonzero, handle it.
	 * Basically, delta is now positive if wheel was scrolled up,
	 * and negative, if wheel was scrolled down.
	 */
	if (delta)
	{
		if(delta < 0)
			Litium.WebControls.DropDownList.Previouse();
		else
			Litium.WebControls.DropDownList.Next();
	}
	
	/** Prevent default actions caused by mouse wheel.
	 * That might be ugly, but we handle scrolls somehow
	 * anyway, so don't bother here..
	 */
	if (e.preventDefault)
		e.preventDefault();
	
	e.returnValue = false;
}

Litium.WebControls.DropDownList.OnKeyDown = function(e)
{
	if(Litium.WebControls.DropDownListObject.MenuActive)
	{
		switch(e.keyCode)
		{
			case 33:	// page up
			case 36:	// home
				Litium.WebControls.DropDownList.First();
				e.preventDefault();
				break;
			case 34:	// page down
			case 35:	// end
				Litium.WebControls.DropDownList.Last();
				e.preventDefault();
				break;
			case 38:	// up
			case 37:	// left
				Litium.WebControls.DropDownList.Previouse();
				e.preventDefault();
				break;
			case 39:	// right
			case 40:	// down
				Litium.WebControls.DropDownList.Next();
				e.preventDefault();
				break;
			case 13: // enter
				Litium.WebControls.DropDownList.OpenLink(Litium.WebControls.DropDownListObject.SelectedIndex);
				break;
			case 27: // escape
				Litium.WebControls.DropDownList.ToggleMenu();
				break;
			default:
				// Select by char?
				break;

		}
	}
}
