// JavaScript Document - Script for showing and hiding content within page

// un-hides section
function showSection(id) {
  var divs = document.getElementsByTagName("div");
  for (var i=0; i<divs.length; i++ ) {
	
	var welcomeDiv = document.getElementById("welcome");						 
							 
    if (divs[i].className.indexOf("section") == -1) continue;
    if (divs[i].getAttribute("id") != id) {		
		divs[i].style.display = "none";
	  
	} else {
      divs[i].style.display = "block";
	  welcomeDiv.style.display = "none";
	 // alert("I should show: " + divs[i].getAttribute("id") );
	  
    }
  }
}


// gets all the main nav links and hides the reffered to sections
function prepareInternalnav() {
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById("internalnav")) return false;
  var nav = document.getElementById("internalnav");
  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).style.display = "none";
    links[i].destination = sectionId;
    links[i].onclick = function() {
      showSection(this.destination);
      return false;
    }
  }
}

// Creates show/hide nav for image map

function prepareMapnav() {
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById("Map")) return false;
  var mapnav = document.getElementById("Map");
  var maplinks = mapnav.getElementsByTagName("area");
  for (var i=0; i<maplinks.length; i++) {
	var sectionIdMap = maplinks[i].getAttribute("href").split("#")[1];
    if (!document.getElementById(sectionIdMap)) continue;
    document.getElementById(sectionIdMap).style.display = "none";
  	maplinks[i].destination = sectionIdMap;
	maplinks[i].onclick = function () {
		showSection(this.destination);
		return false;
	}
  }
}

// Simon Willison's add multiple function on page loads script
function addLoadEvent(func) {
  var oldonload = window.onload;

if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}


addLoadEvent(prepareInternalnav);
addLoadEvent(prepareMapnav);
