/************************************************************************************************************

(C) www.dhtmlgoodies.com, October 2005

This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.   

Terms of use:
You are free to use this script as long as the copyright message is kept intact. However, you may not
redistribute, sell or repost it without our permission.

Update log:

March, 15th: Fixed problem with sliding in MSIE

Thank you!

www.dhtmlgoodies.com
Alf Magne Kalleland

************************************************************************************************************/   

var expandFirstItemAutomatically = false;   // Expand first menu item automatically ?
var initMenuIdToExpand = "initExpand";      // Id of menu item that should be initially expanded. the id is defined in the <li> tag.
var menuColor = "#0058a1";                  // Color to be used for current node.
var menuIdToColor = "currentLink";          // Id of menu item (the current node) that should be colored.
var expandMenuItemByUrl = true;             // Menu will automatically expand by url - i.e. if the href of the menu item is in the current location, it will expand
var slidedownMenuId = "slidedownMenu"       // Id of the topmost DIV containing the slidedown menu.
var iterHeight = 15;                        // Height used to iterate through hide/show loops.

var initialMenuItemAlwaysExpanded = true;   // NOT IMPLEMENTED YET

var slideMenuObj;
var divToScroll = false;
var ulToScroll = false; 
var divCounter = 1;
var otherDivsToScroll = new Array();
var divToHide = false;
var parentDivToHide = new Array();
var ulToHide = false;
var offsetOpera = 0;

if (navigator.userAgent.indexOf("Opera") >= 0) offsetOpera = 1;   

var slideMenuHeightOfCurrentBox = 0;
var objectsToExpand = new Array();
var initExpandIndex = 0;
var alwaysExpandedItems = new Array();

function popMenusToShow()
{

    var obj = divToScroll;
    var endArray = new Array();

    while (obj && obj.tagName != "BODY") {

        if (obj.tagName == "DIV" && obj.id.indexOf("slideDiv") >= 0) {

            var objFound = -1;

            for (var no=0; no < otherDivsToScroll.length; no++) {

                if (otherDivsToScroll[no] == obj) {
                    objFound = no;      
                }                   

            }   

            if (objFound >= 0) {
                otherDivsToScroll.splice(objFound,1);   
            }       

        }   

        obj = obj.parentNode;

    }   

}

// ************************************************************************************
// *  showSubMenu is the function that will be called via the onclick event.   This
// *  function is copied into the onclick event of elements displayed in the menu.
// ************************************************************************************ 

function showSubMenu(e, inputObj) 
{ 

    if (this && this.style) {

        var currNode = document.getElementById(menuIdToColor);

        if (currNode) {
            currNode.style.color = "#002747";
            currNode.removeAttribute("id");
        }

        this.style.color = menuColor;
        this.id = menuIdToColor;

    }

    if (this && this.tagName) {
        inputObj = this.parentNode;
    }

    if (inputObj && inputObj.tagName == "LI") {

        divToScroll = inputObj.getElementsByTagName("DIV")[0];

        for (var no=0; no < otherDivsToScroll.length; no++) {

            if (otherDivsToScroll[no] == divToScroll) {
                return;
            }

        }           

    }

    hidingInProcess = false;

    if (otherDivsToScroll.length > 0) {

        if (divToScroll) {                

            if (otherDivsToScroll.length > 0) {
                popMenusToShow();
            }

            if (otherDivsToScroll.length > 0) { 
                autoHideMenus();
                hidingInProcess = true;
            }

        }   

    }       

    if (divToScroll && !hidingInProcess) {

        divToScroll.style.display = "";
        otherDivsToScroll.length = 0;
        otherDivToScroll = divToScroll.parentNode;
        otherDivsToScroll.push(divToScroll);    

        while (otherDivToScroll && otherDivToScroll.tagName != "BODY") {

            if (otherDivToScroll.tagName == "DIV" && otherDivToScroll.id.indexOf("slideDiv") >= 0) {
                otherDivsToScroll.push(otherDivToScroll);              
            }

            otherDivToScroll = otherDivToScroll.parentNode;
        }           

        ulToScroll = divToScroll.getElementsByTagName("UL")[0];

        if (divToScroll.style.height.replace("px","") / 1 <= 1) {
            scrollDownSub();
        }

    }

}

