/*
 * menuDropdown.js - implements an dropdown menu based on a HTML list
 * Author: Dave Lindquist (http://www.gazingus.org)
 * Modification (noted below) with code from http://www.quirksmode.org/js/findpos.html
 */

var currentMenu = null;

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

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

    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();
        }
    }
  
    actuator.onclick = function() {
        if (currentMenu == null) {
            this.showMenu();
        }
        else {
            currentMenu.style.visibility = "hidden";
            currentMenu = null;
        }

        return false;
    }

    actuator.showMenu = function() {
    	/* Below is a modification from Lindquist's version
    	 * Lindquist works fine if there are no page margins, but sometimes we want those
    	 */
        menu.style.left = findPosX(this) + "px";
        menu.style.top = findPosY(this) + this.offsetHeight + "px";
        menu.style.visibility = "visible";
        currentMenu = menu;
    }
    
    /* The following functions taken entirely from http://www.quirksmode.org/js/findpos.html, with one bugfix in each */
    function findPosX(obj)
	{
			var curleft = 0;
			if (obj.offsetParent)
			{
					while (obj.offsetParent)
					{
							curleft += obj.offsetLeft;
							obj = obj.offsetParent;
					}
					curleft += obj.offsetLeft /* this bugfix by Jay Tamboli */
			}
			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;
					}
					curtop += obj.offsetTop; /* this bugfix by Jay Tamboli */
			}
			else if (obj.y)
					curtop += obj.y;
			return curtop;
	}
}
