// browser detection
agt = navigator.userAgent.toLowerCase();
window.ns6 = (document.getElementById && !document.all) ? 1 : 0;
window.ie5 = (document.all) ? 1 : 0;
window.opera = (agt.indexOf("opera") != -1) ? 1 : 0;

// timer variables
window.mmHideMenuTimer = null;
window.mmStart = null;
window.mmDHFlag = false;
window.mmHideTimeOut = 100;

// global variables
window.menus = new Array();
window.activeMenus = new Array();
window.activeMenuItem = null;
window.displayedMenus = new Array()

// configurations
gColor = "#FFFFFF";
gColorHi = "#FFFFFF";
gBg = "#CE0000";
gBgHi = "#AAAAAA";
gBorder = "#00bfff";
gBorderWidth = 1;
gChildMenuIcon = "arrows.gif";
gDownArrowIcon = "tridown.gif";
gSubmenuXOffset = -5;
gSubmenuYOffset = 5;
gMenuItemHeight = 15;

// styles
gFontStyle = "font:bold 10px Verdana;";
gPadStyle = "padding:3px;"
gColorStyle = "color:" + gColor + "; background-color:" + gBg + ";";

// id generators
gMenuId = 0;
gItemId = 0;

function Menu(label, type, slide) 
{
	gMenuId++;
	this.label = "" + label;
	this.type = type;
	this.slide = slide;
	this.id = "mn" + gMenuId;
	this.items = new Array();
	this.itemIds = new Array();
	this.actions = new Array();
	this.selectedItem = null;
	this.addMenuItem = addMenuItem;
	window.menus[window.menus.length] = this;
}

function addMenuItem(label, action) {
	gItemId++;
	this.items[this.items.length] = label;
	this.actions[this.actions.length] = action;
	this.itemIds[this.itemIds.length] = "itm" + gItemId;
}

/*
function debugShowMenuTree(menu, level) {
	var debug = document.frmDebug.out;
	var label = "";
	var child = null;
	var i = 0;
	
	if (level == 1) prefix = "";
	else if (level == 2) prefix = "    ";
	else if (level == 3) prefix = "        ";
	
	label = menu.label;
	debug.value += prefix + label + "\n";
	
	for (i = 0; i < menu.items.length; i++) {
		child = menu.items[i];
		if (child.label) {
			debugShowMenuTree(child, level+1);
		}
		else {
			debug.value += prefix + prefix + child + "\n";
		}
		
	}
}
*/

function getAnchorTopPos() {
	var y = 0;
	var layer = findObj("holder");
    while (layer.offsetParent != null) {
      y += layer.offsetTop;
      layer = layer.offsetParent;
    }
    y += layer.offsetTop;
    return y;
}

function menuInitiate() {
	var menu = window.menus[0];
	writeMenu(menu);
	menuLayer = findObj(menu.id);
	menuLayer.style.left = "0px";
	menuLayer.style.top = "116px";
//	menuLayer.style.top = getAnchorTopPos() + "px";
	menuLayer.style.visibility = "visible"
	window.displayedMenus[window.displayedMenus.length] = menuLayer;
}


function writeMenu(objMenu) {
	var htm = "";
	var child = null;
	var label = "";
	var i = 0;
	var menu = objMenu;
	var menuLayer = null;
	var itemLayer = null;
	
	if (menu.type == "top") {
		htm += "<div id='" + menu.id + "' style='position:absolute; visibility:hidden; cursor:hand; background-color:" + gBorder + ";'>\n";
		htm += "<table width=780 cellpadding=0 cellspacing=" + gBorderWidth + " border=0>\n"
		htm += "<tr>\n";

		for (i = 0; i < menu.items.length; i++) {
			child = menu.items[i];
			if (child.label) {
				writeMenu(child);
				label = child.label;
			}
			else {
				label = child;
			}
			htm += "<td><div id='" + menu.itemIds[i] + "' style='width:100%; cursor:hand;" + gFontStyle + gPadStyle + gColorStyle + "' onMouseOver='onMenuItemOver(this)' onMouseOut='onMenuItemOut(this)' onClick='onMenuItemAction(this)'>" + label + "</div></td>\n";
		}
		
		htm += "</tr></table></div>\n";
		document.write(htm);
	}
	else if (menu.type == "ver") {
		htm += "<div id='" + menu.id + "' style='position:absolute; visibility:hidden; cursor:hand; background-color:" + gBorder + ";'>\n";
		htm += "<table cellpadding=0 cellspacing=" + gBorderWidth + " border=0>\n"

		for (i = 0; i < menu.items.length; i++) {
			child = menu.items[i];
			if (child.label) {
				writeMenu(child);
				label = child.label;
			}
			else {
				label = child;
			}
			htm += "<tr><td><div id='" + menu.itemIds[i] + "' style='width:100%; cursor:hand;" + gFontStyle + gPadStyle + gColorStyle + "' onMouseOver='onMenuItemOver(this)' onMouseOut='onMenuItemOut(this)' onClick='onMenuItemAction(this)'>" + label + "</div></td></tr>\n";
		}
		
		htm += "</table></div>\n";
		document.write(htm);
	}
	
	// set up the layers
	menuLayer = findObj(menu.id);
	menu.layer = menuLayer;
	menuLayer.menu = menu;
	
	for (i = 0; i < menu.items.length; i++) {
		itemLayer = findObj(menu.itemIds[i]);
		itemLayer.menu = menu;
		itemLayer.action = menu.actions[i];

		child = menu.items[i];
		if (child.label) {
			itemLayer.childMenu = child;
		}
	}

	menuLayer.onmouseover = MM_clearTimeout;
	menuLayer.onmouseout = MM_startTimeout;
}

