﻿/*
     In order for the left nav menu to work without frames all the currently open 
    menu items must be sent to the next page each time a menu link is clicked.  
    
    To work, this sytem needs an input element on every page that has a left nav that is to 
    expanded (every secondary master page). It should look like:
    <input type="hidden" name="itemsToBeExpanded" id="itemsToBeExpanded" value=""/>
    
    This system also relies on a call to some functions that must appear in script tags
    after the menu elements on each page where there is a left nav that is to be expanded 
    (every secondary master page).
    It should look like:
    <!-- Load the ids of divs that need to be expanded from the querystring, then expand them. -->
    <script type="text/javascript" language="javascript"> loadExpandedItems(); expandItems();</script>
    
    Each menu item, in this case a div that contains a asp:hyperlink element or UL must have an
        onclick="navMenuClick('<id of div to be expanded>', this.id);" 
    property. Where <id of div to be expanded> is either an id or an empty string. Empty strings
    must be used for nav items that won't actually expand antying.  Every div that calls 
    this function must have an id.
    
    This system works by keeping track of all currently open menu items in the html input
    and adding query string text to each link before the page tries to follow it. Upon loading
    the page calls functions that look for the names of any menu items that are supposed to be openad
    in the query string and then does the CSS expansion.
    The names of the open menu items are stored in the input, double comma delimited. An id of "stomper"
    will look like ",stomper,".
    
    The cycle of activity is as follows:
    When a user clicks on a menu item,
            1.  If it is not expanded already add the currently hidden div's id to the itemsToBeExpanded input.
                If it is currently expanded then remove the corresponding div's id from the the itemsToBeExpanded.
            2.  Before the click registeres with the actual link, change the link's href by adding a querystring
                parameter that is the itemsToBeExpanded.
    When the page loads up,
            1.  Parse the querysting for any incomming itemsToBeExpanded and add them back to the currenly
                empty(because the page just loaded) itemsToBeExpanded input.
            2.  Use the information currently in itemsToBeExpanded to look up by id and expand each menu item
                that is supposed to be expanded.
                
*/
        /*This is the function that does the actual expanding by changing the style attribute of a div.*/
       function expand(thistag) {
           styleObj = document.getElementById(thistag).style;
           if (styleObj.display == 'none') {
               styleObj.display = 'block';
           }
       }
       
       /*This function actually changes the "class" attribute to visualy indicate it is selected.*/
       function hilightCurrentMenuItemFromInput()
       {
            var itemId;
            itemId = document.currentMenuItem;
            
            if(itemId != '' && itemId != null && itemId != undefined)
            {
                document.getElementById(itemId).className = 'selectedmenuitem';
                
                document.getElementById(itemId).firstChild.className = 'current';
            }
       }
       
       /*This function actually changes the "class" attribute to visualy indicate it is selected.*/
       function hilightCurrentMenuItem(itemId)
       {
            if(itemId != '' && itemId != null && itemId != undefined)
            document.getElementById(itemId).style.className = 'selectedmenuitem';
       }
       
       /*This function parses the URL and extracts out the itemsToBeExpanded and puts it in the itemsToBeExpanded input.*/
       function loadCurrentMenuItem()
       {
            var url = document.URL ;
            
            if(url.match("currentMenuItem"))
            document.currentMenuItem = url.substring(url.indexOf("currentMenuItem=") + 16 ,url.indexOf("endCurrentMenuItem")) ;
            
            
       }
       
       /*This function parses the URL and extracts out the itemsToBeExpanded and puts it in the itemsToBeExpanded input.*/
       function loadExpandedItems()
       {
            var url = document.URL ;
            
            if(url.match("itemsToBeExpanded"))
            document.itemsToBeExpanded = url.substring(url.indexOf("itemsToBeExpanded=") + 18 ,url.indexOf("&currentMenuItem=")) ;
            
       }
       
       /*This function appends the itemsToBeExpaned to the querystring part of every link on the page*/
       function appendExpandedItemsToLinks()
       {
            var linkArray = document.getElementsByTagName("A");
            //TODO: Only change links that link to this global nav.
            
            if(document.itemsToBeExpanded == undefined)
            {document.itemsToBeExpanded = "";}
            
            
            for( var link = 0; link < linkArray.length; link++)
            {
                if(!linkArray[link].href.match("\\?"))
                linkArray[link].href += "?";
                
                //use the commented out code directly below instead of the code two lines below, if expandable menus are ever reactivated
                //linkArray[link].href += "&itemsToBeExpanded=" + document.itemsToBeExpanded + "&currentMenuItem=" + document.currentMenuItem + "endCurrentMenuItem";
                linkArray[link].href += "&currentMenuItem=" + document.currentMenuItem + "endCurrentMenuItem";
            }
       }
       
       function storeCurrentMenuItem(hilightItemId)
       {
            //alert("storeCurrentMenuItem: " + hilightItemId);
            document.currentMenuItem = hilightItemId;
            //alert("currentMenuItem: " + document.currentMenuItem ;
       }
       
       /*This function adds or removes ids from the itemsToBeExpanded input.*/
       function toggleItemToBeExpanded(itemId){
            
            if (document.itemsToBeExpanded == null || document.itemsToBeExpanded == undefined)
            {
                document.itemsToBeExpanded = "";
            }
            
            if(itemId != "" && itemId != null && itemId != undefined)
            {
                if(! document.itemsToBeExpanded.match("," + itemId + ","))
                {
                    document.itemsToBeExpanded += "," + itemId +",";
                }else{
                    var tempStr = document.itemsToBeExpanded.substring(0, document.itemsToBeExpanded.indexOf("," + itemId + ","));
                    document.itemsToBeExpanded = tempStr + document.itemsToBeExpanded.substring( document.itemsToBeExpanded.indexOf("," + itemId + ",") + itemId.length + 2, document.itemsToBeExpanded.length);
                    
                }

            }
       }
       
       /*This function gets all divs and looks for any that have a matching id in the intemsToBeExpanded
         input.  If so it calls the expand function on them.   
       */
       function expandItems()
       {
            var strItemsMarkedForExpansion = document.itemsToBeExpanded;
            var itemsThatMightBeExpanded   = document.getElementsByTagName("DIV");
            
            if(strItemsMarkedForExpansion != null && strItemsMarkedForExpansion != undefined)
            for( var item = 0; item < itemsThatMightBeExpanded.length; item++)
            {

                if(itemsThatMightBeExpanded[item].id != "" && itemsThatMightBeExpanded[item].id != null && itemsThatMightBeExpanded[item].id != undefined )
                if(strItemsMarkedForExpansion.match("," + itemsThatMightBeExpanded[item].id + ","))
                {
                    expand(itemsThatMightBeExpanded[item].id);
                }
                
            }
            
       }
       
       
       function navMenuClick(itemToExpandId, itemToHilightId)
       {
            storeCurrentMenuItem(itemToHilightId);
            //alert(itemToHilightId);
            //toggleItemToBeExpanded(itemToExpandId);
            appendExpandedItemsToLinks();
       }
       
       