var navMenu = new function() {
	this.navigationID = "navMenu";  // Element ID of navigation menu.
	this.highlightClass = "current";// The CSS class of the current link location.
	this.parentClass = "parent";    // The CSS class of the current link location's parent element.
	this.setParentClass = false;     // Set the CSS class of the parent element.
	this.doNotSetAnchorClass = false; // Don't set the class for the A tag.
	this.getChildren = function(el){
		var children = new Array();
		for (var i=0; i < el.childNodes.length; i++){
			if (el.childNodes[i].nodeType == 1)
				children.push(el.childNodes[i]);
		}
		return children;
	}
	this.getChildElementsByTagName = function(el, tagName){
		var children = this.getChildren(el);
		var tagDescendents = Array();
		for (var i=0; i<children.length; i++) {
			if (children[i].tagName == tagName.toUpperCase())
				tagDescendents.push(children[i]);
			var tagGrandChildren = this.getChildElementsByTagName(children[i], tagName);
			tagDescendents = tagDescendents.concat(tagGrandChildren);
		}
		return tagDescendents;
	}
	// keep only UL tags, ignore nav root.
	this.getUlAncestors = function(el){
		var ancestors = new Array();
		while (el.id != this.navigationID){
			if (el.tagName == "UL")
				ancestors.push(el);
			el = el.parentNode;
		}
		return ancestors;
	}
	this.getUlChildren = function(el){
		var children = new Array();
		for (var i=0; i < el.childNodes.length; i++){
			if (el.childNodes[i].tagName == "UL")
				children.push(el.childNodes[i]);
		}
		return children;
	}
	this.displayCurrentLocation = function(a){
		// Show all ancestors
		var ancestors = this.getUlAncestors(a);
		for (var i=0; i < ancestors.length; i++)
			ancestors[i].style.display = 'block';
		
		// Give the parent element a CSS class.
		if (this.setParentClass) {
			if (ancestors.length > 0)
				ancestors[0].parentNode.className = this.parentClass;
			else
				a.parentNode.className = this.parentClass;
		}
		
		// Display first submenu of selected location, if it exists.
		var children = this.getUlChildren(a.parentNode);
		if (children.length > 0)
			children[0].style.display = 'block'; // show sub menu.
	}
	this.highlightLink = function(){
		var url = window.location.href.toLowerCase();
		url = url.replace(/#.*$|\?.*$/, "");
		var hostName = window.location.protocol + "//" + window.location.hostname + "/";
		var wasLinkFound = false;
		var links = this.getChildElementsByTagName(document.getElementById(this.navigationID), 'A'); // get all links
		while (!wasLinkFound && url != hostName)
		{
			for (var i=0; i < links.length; i++){
				if (links[i].href.toLowerCase() == url){
					if (!this.doNotSetAnchorClass)
						links[i].className = this.highlightClass; // highlight
					this.displayCurrentLocation(links[i]);
					wasLinkFound = true;
					break;
				}
			}
			// update link to point to parent
			if (!wasLinkFound)
				url = url.replace(/[A-Za-z0-9\-\(\)]+\.aspx|[A-Za-z0-9\-\(\)]+\/$/,"");
		}
	}
}
window.onload = function(){
	navMenu.navigationID = 'primarynav';
	navMenu.highlightClass = 'youAreHere';
	navMenu.setParentClass = true;
	navMenu.highlightLink();
}
