// stores the currently selected parent menu item
var lastParent = null;
// stores the currently selected sub menu item
var lastSubSel = null;
// stores the currently visible sub menu
var lastSubMenu = null;

// this function will give a parent menu item the selected style
function setParentSel(o) {
    // test to make sure we have a valid object
    if (o != null) {
        // clear the style of the last selected item
        clearClass(lastParent);
        // set the style class
        o.setAttribute('class', 'parentMenu');
        o.setAttribute('className', 'parentMenu');
        // this item is now the currently selected item
        // this is used to clear the class if another item is selected
        lastParent = o;
    }
}

function setSubSel(o) {
    // test to make sure we have a valid object
    if (o != null) {
        // clear the style of the last selected sub item
        clearClass(lastSubSel);
        // set the style class
        o.setAttribute('class', 'childMenu');
        o.setAttribute('className', 'childMenu');       
        // this item is now the currently selected sub item  
        lastSubSel = o;
    }
}

function setSubMenu(o) {
    // test to make sure we have a valid object
    if (o != null) {
        // set the class
        o.setAttribute('class', 'selectedsubMenu');
        o.setAttribute('className', 'selectedsubMenu');
    }
    // this is now the current sub menu
    lastSubMenu = o;
}

function clearSubMenu(o) {
    // test to make sure we have a valid object
    if (o != null) {
        // reset the sub menu class back to the default
        o.setAttribute('class', 'submenu');
        o.setAttribute('className', 'submenu');
    }
}

function clearClass(o) {
    // test to make sure we have a valid object
    if (o != null) {
        // clear class values
        o.setAttribute('class', '');
        o.setAttribute('className', '');
    }
}

function parentMenuhover(e,o) {

    if (o == null) {
        return;
    }

    // clear the current selection
    clearClass(lastParent);
    // hide the current sub menu
    clearSubMenu(lastSubMenu);
    // set the new selection
    setParentSel(o);
    // get the sub menu for the selected item
    var child = o.getElementsByTagName('ul')[0];
    // make the sub menu visible
    setSubMenu(child);
}

function parentMenuout(e,o) {
    if (o == null) {
        return;
    }
    if (e != null) {
        var obj;
        if (e.relatedTarget) {
            obj = e.relatedTarget;
        }
        else if (e.toElement) {
            obj = e.toElement;
        }
        else if (e.srcElement) {
            obj = e.srcElement;
        }
        if (obj.tagName == 'A' || obj.tagName == 'UL') {
            return;
        }        
    }

    // get the current submenu
    var child = o.getElementsByTagName('ul')[0];
    // clear the style of the selected item
    clearClass(lastParent);
    // hide the sub menu
    clearSubMenu(child);

    lastParent = mainsel;
    // if we are on a page that exists in the menu
    if (lastParent != null) {
        // pretend that we are hovering over it.
        // this causes the selection to set itself when there is no mouse activity

        parentMenuhover(null, lastParent);

        if (subsel != null) {
            // pretend we are hovering over that link
            childMenuhover(null, subsel);
            lastSubSel = subsel;
        }
    }
}

function childMenuhover(e,o) {
    if (o == null) {
        return;
    }
//    if (e.relatedTarget) {
//        o = e.relatedTarget;
//    }
//    else if (e.toElement) {
//        o = e.toElement;
//    }
//    if (o.tagName == 'A') {
//        o=o.parentNode;
//    }
//    if (o.tagName != 'LI') {
//        return;
//    }
    // clear the old sub selection's class
    clearClass(lastSubSel);
    // set the new sub selection
    setSubSel(o);
}

function childMenuout(e,o) {
    if (o == null) {
        return;
    }
    if (e != null) {
        var obj;
        if (e.relatedTarget) {
            obj = e.relatedTarget;
        }
        else if (e.toElement) {
            obj = e.toElement;
        }
        else if (e.srcElement) {
            obj = e.srcElement;
        }       
        if ((obj.tagName == 'A') || (obj.tagName == 'UL' && obj.id != 'selectedsubMenu')) 
              {            
            return;
        }
    }
    // clear the selected item's class
    clearClass(o);
    lastSubSel = subsel;
    // if we are on a page that exists in the menu
    if (lastSubSel != null) {
        // pretend we are hovering over that link
        childMenuhover(null,lastSubSel);
    }
}

function extractFilename(s) {
    // extracts just the file name from a string
    return s.substring(s.lastIndexOf('/') + 1);
}

function extractDirectories(s)
{
       var temp = s.substring(0, s.lastIndexOf('/'));
       return temp.split('/');
}

// this variable will hold the menu item that is used to navigate to the current page
var mainsel = null;
// this variable will hold the sub menu item that is used to navigate to the current page
var subsel = null;

function getClass(o) {
    var cl = o.getAttribute('class');
    if(cl == null) {
        cl = o.getAttribute('className');
    }
    return cl;
}

// this runs on page load and is used to determine if there is a default selection in the menu.
function MainMenuLoad() {
    // we need to get the file name for the current page so we can compare it to the links
    // in the menu to determine which link should be selected.
    
       var dirs = extractDirectories(window.location.href);
       var curDir = dirs[dirs.length-1].toLowerCase();
       var parentDir = dirs[dirs.length-2].toLowerCase();
    // get all of the hyperlinks in the #menu
    var anchors = document.getElementById('menu').getElementsByTagName('a');

    // we have to go through every link in the menu to find the one that refers to which page we are on
    for (c = 0; c < anchors.length; ++c) {
              var linkdirs = extractDirectories(anchors[c].href);
              var linkdir = linkdirs[linkdirs.length-1].toLowerCase();
              var linkparent = linkdirs[linkdirs.length-2].toLowerCase();
        // this checks to see if the link points to current page
        if (curDir == linkdir && linkparent == parentDir) {
            // this will check to see if the link is part of a sub menu.
            if (getClass(anchors[c].parentNode.parentNode) == 'submenu') {
                // set appropriate classes
                setSubSel(anchors[c].parentNode);
                setParentSel(anchors[c].parentNode.parentNode.parentNode);
                setSubMenu(anchors[c].parentNode.parentNode)
                // remember what the current selections are so we can use them later
                mainsel = anchors[c].parentNode.parentNode.parentNode;
                subsel = anchors[c].parentNode;
            }
            // it must be a parent item
            else {
                setParentSel(anchors[c].parentNode);
                setSubMenu(anchors[c].parentNode.getElementsByTagName('ul')[0])
                mainsel = anchors[c].parentNode;
            }
            // this stops the loop from continuing once we've found a match
            break;
        }
    }
    // set the currently selected items to the selected items for the page
    lastParent = mainsel;
    lastSubSel = subsel;
       // if document.getElementById("content").style.height
       document.getElementById("content").style.height = getHeight() - 284 + "px";
}



function getHeight()
{
       var ret = self.innerHeight;
       
       // Explorer 6 Strict Mode
       if (document.documentElement
       && document.documentElement.clientHeight) {
              ret = document.documentElement.clientHeight;
       } 
       // other Explorers
       else if (document.body) {
              ret = document.body.clientHeight;
       }
       
       if(document.height > ret)
       {
              ret = document.height;     
       }
       else if(document.body.clientHeight > ret)
       {
              ret = document.body.clientHeight; 
       }
       return ret;
}

function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

