
/******************/
/* QuickLink Menu */
/******************/

var QuickLinksMenu = function(containerId, selectMenuId, displayDefaultOption, buttonValue, buttonClass, buttonImageUrl) {

	this.ContainerId = containerId;
	this.SelectMenuId = selectMenuId;
	this.DisplayDefaultOption = displayDefaultOption;
	this.ButtonClass = buttonClass;
	this.ButtonValue = buttonValue;
	this.ButtonImageUrl = buttonImageUrl;
	this.Init = function() {

		if(!document.getElementById || !document.getElementsByTagName || !document.createTextNode) {
			return;
		}

		var _containerElement = document.getElementById(this.ContainerId);
		var _optionArray = QuickLinkOption.getOptionsFromList(_containerElement);
		
		//Remove child nodes
		DomUtility.EmptyContainer(_containerElement);
		
		//Add list
		var _select = this.CreateList(_optionArray);
		_containerElement.appendChild(_select);
		
		//Add button
		_containerElement.appendChild(this.CreateListButton());
		
	}
}

/* Instance methods */

QuickLinksMenu.prototype.CreateList = function(optionList) {
	
	var _select = document.createElement("select");
	var _option = null;

	_select.setAttribute("id", this.SelectMenuId);
	
	//Create default option
	if(this.DisplayDefaultOption)
	{
		var _defaultOption = document.createElement("option");
		_defaultOption.setAttribute("value", "0");
		_defaultOption.appendChild(document.createTextNode("Select one"));
		_select.appendChild(_defaultOption);
	}
	
	
	for(i=0; i < optionList.length; i++)
	{
		_option = document.createElement("option");
		_option.setAttribute("value", optionList[i].Value);
		_option.appendChild(document.createTextNode(optionList[i].Text));
		_select.appendChild(_option);
	}
	return _select;
}


QuickLinksMenu.prototype.CreateListButton = function() {
	
	var _button = document.createElement("input");
	if(this.ButtonImageUrl != null)
	{
	  //_button.setAttribute("type", "button");
		_button.setAttribute("type", "image");
		_button.setAttribute("src", this.ButtonImageUrl);
	}
	else
	{
		_button.setAttribute("type", "button");
	}
	
	_button.setAttribute("value", this.ButtonValue);
	_button.setAttribute("class", this.ButtonClass);;
	
	var selectMenuId = this.SelectMenuId;
	var list = document.getElementById(selectMenuId);
	var listValue = null;
	_button.onclick = function(){
		listValue = list.options[list.options.selectedIndex].value;
		if(listValue != 0) 
		{
		  location.href = listValue;
		  return false;
		}
	}
	return _button;
}



/********************/
/* QuickLink Option */
/********************/

var QuickLinkOption = function(value, text)
{
	this.Value = value;
	this.Text = text;
}

/* Static methods */

QuickLinkOption.getOptionsFromList = function(containerElement)
{
	var nodeList = containerElement.childNodes;

	var optionList = new Array();
	var optionText;
	var optionValue;
	
	for(var i=0; i<nodeList.length; i++)
	{
		if(nodeList[i].nodeType == (document.ELEMENT_NODE | 1))
		{
			switch(nodeList[i].tagName.toLowerCase())
			{
				case "ul":
					optionList = QuickLinkOption.getOptionsFromList(nodeList[i]);
					break;

				case "li":
					optionValue = DomUtility.getUrlFromListItem(nodeList[i]);
					optionText = DomUtility.getTextFromListItem(nodeList[i]);
					optionList[optionList.length] = new QuickLinkOption(optionValue, optionText);
					break;
			}
		}
	}
	return optionList;
}





/***************/
/* Dom Utility */
/***************/

var DomUtility = function(){};

/* Static methods */

DomUtility.EmptyContainer = function(container)
{
	if(container.hasChildNodes())
	{
		while(container.childNodes.length >= 1)
			container.removeChild(container.firstChild);
	}
}

DomUtility.getUrlFromListItem = function(containerElement) {
	var nodeList = containerElement.childNodes;
	for(var i=0; i<nodeList.length; i++)
	{
		if(nodeList[i].nodeType == (document.ELEMENT_NODE | 1))
		{
			if(nodeList[i].tagName.toLowerCase() == "a")
				return nodeList[i].getAttribute("href");
		}
	}
}

DomUtility.getTextFromListItem = function(containerElement) {
	var nodeList = containerElement.childNodes;
	for(var i=0; i<nodeList.length; i++)
	{
		if(nodeList[i].nodeType == (document.ELEMENT_NODE | 1))
		{
			if(nodeList[i].childNodes[0].nodeValue != "")
				return nodeList[i].childNodes[0].nodeValue;
		}
	}
}


