/* 
 * thanks to Dave Lindquist (dave@gazingus.org) for a working example ot the core idea:
 * http://www.gazingus.org/html/menuDropdown.html 
 * (implements an dropdown menu based on a HTML list) 
 * 
 * also thanks to Philip Shaw of codestyle.com for the prototypic js
 * to make the menus behave more ... menu-like:
 * http://www.codestyle.org/scripts/dom/css/visibility-HorizontalMenuBuilder.js
 * 
 * and Garrett Smith for the bit of browser sniffing 
 * 
 * and inevitably, to Zeldman
 * 
 * code modified/integrated/improved(?) by David Fields (fivecentdesign@f2o.org | david.fields@normandale.edu) 
 * first implemented at Normandale Community College (http://www.normandale.edu/) 
 * 
 * embrace and extend, by all means. and please pass on the recognition.
 */

var ua= navigator.userAgent; 
var currentMenu = null;
var menuItems = null;
var currentActuator = null;
var Timeout_ID = null;

// don't bother with non-DOM1-compliant browsers
if (!document.getElementById)
	document.getElementById = function() { return null; }

// initializeMenu contains all the action
//-------------------------------------------
function initializeMenu(menuId, actuatorId) {
	var menu = document.getElementById(menuId);
	var actuator = document.getElementById(actuatorId);
	
	if (menu == null || actuator == null) return;
	
	if (window.opera) return; // just not bothering for now 
	
	if (ua.indexOf("Mac") > 0 && ua.indexOf("MSIE 5") > 0) return; // ditto

  function showMenu() {
    // If DOM1 supported and element exists ...
    if((document.getElementById)&&(menu!=null)){
      // If this menu is already open ...
      if(currentMenu==menu){
        // Do not close it
        clearTimeout(Timeout_ID);
      }
      // Another might still be open ...
      else{
        // If another menu is open ...
        if(currentMenu!=null){
          // Do not wait, shut it now
          clearTimeout(Timeout_ID);
          hideNow();
        }
      }
      // This is the new 'live' menu, make it visible
      currentMenu = menu;
      currentActuator = actuator;
      // currentMenu.style.visibility is initially empty in IE5 until
      // it is assigned by these functions, so must check that
      // it's not null before proceeding...
      if((currentMenu.style)&&(currentMenu.style.visibility!=null)){
        currentMenu.style.left = this.offsetLeft + 'px';
        currentMenu.style.top = this.offsetTop + this.offsetHeight + 'px';
        // display is originally set to "none" 
        // to keep horizontal scrollbars from appearing 
        // before the menu has been positioned 
        currentMenu.style.display = 'block';
        // from now on, we just switch style.visibility 
        currentMenu.style.visibility = 'visible';
        //this.style.backgroundColor = '#f0f4fc';
        //this.style.borderLeft = '1px solid #a6b9ca';
        this.style.fontWeight = 'bold';
      }
    }
  }

  // to shut the menu after a bit of time
  //-------------------------------------------
  function hideMenu() {
    // If DOM1 supported and a menu is open ...
    if((document.getElementById)&&(currentMenu!=null)) {
      // set the timer to shut it    
      Timeout_ID = window.setTimeout(hideNow,250);
    }
  } 

  function keepOpen() {
    clearTimeout(Timeout_ID);
  }
    
  
  // shut previous menu immediately
  //---------------------------------------
  function hideNow(){
    if((document.getElementById)&&(currentMenu!=null)){
      currentMenu.style.visibility = 'hidden';
      currentActuator.style.backgroundColor = 'transparent';
      currentActuator.style.borderLeft = '0';
      currentActuator.style.fontWeight = 'normal';
      currentMenu = null;
    }
  }
  
  menu.onmouseover=keepOpen;
  menu.onmouseout=hideMenu;

	actuator.onmouseover=showMenu;
  actuator.onmouseout=hideMenu;
}
