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

var currentMenu = null;

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

function findLeftPosition(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 findTopPosition(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 isSelected(menu, actuator) {
     //location.pathname
    // location.search for string after ?
    if (menu.hasChildNodes()) {
        var filePath = this.getPageLocation();
        //alert(filePath);
        var level1LINodes = menu.getElementsByTagName("li");
        //alert(level1LINodes.length);
        for (i = 0; level1LINodes.length > i ; i++) {
            var level1LINode = level1LINodes[i];
            if ((level1LINode != null) && (level1LINode.hasChildNodes())){
                var aNodes = level1LINode.getElementsByTagName("a");
                for (j = 0; aNodes.length > j ; j++) {
                    var aA = aNodes[j];
                    if ((aA != null) && (filePath.indexOf(cleanHREF(aA.getAttribute("href"))) != -1)) {
                        //alert("we found it ");
                       return true;
                    }
                }                
            }
        }
    }
}

function getPageLocation() {
    var index1 = location.pathname.indexOf("/");
    var index2 = location.pathname.indexOf("/",index1+1);
    var index3 = location.pathname.indexOf("/",index2+1);
    var index4 = location.pathname.length;
    return location.pathname.substring(index3+1,index4);
}

function cleanHREF(href) {
    var index1 = 0;
    while (index1 != -1) {
        index1 = href.indexOf("../");
        href = href.substring(index1+1, href.length);
    }
    //alert(href);
    return href;
}


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() {
        //menu.style.left = "-" + (findLeftPosition(this)) + "px"
        menu.style.left = "0px"
        //menu.style.left = this.offsetLeft + "px";
        
        menu.style.top = findTopPosition(this) + this.offsetHeight + 3 + "px";
        //menu.style.top = this.offsetTop + this.offsetHeight + 3 + "px";
        menu.style.visibility = "visible";
        currentMenu = menu;
    }
    
    if (isSelected(menu, actuator)) {
        actuator.showMenu();
    }
}
