function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

// Drop down help for IE
startList = function() {
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("nav");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
	}
}


// Hidden sections
function showSection(id) {
	var divs = document.getElementsByTagName("div")
	for (var i=0; i<divs.length; i++) {
		if (divs[i].className.indexOf("section") == -1) continue;
		if (divs[i].getAttribute("id") != id) {
			divs[i].className = "hidesection";
			//divs[i].style.display = "none";
		} else if (divs[i].className != "hidesection") {
		//} else if (divs[i].style.display != "none") {
			divs[i].className = "hidesection";
			//divs[i].style.display = "none";
		} else {
			divs[i].className = "section showsection";
			//divs[i].style.display = "block";
		}
	}
}


function prepareInternalnav() {
	if(!document.getElementsByTagName) return false;
	if(!document.getElementById) return false;
	if(!document.getElementById("hiddensections")) return false;
	var nav = document.getElementById("hiddensections");
	var links = nav.getElementsByTagName("a");
	for (var i=0; i<links.length; i++) {
		var sectionId = links[i].getAttribute("href").split("#")[1];
		if (!document.getElementById(sectionId)) continue;
		document.getElementById(sectionId).className = "hidesection";
		//document.getElementById(sectionId).style.display = "none";
		links[i].destination = sectionId;
		links[i].onclick = function() {
			showSection(this.destination);
			return false;
		}
	}
}



// SecondaryNav .here
function extractPageName(hrefString)
{
	var arr = hrefString.split('/');
	return  (arr.length < 2) ? hrefString : arr[arr.length-2].toLowerCase() + arr[arr.length-1].toLowerCase();
}
 
function setActiveMenu(arr, crtPage)
{
	for (var i=0; i < arr.length; i++)
	{
		if(extractPageName(arr[i].href) == crtPage)
		{
			if (arr[i].parentNode.tagName != "DIV")
			{
				arr[i].className = "here";
				arr[i].parentNode.className = "here";
			}
		}
	}
}
 
function setPage()
{
	hrefString = document.location.href ? document.location.href : document.location;
 
	if (document.getElementById("secondarylinks") !=null )
	setActiveMenu(document.getElementById("secondarylinks").getElementsByTagName("a"), extractPageName(hrefString));
}


addLoadEvent(startList);
addLoadEvent(prepareInternalnav);
addLoadEvent(setPage);
