/*
 * menuDropdown.js - implements an dropdown menu based on a HTML list
 * Author: Dave Lindquist (dave@gazingus.org)
 */

var currentMenu = null;

if (!document.getElementById)
    document.getElementById = function() { return null; }

function initializeMenu(menuId, actuatorId, menuBarId) {
    var menu = document.getElementById(menuId);
    var actuator = document.getElementById(actuatorId);
	var menuBar = document.getElementById(menuBarId);

    if (menu == null || actuator == null) return;

    //if (window.opera) return; // I'm too tired

    actuator.onmouseover = function() {
       if (currentMenu) {
          currentMenu.style.visibility = "hidden";
          this.showMenu();
       } else if (currentMenu == null) {
            this.showMenu();
        }
    }
  
    actuator.onclick = function() {
        if (currentMenu == null) {
            this.showMenu();
        }
        else {
            currentMenu.style.visibility = "hidden";
            currentMenu = null;
        }

        return false;
    }

	actuator.onmouseout = function() {
		if (window.event.toElement == currentMenu) {
			// nothing -- keep showing
		} else if ( (window.event.toElement.id == "childHREF") && (currentMenu !=null ) ) {
			// skipped currentMenu bounds somehow, but landed on top of a child HREF, do nothing
		}
		else {
        	currentMenu.style.visibility = "hidden";
        	currentMenu = null;
		}
	}
		
    actuator.showMenu = function() {
        menu.style.left = this.offsetLeft + "px";
        menu.style.top = this.offsetTop + this.offsetHeight + "px";
        menu.style.visibility = "visible";
        currentMenu = menu;
    }
		
	menu.onmouseout = function() {
//		alert("w.e.sE.c=" + window.event.srcElement.className + "w.e.sE.id=" + window.event.srcElement.id);
		if (currentMenu == null) {
			// nothing
		}
		else if (window.event.srcElement.className == "actuator") {
			// nothing
		}
		else if ( (window.event.srcElement.className == "menu") && (window.event.toElement.id == "childHREF") ) {
			// we've entered a child HREF inside this menu from an non-HREF location in menu. For this to work, all HREF elements inside a menu must have id="childHREF"
		}
		else if ( (window.event.srcElement.id == "childHREF") && (window.event.toElement.id == "childHREF") && (currentMenu !=null ) ) {
			// we've entered a child HREF inside the menu from another HREF. Do nothing
		}
		else if (window.event.toElement.id == currentMenu.id) {
			// we've entered a non-HREF location in the existing currentMenu (probably from a childHREF), do nothing
		}
		else {
			// we've left the bounds of the box
   		    	currentMenu.style.visibility = "hidden";
   		        currentMenu = null;
		}			
	}
}
