// stores the currently selected parent menu item
var lastSide = 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 setSideSel(o) {
    // test to make sure we have a valid object
    if (o != null) {
        // clear the style of the last selected item
        clearClass(lastSide);
        // set the style class
        o.setAttribute('class', 'leftMenuSelect');
        o.setAttribute('className', 'leftMenuSelect');
        // this item is now the currently selected item
        // this is used to clear the class if another item is selected
        lastSide = o;
    }
}

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 sidemenuhover(e,o) {

    if (o == null) {
        return;
    }
	if (e != null) {
		if (e.relatedTarget) {
			obj = e.relatedTarget;
		}
		else if (e.toElement) {
			obj = e.toElement;
		}
	}
       
    //debugger;
    if (o.tagName == 'A') {     
        o = o.parentNode;
    }
    if (o.tagName != 'LI') {
        return;
    }

    // clear the current selection
    clearClass(lastSide);
//    // hide the current sub menu
//    clearSubMenu(lastSubMenu);
    // set the new selection
    setSideSel(o);

}

function sidemenuout(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;
        }        
    }

    //debugger;
    if (o.tagName == 'A') {     
        o = o.parentNode;
    }
    if (o.tagName != 'LI') {
        return;
    }
    // clear the style of the selected item
    clearClass(lastSide);


    lastSide = sidesel;
    // if we are on a page that exists in the menu
    if (lastSide != null) {
        // pretend that we are hovering over it.
        // this causes the selection to set itself when there is no mouse activity

        sidemenuhover(null, lastSide);        
    }
}


function extractFilename(s) {
    // extracts just the file name from a string
    return s.substring(s.lastIndexOf('/') + 1);
}

// this variable will hold the menu item that is used to navigate to the current page
var sidesel = null;
// this variable will hold the sub menu item that is used to navigate to the current page
//var subsel = null;

// this runs on page load and is used to determine if there is a default selection in the menu.
function SideMenuLoad() {
    // 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 file = extractFilename(window.location.href).toLowerCase();
    // get all of the hyperlinks in the #menu
    var mid=document.getElementById('midNavigation');
	if(mid)
	{
		var anchors = mid.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) {
	
	 //        this checks to see if the link points to current page
			if (file == extractFilename(anchors[c].href).toLowerCase()) {                        
				setSideSel(anchors[c].parentNode);		
				sidesel = 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
		lastSide = sidesel;
	}
}
        

