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

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.onfocus = function onfocus()
    {
          if (currentMenu)
          {
               // only show menus on mouseover if we are tracking
               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.onblur = function()
    {
		hideMenu();
		return true;	// follow the link
    }

    menu.onclick = function()
    {
		hideMenu();
		return true;	// follow the link
    }

    actuator.showMenu = function showMenu()
    {
          var fudgeH = 0;
          var fudgeV = 0;


		// 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.width = '150px';
          menu.style.left = (findPosX(this) - fudgeH) + "px";
          menu.style.top = ( findPosY(this) + this.offsetHeight) + "px";
          menu.style.visibility = "visible";

          currentMenu = menu;

		startHideTimerWithTimeout(MENU_HIDE_TIMEOUT);
     }

     function findPosX(obj)
     {
	    var curleft = 0;
	    if (obj.offsetParent)
	    {
               while (obj.offsetParent)
               {
                    curleft += obj.offsetLeft
                    obj = obj.offsetParent;
               }
          }
          else if (obj.x)
               curleft += obj.x;
          return curleft;
     }

     function findPosY(obj)
     {
          var curtop = 0;
          if (obj.offsetParent)
          {
               while (obj.offsetParent)
               {
                    curtop += obj.offsetTop
                    obj = obj.offsetParent;
               }
          }
          else if (obj.y)
               curtop += obj.y;
          return curtop;
     }

     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();
	}

	function onFocusHandler()
	{
		// if we are an actuator, and there is no current menu
		// make sure we re-show ourselves if necessary

		if ((currentMenu == null) && this.showMenu)
			this.showMenu();
	}

	actuator.onmousemove = mousemoveHandler;
	menu.onmousemove = mousemoveHandler;
     actuator.onfocus = onFocusHandler;
	menu.onfocus = onFocusHandler;
     
	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.onfocus = function()
   	{
		this.style.visibility = "visible";
		clearHideTimer();
	}

	menu.onblur = function()
	{
		startHideTimer();
	}
	
	menu.onmouseout = function()
	{
		startHideTimer();
	}

} // _InitializeMenu end

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

		_InitializeMenu(menuId, actuatorId);
	}
}