function autoHideMenus()
{

    if (otherDivsToScroll.length > 0) {

        divToHide = otherDivsToScroll[otherDivsToScroll.length-1];
        parentDivToHide.length = 0;

        var obj = divToHide.parentNode.parentNode.parentNode;

        while (obj && obj.tagName == "DIV") {           

            if (obj.id.indexOf("slideDiv") >= 0) {
                parentDivToHide.push(obj);
            }

            obj = obj.parentNode.parentNode.parentNode;

        }

        var tmpHeight = (divToHide.style.height.replace("px","") / 1 - slideMenuHeightOfCurrentBox);

        if (tmpHeight < 0) {
            tmpHeight = 0;
        }

        if (slideMenuHeightOfCurrentBox) {
            divToHide.style.height = tmpHeight  + "px";
        }

        ulToHide = divToHide.getElementsByTagName("UL")[0];
        slideMenuHeightOfCurrentBox = ulToHide.offsetHeight;
        scrollUpMenu();     

    } else {
        slideMenuHeightOfCurrentBox = 0;
        showSubMenu();          
    }

}

function scrollUpMenu()
{

    var height = divToHide.offsetHeight;

    height -= iterHeight;

    if (height < 0) {
        height = 0;
    }

    divToHide.style.height = height + "px";

    for (var no=0; no < parentDivToHide.length; no++) {   
        parentDivToHide[no].style.height = parentDivToHide[no].getElementsByTagName("UL")[0].offsetHeight + "px";
    }

    if (height > 0) {
        setTimeout("scrollUpMenu()",5);
    } else {
        divToHide.style.display = "none";
        otherDivsToScroll.length = otherDivsToScroll.length - 1;
        autoHideMenus();            
    }

}   

function scrollDownSub()
{

    if (divToScroll) {            

        var height = divToScroll.offsetHeight / 1;

        var offsetMove = Math.min(iterHeight, (ulToScroll.offsetHeight - height));

        height = height + offsetMove;
        divToScroll.style.height = height + "px";
        
        for (var no=1; no < otherDivsToScroll.length; no++) {
            var tmpHeight = otherDivsToScroll[no].offsetHeight / 1 + offsetMove;
            otherDivsToScroll[no].style.height = tmpHeight + "px";
        }           

        if (height < ulToScroll.offsetHeight) {
            setTimeout("scrollDownSub()",5); 
        } else {

            divToScroll = false;
            ulToScroll = false;

            if (objectsToExpand.length > 0 && initExpandIndex < (objectsToExpand.length-1)) {
                initExpandIndex++;
                showSubMenu(false, objectsToExpand[initExpandIndex]);
            }

        }

    }

}
    
function initSubItems(inputObj, currentDepth)
{       

    divCounter++;       

    var div = document.createElement("DIV");    // Creating new div     
    div.style.overflow = "hidden";  
    div.style.position = "relative";
    div.style.display ="none";
    div.style.height = "1px";
    div.id = "slideDiv" + divCounter;
    div.className = "slideMenuDiv" + currentDepth;      

    inputObj.parentNode.appendChild(div);   // Appending DIV as child element of <LI> that is parent of input <UL>      

    div.appendChild(inputObj);  // Appending <UL> to the div

    var menuItem = inputObj.getElementsByTagName("LI")[0];

    while (menuItem) {

        if (menuItem.tagName == "LI") {

            var tag = menuItem.getElementsByTagName("SPAN")[0];

            if (!tag) {
                tag = menuItem.getElementsByTagName("A")[0];
            }

            tag.className = "slMenuItem_depth" + currentDepth; 
            var subUl = menuItem.getElementsByTagName("UL");

            if (subUl.length>0) {
                initSubItems(subUl[0], currentDepth+1);                  
            }

            if (tag.onclick) {
            
                if (tag.onclick == "") {
                    tag.onclick = showSubMenu;
                } 

            } else {
                tag.onclick = showSubMenu;
            }

        }           

        menuItem = menuItem.nextSibling;                        

    }       

}

