//////////////////////////////////////////////////////////////////////////////////////////
// internalNav script
// Author: Alec Hill
//////////////////////////////////////////////////////////////////////////////////////////

// DESCRIPTION
	//instead of large blocks of text and internal links jumping through them, this hides all the sections (must be divs with class="section" and an id), leaving only a list of internal links. If javascript is off then will degrade by simply jumping through longer page

//////////////////////////////////////////////////////////////////////////////////

//function to allow more than one window.onload event
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
//function to show section when selected from list
function showSection(sectionToShow){
	var divs = document.getElementsByTagName("div");
	for(var i=0; i<divs.length; i++){
		var sectionClass = divs[i].className;
		var id = divs[i].getAttribute("id");
		if (sectionClass == "section") {
			divs[i].style.display = "none";
			if (id == sectionToShow){
				divs[i].style.display = "block";
			}
		}
	}
}
//function to set up the internal nav, by hiding each and by assigning onclick events to call showSection
function setupInternalNav(){
	if(!document.getElementById) return false;
	if(!document.getElementsByTagName) return false;
	if(!document.getElementById("internalnav")) return false;
	var internalNav = document.getElementById("internalnav");
	var links = internalNav.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).style.display="none";
		links[i].destination = sectionId;
		links[i].onclick = function(){
			showSection(this.destination);
			return false;
		}
	}
}
//add initiateInternalNav to window.onload event
addLoadEvent(setupInternalNav);