function showChildMenu(itmLayer) {
	if (itmLayer.childMenu) {
		var childLayer = itmLayer.childMenu.layer;
		var menuLayer = itmLayer.menu.layer;
		var childSlide = itmLayer.childMenu.slide;
		//if (childSlide == "down"){
			//FindObj the left position
			var offLeft = 0;
			var p = itmLayer;
			while (p)
			{
				offLeft += p.offsetLeft;
				p = p.offsetParent;
			}
			var offRight = offLeft + itmLayer.offsetWidth + gBorderWidth;
			if ((offLeft + childLayer.offsetWidth) > 780) {
				offLeft = offRight - childLayer.offsetWidth;
			}
			else {
				offLeft -= gBorderWidth;
			}
			
			//FindObj the top position
			var offTop = 0;
			var p = itmLayer;
			while (p)
			{
				tmpOffset = p.offsetTop;
				offTop += p.offsetTop;
				p = p.offsetParent;
			}
			offTop += itmLayer.offsetHeight;
			
			var s = childLayer.style;
			s.zIndex = menuLayer.style.zIndex + 1;
			s.top = offTop + 'px';
			s.left = offLeft + 'px';
			s.visibility = "visible";
		//}
	}
}

function hideChildMenu(itmLayer) {
	if (itmLayer.menu.selectedItem){
		var selLayer = itmLayer.menu.selectedItem;
		if (selLayer == itmLayer) return;
		if (selLayer.childMenu) {
			hideChildMenu(selLayer.childMenu.layer);
			selLayer.childMenu.layer.style.visibility = "hidden";
		}
		selLayer.style.color = gColor;
		selLayer.style.backgroundColor = gBg;
		itmLayer.menu.selectedItem = null;
	}
}

function hideDisplayedMenus() {
	var activeItem = window.activeMenuItem;
	if (activeItem) {
		activeItem.style.color = gColor;
		activeItem.style.backgroundColor = gBg;
	} 
	window.activeMenuItem = null;
	for (var i = 0; i < window.displayedMenus.length; i++) {
		hideChildMenu(window.displayedMenus[i]);
	}
	return;
}

function onMenuItemOut(itmLayer) {
	MM_startTimeout();
	return true;
}

function onMenuItemOver(itmLayer) {
	MM_clearTimeout();
	
	if (itmLayer.style) {
		hideChildMenu(itmLayer);
		itmLayer.style.backgroundColor = gBgHi;
		itmLayer.style.color = gColorHi;
		itmLayer.zIndex = 1;
		itmLayer.menu.selectedItem = itmLayer;
		showChildMenu(itmLayer);
	} 
	else return;
	window.activeMenuItem = itmLayer;
	window.event.cancelBubble = true;
}

function onMenuItemAction(itmLayer) {
	//if (!itmLayer) return;
	hideDisplayedMenus();
	if (itmLayer.action) {
		window.location = itmLayer.action;
	}
}

function MM_clearTimeout() {
	if (window.mmHideMenuTimer) clearTimeout(window.mmHideMenuTimer);
	window.mmHideMenuTimer = null;
	window.mmDHFlag = false;
}

function MM_startTimeout() {
	window.mmStart = new Date();
	window.mmDHFlag = true;
	window.mmHideMenuTimer = setTimeout("mmDoHide()", window.mmHideTimeOut);
}

function mmDoHide() {
	if (!window.mmDHFlag) return;
	var elapsed = new Date() - window.mmStart;
	var timeout = window.mmHideTimeOut;
	if (elapsed < timeout) {
		window.mmHideMenuTimer = setTimeout("mmDoHide()", timeout + 100 - elapsed);
		return;
	}
	window.mmDHFlag = false;
	hideDisplayedMenus();
}

function findObj(item) {
	if (document.all) return(document.all[item]);
	if (document.getElementById) return(document.getElementById(item));
	return(false);
}