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

var currentMenu = null;
var previousInvertedActuator = 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 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) {
    //first take out the domain if there is one
    var index = href.indexOf("http://");
    if(index != -1) {
         href = href.substring(index+7, href.length);
         index = href.indexOf("/");
         // keep the first slash
         href = href.substring(index, href.length);
    }
    //
    index = 0;
    while (index != -1) {
        index = href.indexOf("../");
        href = href.substring(index+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();           
        }
        if (this != previousInvertedActuator) this.style.color = "#000";
    }
    
    actuator.onmouseout = function() {
        if (this != previousInvertedActuator) this.style.color = "#fff";
    }
 
    actuator.onclick = function() {
        if (currentMenu == null) {
            this.showMenu();
            this.invertMenu();
        }
        //
        else {
            currentMenu.style.visibility = "hidden";
            this.showMenu();
            this.invertMenu();
            //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;
    }

    actuator.invertMenu = function() {
        if (previousInvertedActuator != null) {
            previousInvertedActuator.parentNode.style.backgroundImage = "none";
            previousInvertedActuator.style.color = "#fff";
        }
        this.parentNode.style.backgroundImage = "url(css/images/MenuBGSelected.gif)";
        this.parentNode.style.backgroundRepeat = "repeat-x";
        this.style.color = "#272f81";
        previousInvertedActuator = this;
    }
    
     actuator.isSelectedMenu = function(aMenu) {
        if (aMenu.hasChildNodes()) {
            var filePath = getPageLocation();
            var level1LINodes = aMenu.getElementsByTagName("li");
            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)) {
                            this.invertMenu();
                            aA.style.color = "#272f81";
                            return true;
                        }
                    }                
                }
            }
        }
     }
    if (actuator.isSelectedMenu(menu)) {
        actuator.showMenu();
    }
}
