// CONSTANTS
var MOUSEOVER		= "#D30E34";
var MOUSEOUT		= "#606178";
var SUBMOUSEOVER	= "#B10F29";
var SUBMOUSEOUT		= "#CB0F2D";

var subMenuArray = new Array();
var timer = null;

if (![].pop) Array.prototype.pop = ArrayPop;
if (![].push) Array.prototype.push = ArrayPush;

function ArrayPop() {
	var tempItem = this[this.length-1];
	this.length--;
	return tempItem;
}
function ArrayPush() {
	this[this.length] = arguments[0];
	return this.length;
}

function hideSubMenus() {
	var el;

	while (subMenuArray.length > 0) {
		el = subMenuArray.pop();
		el.style.visibility = "hidden";
	}
}

function mouseOver() {
	if (this.className != "selected")
		this.style.backgroundColor = MOUSEOVER;
	
	this.style.cursor = "hand";
	
	if (this.subMenu != null) {
		clearTimeout(timer);
		hideSubMenus();
		this.subMenu.style.visibility = "visible";
		subMenuArray.push(this.subMenu);
	}
}

function mouseOut() {
	if (this.className != "selected")
		this.style.backgroundColor = MOUSEOUT;
	
	this.style.cursor = "none";
	
	if (this.subMenu != null) {
		timer = setTimeout("hideSubMenu()", 100);
	}
}

function subMouseOver() {
	this.style.backgroundColor = SUBMOUSEOVER;
	this.style.cursor = "hand";
	clearTimeout(timer);
}

function subMouseOut() {
	this.style.backgroundColor = SUBMOUSEOUT;
	this.style.cursor = "none";
	
	timer = setTimeout("hideSubMenu()", 100);
}

function hideSubMenu() {
	var el = subMenuArray.pop();
	if (el != null)
		el.style.visibility = "hidden";
}

function initMenu() {
	var el, elSub;
	var parentEl = document.getElementById("menucontainer");
	if (parentEl && parentEl.hasChildNodes()) {
		// add mouse handlers for the menu items
		for (var i = 0; i < parentEl.childNodes.length; i++) {
			el = parentEl.childNodes.item(i);
			if (el.className == "menuitem_left" || el.className == "menuitem") {
				elSub = findSubDivEl(el);
				el = findMainDivEl(el);
				if (el) {
					setupSubMenu(el, elSub);
					el.onmouseover = mouseOver;
					el.onmouseout = mouseOut;
				}
			}
		}
	}
}

function setupSubMenu(el, elSub) {
	if (elSub == null) {
		el.subMenu = null;
		return;
	}
	
	var ancEl;
	var left = el.offsetWidth - elSub.offsetWidth;
	
	el.subMenu = elSub;
	elSub.style.left = left + "px";
	if (elSub.hasChildNodes()) {
		for (var i = 0; i < elSub.childNodes.length; i++) {
			ancEl = elSub.childNodes.item(i);
			if (ancEl.tagName == "A") {
				ancEl.onmouseover = subMouseOver;
				ancEl.onmouseout = subMouseOut;
			}
		}
	}
}

function findMainDivEl(el) {
	var child;
	
	if (el.hasChildNodes()) {
		for (var i = 0; i < el.childNodes.length; i++) {
			child = el.childNodes.item(i);
			if (child.tagName == "DIV" && child.className != "submenu")
				return child;
		}
	}
	return null;	// no DIVs found
}

function findSubDivEl(el) {
	var child;
	
	if (el.hasChildNodes()) {
		for (var i = 0; i < el.childNodes.length; i++) {
			child = el.childNodes.item(i);
			if (child.tagName == "DIV" && child.className == "submenu")
				return child;
		}
	}
	return null;	// no DIVs found
}

function findAnchorEl(el) {
	var child;
	
	if (el.hasChildNodes()) {
		for (var i = 0; i < el.childNodes.length; i++) {
			child = el.childNodes.item(i);
			if (child.tagName == "A")
				return child;
		}
	}
	return null;	// no As found
}


onload = initMenu;