var currentMenu = null;
var menuHideTimer = null;
var REQUIRE_CLICK_TO_DROP_MENU = false;
var MENU_HIDE_TIMEOUT = 5000;

function _HideMenu()
{
	if (currentMenu)
	{
		currentMenu.style.visibility = "hidden";
		currentMenu = null;
	}
}

function _InitializeMenu(menuId, actuatorId)
{
	if (document.getElementById == null) return;
	
    var menu = document.getElementById(menuId);
    var actuator = document.getElementById(actuatorId);
    
    if (menu == null || actuator == null) return;

    actuator.onmouseover = function onmouseover()
    {
		clearHideTimer();            

    	if (REQUIRE_CLICK_TO_DROP_MENU)
    	{
			if (currentMenu)
			{
				// only show menus on mouseover if we are tracking
				this.showMenu();
			}
    	}
    	else
    	{
    		this.showMenu();
    	}
    }
  
    actuator.onclick = function onclick()
    {
    	if (REQUIRE_CLICK_TO_DROP_MENU == false)
    	{
	  		this.hideMenu();
    		return true;	// follow the link, do no futher work
    	}
    
        if (currentMenu == null)
	  	{
	  		this.showMenu();
	  	}
	  	else
	  	{
	  		this.hideMenu();
	  	}
	  	
        return false; 	// don't follow the link
    }
    
    menu.onclick = function()
    {
		hideMenu();
		return true;	// follow the link
    }

    actuator.showMenu = function showMenu()
    {
    	var fudgeH = 0;
    	var fudgeV = 0;
    	
		this.hideMenu();
		
		// fudge for IE Mac

    	if ((navigator.appVersion.indexOf("Macintosh") != -1) &&
    		(navigator.appVersion.indexOf("MSIE") != -1))
       	{
    		fudgeH = 11;
    		fudgeV = 5;
    		
    		// IE 5 Mac
    		// will make menus as wide as parent element if not forced
    		
    		menu.style.width = '150px';
    	}
    
        menu.style.left = (this.offsetLeft - fudgeH) + "px";
        menu.style.top = (this.offsetTop + this.offsetHeight - fudgeV) + "px";
        menu.style.visibility = "visible";

        currentMenu = menu;

		startHideTimerWithTimeout(MENU_HIDE_TIMEOUT);
    }

	function hideMenu()
    {
        if (currentMenu)
        {
            currentMenu.style.visibility = "hidden";
            currentMenu = null;
        }
    }

	actuator.hideMenu = hideMenu;
	
	function mousemoveHandler()
	{
		startHideTimerWithTimeout(MENU_HIDE_TIMEOUT);
		
		// if we are an actuator, and there is no current menu
		// make sure we re-show ourselves if necessary
		
		if ((false == REQUIRE_CLICK_TO_DROP_MENU) && (currentMenu == null) && this.showMenu)
			this.showMenu();
	}
	
	actuator.onmousemove = mousemoveHandler;
	menu.onmousemove = mousemoveHandler;

	function startHideTimerWithTimeout(timeout)
	{
		clearHideTimer();
			
		menuHideTimer = setTimeout('_HideMenu()', timeout);
	}
	
	function startHideTimer()
	{
		var timeout = 500;
		
		if (REQUIRE_CLICK_TO_DROP_MENU == false)
			timeout = 0;
			
		startHideTimerWithTimeout(timeout);
	}
	
	function clearHideTimer()
	{
		if (menuHideTimer != null)
			clearTimeout(menuHideTimer);
	}
	
	actuator.onmouseout = function()
	{
		startHideTimer();
	}
    
   	menu.onmouseover = function()
   	{
		this.style.visibility = "visible";
		clearHideTimer();
	}
	
	menu.onmouseout = function()
	{
		startHideTimer();
	}
	
}

function InitializeMenus(menus)
{
	for (var i=0; i<menus.length; i++)
	{
		var	menuId = menus[i] + "Menu";
		var actuatorId = menus[i] + "Actuator";

		_InitializeMenu(menuId, actuatorId);	
	}
}