function initSlideDownMenu()
{

    var initExpandedMenuObj = document.getElementById(initMenuIdToExpand);

/* 
if (initExpandedMenuObj) {
    alert("Element found where id is " + initExpandedMenuObj.id);
} else {
    alert("Element not found where id is " + initMenuIdToExpand);
} 
*/

    slideMenuObj = document.getElementById(slidedownMenuId);

    if (slideMenuObj) {

        slideMenuObj.style.visibility = "visible";

        var mainUl = slideMenuObj.getElementsByTagName("UL")[0];      
        var mainMenuItem = mainUl.getElementsByTagName("LI")[0];

        mainItemCounter = 1;

        while (mainMenuItem) {            

            if (mainMenuItem.tagName == "LI") {

                var tag = mainMenuItem.getElementsByTagName("SPAN")[0];
                var tagType = "SPAN";

                if (!tag) {
                    tag = mainMenuItem.getElementsByTagName("A")[0];
                    tagType = "A";
                }

                tag.className = "slMenuItem_depth1"; 

                var subUl = mainMenuItem.getElementsByTagName("UL");

                mainMenuItem.id = "mainMenuItem" + mainItemCounter;
                mainItemCounter++;

                if (tag.onclick) {
            
                    if (tag.onclick == "") {
                        tag.onclick = showSubMenu;
                    }

                } else {
                    tag.onclick = showSubMenu;
                }

                if (subUl.length > 0) {
                    initSubItems(subUl[0], 2);
                }               

            }           

            mainMenuItem = mainMenuItem.nextSibling;    

        }       
    
        if (location.search.indexOf("mainMenuItemToSlide") >= 0) {

            var items = location.search.split("&");

            for (var no=0; no < items.length; no++) {

                if (items[no].indexOf("mainMenuItemToSlide") >= 0) {
                    values = items[no].split("=");
                    showSubMenu(false, document.getElementById("mainMenuItem" + values[1])); 
                    initMenuIdToExpand = false;             
                }

            }           

        } else if (expandFirstItemAutomatically > 0) {

            if (document.getElementById("mainMenuItem" + expandFirstItemAutomatically)) {
                showSubMenu(false,document.getElementById("mainMenuItem" + expandFirstItemAutomatically));
                initMenuIdToExpand = false;
            }

        }

        if (expandMenuItemByUrl) {

            var aTags = slideMenuObj.getElementsByTagName("A");

            for (var no=0; no < aTags.length; no++) {

                var hrefToCheckOn = aTags[no].href;             

                if (location.href.indexOf(hrefToCheckOn) >= 0 && hrefToCheckOn.indexOf("#") < hrefToCheckOn.length-1) {

                    initMenuIdToExpand = false;
                    var obj = aTags[no].parentNode;

                    while(obj && obj.id != slidedownMenuId) {

                         if (obj.tagName == "LI") {                          

                            var subUl = obj.getElementsByTagName("UL");

                            if (initialMenuItemAlwaysExpanded) {
                                alwaysExpandedItems[obj.parentNode] = true;
                            }

                            if (subUl.length > 0) {                             
                                objectsToExpand.unshift(obj);
                            }

                        }

                        obj = obj.parentNode;   
                    }

                    showSubMenu(false, objectsToExpand[0]);
                    break;                  

                }           

            }

        }
            
        if (initMenuIdToExpand && initExpandedMenuObj) {

            objectsToExpand = new Array();

            var obj = initExpandedMenuObj;

            while (obj && obj.id != slidedownMenuId) {

                if (obj.tagName == "LI") {

                    var subUl = obj.getElementsByTagName("UL");

                    if (initialMenuItemAlwaysExpanded) {
                        alwaysExpandedItems[obj.parentNode] = true;
                    }

                    if (subUl.length > 0) {                     
                        objectsToExpand.unshift(obj);
                    }

                }

                obj = obj.parentNode;   

            }

            /*
            alert("initExpandedMenuObj id is " + initExpandedMenuObj.id +
                ", and obj id is " + obj.id + ".");
            */

            showSubMenu(false, objectsToExpand[0]);


            // ************************************************************************
            // *  If an element has an id value that matches the value specified in 
            // *  the initialization of the menuIdToColor variable (see variable
            // *  initializations at the top),  then that element needs to be colored;
            // *  otherwise, the first element with a tag name of 'A' that is found
            // *  under the initial element to expand should be colored.
            // ************************************************************************

            var currNode = document.getElementById(menuIdToColor);

            if (!currNode) {
                currNode = initExpandedMenuObj.getElementsByTagName("A")[0];
            }

            if (currNode) {
                currNode.style.color = menuColor;
                currNode.id = menuIdToColor;
            }

        }

    }
    
} 

// ************************************************************************************
// *  This function is only used for Javascript testing purposes.  It simulates the
// *  effects of the form post.
// ************************************************************************************ 

function simulateFormPost(obj) {

    var currNode = document.getElementById(menuIdToColor);

    if (currNode) {
        currNode.style.color = "#002747";
        currNode.removeAttribute("id");
    }

    obj.style.color = menuColor;
    obj.id = menuIdToColor;

}

// ************************************************************************************
// *  The staticValue function is used to simulate a static variable containing a 
// *  numeric amount.
// ************************************************************************************ 

function staticValue(action) {

    if (!action) {
        action = "";
    }

    if (typeof staticValue.number == "undefined") {
        staticValue.number = 0;
    }

    if (action == "+") {
        staticValue.number++;
    }

    return staticValue.number;

}

