window.onload = initMenu;

function initMenu()
{
	var e = document.getElementById("theMenu");
	e = e.firstChild;
	while (e)
	{
		if (e.className == "sectionButton")
		{
			attachEvent(e, "mouseover", sectionHover);
			attachEvent(e, "mouseout", sectionHover);
		}
		else if ((e.className == "dropButton") || (e.className == "dropButton2"))
		{
			attachEvent(e, "mouseover", dropHover);
			attachEvent(e, "mouseout", dropHover);
			attachEvent(e, "mousedown", dropClick);
		}
		else if (e.className == "dropList")
		{
			attachEvent(e, "mouseover", dropListHover);
			attachEvent(e, "mouseout", dropListHover);
			attachEvent(e, "mouseup", dropListClick);
		}
		else if (e.className == "item")
		{
			attachEvent(e, "mouseover", itemHover);
			attachEvent(e, "mouseout", itemHover);
		}
		if (e.className == "subitems")
		{
			e = e.firstChild;
		}
		else
		{
			e = e.nextSibling;
		}
	}
}

function attachEvent(element, eventName, func)
{
	if (navigator.userAgent.indexOf("MSIE") < 0)
	{
		element.addEventListener(eventName, func, false)
	}
	else
	{
		element.attachEvent("on" + eventName, func);
	}
}

function dropClick(event)
{
	var eCombo = (event.srcElement) ? event.srcElement : event.target;
	if (eCombo.className.substr(0, 10) != "dropButton") eCombo = eCombo.parentNode;
	showDropList(eCombo);
}

function dropHover(event)
{
	var eCombo = (event.srcElement) ? event.srcElement : event.target;
	if (eCombo.className.substr(0, 10) != "dropButton") eCombo = eCombo.parentNode;
	var eList = eCombo.nextSibling; if (eList.nodeType != 1) eList = eList.nextSibling;

	var visible = (eList.style.display == "block");
	if (visible && (eList.className == "dropList"))
	{
		// don't highlight when drop list is open (visible)
	}
	else
	{
		if (eCombo.className == "dropButton2")
		{
			eCombo.style.backgroundColor = (event.type == "mouseover") ? "#d8d8d8" : "";
		}
		else
		{
			eCombo.style.backgroundColor = (event.type == "mouseover") ? "#A0A0A0" : "#808080";
		}
	}
}

function dropListHover(event)
{
	var eItem = (event.srcElement) ? event.srcElement : event.target;
	var eList = (eItem.className == "dropList") ? eItem : eItem.parentNode;

	if (eList != eItem)
	{
		eItem.style.color       = (event.type == "mouseover") ? "#FF4400" : "";
		eItem.style.borderColor = (event.type == "mouseover") ? "#FF4400" : "";
	}

	if (eList.timeoutID)
	{
		clearTimeout(eList.timeoutID);
		eList.timeoutID = null;
	}
	if (event.type == "mouseout")
	{
		if (eList.style.display == 'block')
		{
			eList.timeoutID = setTimeout("dropListTimeout('" + eList.id + "')", 500);
		}
	}
}

function dropListTimeout(id)
{
	var eList = document.getElementById(id);
	hideDropList(eList);
}

function dropListClick(event)
{
	var eItem = (event.srcElement) ? event.srcElement : event.target;
	if (eItem.className == "dropList") return;
    
    var id = eItem.id;
    
	var eList = eItem.parentNode;
	var eCombo = eList.previousSibling;
	if (eCombo.nodeType != 1) eCombo = eCombo.previousSibling;

	var eCaption = eCombo.getElementsByTagName("div")[0];
	eCaption.innerHTML = eItem.innerHTML;
	
	hideDropList(eItem.parentNode);
	
	var e = document.getElementById("theLanguage");
	e.value = id;
	e.form.submit();
}

function showDropList(eCombo)
{
	var eList = eCombo.nextSibling; if (eList.nodeType != 1) eList = eList.nextSibling;

	var hide = (eList.style.display == "block");

	eList.style.display = (hide) ? "none" : "block";

	if (eList.className != "dropList")
	{
		var eImage = eCombo.getElementsByTagName("div")[0];
		eImage.style.backgroundPosition = (hide) ? "0px 0px" : "0px 17px";
	}
}

function hideDropList(eList)
{
	var eCombo = eList.previousSibling;
	if (eCombo.nodeType != 1) eCombo = eCombo.previousSibling;

	if (eList.className == "dropList")
	{
		eCombo.style.backgroundColor = "";
	}
	eList.style.display = "none";
}

function sectionHover(event)
{
	var e = (event.srcElement) ? event.srcElement : event.target;
	if (e.className != "sectionButton") e = e.parentNode;

	if (e.getAttribute("selected"))
	{
		// do not highlight the current
	}
	else
	{
	    e.style.backgroundPosition = (event.type == "mouseover") ? "0px 128px" : "0px 0px";
		e.style.backgroundColor = (event.type == "mouseover") ? "#A0A0A0" : "#808080";
	}
}

function itemHover(event)
{
	var e = (event.srcElement) ? event.srcElement : event.target;
	e.style.backgroundColor = (event.type == "mouseover") ? "#c1e4ff" : "";
}
