///////////////////////////////////////////
// 
//  Author: jtnt for Erickson Barnett
//  Creation date: September 30, 2005
// 
//  Last modified: October 3, 2005
// 
///////////////////////////////////////////



classoff = "";

function init() {

    if (document.getElementById){

// just fill some variables to access the parts of the page we need later
    
        node = document.getElementById("w-navdiv");
        listItems = node.getElementsByTagName("li");
        links = node.getElementsByTagName("a");
        
        // set the ids of the list items based on the href
        
        for (i = 0; i < listItems.length; i++) {
            
            href = links[i].getAttribute('href'); // gets the href of the links
            hreflength = href.length; // gets the length of the href
            basestr = href.slice(1,hreflength); // gets just the part after the hash mark (#)
            newid = basestr + "-li"; // sets a variable to hold the id name 
            
            listItems[i].setAttribute('id',newid) // sets the id of all the LIs
            
            links[i].onclick = switchit; // assigns the onclick event to each of the nav links
        
        } // for loop
    
    }
    
    
    // check for bookmarks and direct links with an anchor
    
    loc = location.href;
    
    if (loc.search(/#/) > -1) { // only to the rest of there is an anchor in the URL
            
        loclength = loc.length; // get length of location.href
        hashpos = loc.lastIndexOf('#')+1; // get the position of the hash mark + 1
        
        anchor = loc.slice(hashpos,loclength); // get the text after the hash mark
        
        liid = anchor + "-li";
           
        listItems[0].className = ""; // turn the first LI off
        
        for (i = 0; i < listItems.length; i++) {
        
            theid = listItems[i].getAttribute('id');
            
            if (theid.lastIndexOf(liid) != -1) { // if the id of the LI and the anchor match

                listItems[i].className = "w-on"; // give the proper li a class, i.e. turn it on
                classoff = listItems[i]; // fill the variable so we know what to turn off the next time
            }
        
        } // for loop
        
        divid = anchor + "-show"; 
        
        document.getElementById(divhide).style.display = "none"; // turn the div that used to be on, off
        document.getElementById(divid).style.display = "block"; // turn the right div on
        
        divhide = divid; // fill the variable so it hides this div once people start clicking
    
    }

} // end function init()



    
function switchit(e) {

    var clicked;
    
    // the following 3 if statements deal with the differences in how browsers deal with having events assigned programmatically
    // and how to reference the target of what has been clicked 
    
    
    // IE doesn't know the "e" variable for the event, it uses window.event
    if (!e) var e = window.event; // this is for IE only
    
    if (e.target) {
        clicked = e.target;
    }
    else if (e.srcElement) {
        clicked = e.srcElement;
    }
    if (clicked.nodeType == 3) {// defeat Safari bug
        clicked = clicked.parentNode;
    }
     
    href= clicked.href; // the href of the link that was clicked
       
    hashpos = href.lastIndexOf('#')+1; // get position of the start of just the anchor, minus the hash mark
    
    
    href = href.slice(hashpos,href.length); // put the anchor into variable
    
    
    // this checks for a nav item arrow to turn off. if there is one, it turns that nav item off. if not, it just turns the first one off
    
    if (classoff != "") {
        classoff.className = "";
    }
    else {
        listItems[0].className = "";
    }
    
    clicked.parentNode.className = "w-on"; // turn the proper nav item arrow on
    classoff = clicked.parentNode; // capture this to turn it off next time arround
    
    location.href="#" + href; // change the location field for bookmarks and jank
    
    divshow = href + "-show"; // the actual div we want to show
    
    if (document.getElementById(divshow)!=null) { // if div to show actually exists        
    
        if (divhide != "") {
        // hide the last one
            document.getElementById(divhide).style.display = "none";
        }
        
      
        document.getElementById(divshow).style.display = "block";  // show the one clicked upon
        
        divhide = divshow; // set the one clicked upon to the one to hide next time
    
    }
    else {
    
        alert("We're sorry, but that tool could not be found. Please try again.");
    }
    
    return false;
}
   
  
window.onload=init; 