currentmenu = 0;
openTimeout = 0;
closeTimeout = 0;
waitOpenTimeout = 0;

function triggerOpen(menuId, accelerationOpen) {
	if(currentmenu) {
		document.getElementById(currentmenu).style.top = "-500px";
	}
	
	openMenu(menuId, accelerationOpen);
}

function openMenu(menuId, accelerationOpen) {
	
	menu = document.getElementById(menuId);
	oldMenu = document.getElementById(currentmenu);
			
	if(menu.style.top != "2px") {
		
		if(accelerationOpen == "40") {
			menu.style.top = "-200px";
			//close the current menu
			
			if(currentmenu) {
				if(oldMenu.style.top == "-499px") {
					clearTimeout(closeTimeout);
				}
			}
			
			if(currentmenu != menuId && currentmenu != "" && oldMenu.style.top != "-499px") {
				//closeMenu(currentmenu, 20);
			}	
			
			currentmenu = menuId;
			clearTimeout(openTimeout);
		}
		
		//show the menu mask
		menuMask = document.getElementById("menumask" + menuId);
		menuMask.style.display = "block";
		
		menuTop = menu.style.top.substring(0,menu.style.top.indexOf("px"));
		menu.style.top = parseInt(menuTop) + accelerationOpen;
		
		accelerationOpen = accelerationOpen -8;
		
		if(accelerationOpen > 0) {
			openTimeout = setTimeout("openMenu('" + menuId + "', " + accelerationOpen + ");", 5);
		}
		else {
			if(menu.style.top != "2px") {
				menu.style.top = "2px";
			}
		}
	}
	else {
		waitCloseStop();
	}
}

function closeMenu(menuId, accelerationClose) {
	if(menuId) {
		menu = document.getElementById(menuId);
		
		if(menu.style.top != "-500px") {
			if(accelerationClose == "20") {
				//menu.style.top = "2px";
			}
			
			menuTop = menu.style.top.substring(0,menu.style.top.indexOf("px"));
			menu.style.top = parseInt(menuTop) - accelerationClose;
			
			accelerationClose = accelerationClose -1;
			
			if(accelerationClose > 0) {
				closeTimeout = setTimeout("closeMenu('" + menuId + "', " + accelerationClose + ");", 1);
			}
			else {
				
				//hide the menu mask now that the menu is gone
				menuMask = document.getElementById("menumask" + menuId);
				menuMask.style.display = "none";
				
				menu.style.top = "-500px";
			}
		}
	}
}

function waitClose() {
	clearTimeout(waitOpenTimeout);
	//clearTimeout(openTimeout);
	if(currentmenu) {
		oldMenu = document.getElementById(currentmenu);
		//if(!closeTimeout) {
			closeTimeout = setTimeout("closeMenu('" + currentmenu + "', 20);", 100);
		//}		
	}
}

function waitOpen(menuId, accelerationOpen) {
	
	accelerationOpen = 40;
	
	if(currentmenu == menuId) {
		clearTimeout(closeTimeout);
	}
	
	waitOpenTimeout = setTimeout("triggerOpen('" + menuId + "', " + accelerationOpen + ");", 50);
}

function waitCloseStop() {
	if(currentmenu) {
		oldMenu = document.getElementById(currentmenu);
		if(oldMenu.style.top == "2px") {
			clearTimeout(closeTimeout);
		}		
	}
}

function writeMenu(menuArray, menuHrefArray, selected) {
	
	//generate a random seed for each menu to use as an ID
	random_num = Math.random();
	menuId = "menu" + random_num;
	
	//must be IE5/N6
	if(document.getElementById) {
	
		if(selected) {
			menuHeaderClass = "menuHeaderSelected";
		}
		else {
			menuHeaderClass = "menuHeader";
		}
	
		//write out the menus
		document.write ("<table border='0' cellpadding='0' cellspacing='0'><tr><td nowrap>");
		document.write ("<div class='menuHeaderHolder' id='menuHeader' style='padding : 1px 6px 0px 5px; width : 100%;'>");
		document.write ("<a class='" + menuHeaderClass + "' href='" + menuHrefArray[0] + "' onmouseover=\"waitOpen('" + menuId + "', 30);\" onmouseout='waitClose();'>");
		document.write (menuArray[0]);
		document.write ("</a></div></td></tr><tr><td>");
		document.write ("<div id='menumask" + menuId + "' class='menumask' style='display : none;'>");
		document.write ("<div id='" + menuId + "' class='menu' style='top : -499px' onmouseover='waitCloseStop();' onmouseout='waitClose();'>");
		document.write ("<table border='0' cellpadding='1' cellspacing='0' style='border-width : 1px; border-style : solid; border-color : #000000;'>");
		
		for(i=1; i<menuArray.length; ++i) {
			if(menuArray[i]) {
				document.write ("<tr><td nowrap><a class='menuItem' href='" + menuHrefArray[i] + "'>" + menuArray[i] + "</a></td></tr>");
			}
			else {
				document.write ("<tr><td nowrap align='center'><img src='images/grey.gif' height='1' width='98%'></td></tr>");
			}
		}
		
		document.write ("</table></div></div></td></tr></table>");
		
	}
	
}