/*
 * menuDropdown.js - implements an dropdown menu based on a HTML list
 * Author: Dave Lindquist (http://www.gazingus.org)
 */

if (!document.getElementById)
    document.getElementById = function() { return null; }

document.onclick = function() {
	if (timer != null)
		closeCurrentMenu();
}

function initializeMenu(menuId, actuatorId) {
    var menu = document.getElementById(menuId);
    var actuator = document.getElementById(actuatorId);

    if (menu == null || actuator == null) return;
    
    menu.onmouseover = function() {
    	clearTimeout(timer);
    }
    
    menu.onmouseout = function() {
    	timer = setTimeout('closeCurrentMenu();', 3*1000);
    }

    actuator.onmouseover = function() {
    	clearTimeout(timer);
    	
        if (currentMenu == null) {
            this.showMenu();
        }
        else {
            currentMenu.style.visibility = "hidden";
            currentMenu = null;
            this.showMenu();
        }

        return false;
    }
    
    actuator.onmouseout = function() {
        timer = setTimeout('closeCurrentMenu();', 3*1000);
    }
  
    actuator.onclick = function() {
        if (currentMenu == null) {
            this.showMenu();
        }
        else {
            currentMenu.style.visibility = "hidden";
            currentMenu = null;
        }

        return false;
    }

    actuator.showMenu = function() {
    	xPos = this.offsetLeft;
        tempEl = this.offsetParent;
        while (tempEl != null) {
        	xPos += tempEl.offsetLeft;
        	tempEl = tempEl.offsetParent;
        }
    	
        menu.style.left = xPos + "px";
        
        yPos = this.offsetTop;
        tempEl = this.offsetParent;
        while (tempEl != null) {
        	yPos += tempEl.offsetTop;
        	tempEl = tempEl.offsetParent;
        }
        
        menu.style.top = yPos + this.offsetHeight + "px";
        
        menu.style.visibility = "visible";
        currentMenu = menu;
    }
}

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

	return false;
}