function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function changeNextPrev(activeLink){
	var nav = document.getElementById("slides");
	var listItems = nav.getElementsByTagName("li");
	for (var h=0; h<listItems.length; h++){
		if (listItems[h].className.indexOf("prev") == -1){
			if (listItems[h].className.indexOf("next") == -1) continue;
			var nextLink = listItems[h].getElementsByTagName("a");
			var thenext = parseFloat(activeLink) + 1;
			if (thenext != 10){
				var theLink = nextLink[0];
				nextLink[0].setAttribute("href", "#slide" + thenext);
				nextLink[0].destination = nextLink[0].getAttribute("href").split("#")[1];
				nextLink[0].onclick = function(){
					showSection(nextLink[0].destination, theLink);
				    return false;
				}
			}
			else{
				var theLink = nextLink[0];
				nextLink[0].setAttribute("href", "#slide1");
				nextLink[0].destination = nextLink[0].getAttribute("href").split("#")[1];
				nextLink[0].onclick = function(){
					showSection(nextLink[0].destination, theLink);
				    return false;
				}
			}
		}
		else{
			var prevLink = listItems[h].getElementsByTagName("a");
			var theprev = activeLink - 1;
			var theLink = prevLink[0];
			if (theprev != 0){
				prevLink[0].setAttribute("href", "#slide" + theprev);	 
				prevLink[0].destination = prevLink[0].getAttribute("href").split("#")[1];
				prevLink[0].onclick = function(){
					showSection(prevLink[0].destination, theLink);
				    return false;
				}
			}
			else{
				prevLink[0].setAttribute("href", "#slide9");	 
				prevLink[0].destination = prevLink[0].getAttribute("href").split("#")[1];
				prevLink[0].onclick = function(){
					showSection(prevLink[0].destination, theLink);
				    return false;
				}
			}
		}
	}
}
function showSection(id, theLink) {
  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].style.display="none";
    } 
    else {
     divs[i].style.display="block";
    }
  }
  var theContainer = document.getElementById("slides");
  var theLinks = theContainer.getElementsByTagName("a");
  for (var j=0; j<theLinks.length; j++){
  	if (theLinks[j].getAttribute("href").split("#")[1] == id){
  		theLinks[j].className="active";
  		var theNumber = theLinks[j].getAttribute("href").split("#slide")[1];
  		changeNextPrev(theNumber);
  	}
  	else{
  		theLinks[j].className=" ";
  	}
  }
}

function prepareInternalnav() {
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById("slides")) return false;
  var nav = document.getElementById("slides");
  var links = nav.getElementsByTagName("a");
  for (var i=0; i<links.length; i++ ) {
    var sectionId = links[i].getAttribute("href").split("#")[1];
    var theLink = links[i];
    if (!document.getElementById(sectionId)) continue;
    if (sectionId != "slide1"){
    document.getElementById(sectionId).style.display = "none";
    }
    else{
    links[i].className="active";
    var theNumber = theLink.getAttribute("href").split("#slide")[1];
    changeNextPrev(theNumber);
   	}
    links[i].destination = sectionId;
    links[i].onclick = function() {
      showSection(this.destination, theLink);
      return false;
    }
  }
}

addLoadEvent(prepareInternalnav);