// JavaScript Document
function switchid(id){
	var elemStyle = document.getElementById(id).style;
	var elemDisplay = elemStyle.display;
	var showAlltxt = (document.getElementById('showAll')) ? document.getElementById('showAll').innerHTML : '';
	if (elemDisplay == 'none' || showAlltxt === 'Close All') {
		//console.info("Display: ", elemDisplay);
		var hash = id.substring(0, id.lastIndexOf('-'));
		if(getURLparam('pg') != hash) {
		   document.location.href = '?pg='+hash; //+'#'+hash;
		   return false;
		} else {
			setThisPgClass();
			return (hideallids() && showdiv(id)) ? false : true;			
		}
	} else {
		//console.info("Display: ", elemDisplay);
		//document.location.hash = '#top';
		return (hidediv(id)) ? false : true ;
	}
}

function hideallids(){
	//loop through the array and hide each element by id
	for (var i=0;i<ids.length;i++){hidediv(ids[i]);}
	if(document.getElementById('showAll')){document.getElementById('showAll').innerHTML = 'Show All';}
	return true;
}
function showallids(){
	var showAlltxt = (document.getElementById('showAll')) ? document.getElementById('showAll').innerHTML : '';
	//loop through the array and show each element by id
	if(showAlltxt == 'Show All') {
		for (var i=0;i<ids.length;i++){showdiv(ids[i]);}
		//showAlltxt = 'Close All';
		document.getElementById('showAll').innerHTML = 'Close All';
	} else {
		hideallids();
	}
}
function toggleId(id){
	var toggleState = document.getElementById(id).style.display;
	if(toggleState != null) {
		document.getElementById(id).style.display = (toggleState == 'none') ? showdiv(id) : hidediv(id);
	}
}

function hidediv(id) {
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6, Modern Browsers
		document.getElementById(id).style.display = 'none';
	} else {
		if (document.layers) {document.id.display = 'none';} // Netscape 4
		else {document.all.id.style.display = 'none';} // IE 4
	}
	return true;
}

function showdiv(id) {
	//safe function to show an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6, Modern Browsers
		document.getElementById(id).style.display = 'block';
	} else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
		} else { // IE 4
			document.all.id.style.display = 'block';
		}
	}
	return true;
}
function setThisPgClass() {
	var x = document.getElementsByTagName('a');
	var thispgClassName = 'thispg';
	var thispg = 'home.shtml'
	if(getURLparam('pg')) {
		thispg = document.location.href+'#'+getURLparam('pg');
	} else {
		thispg = document.location.href;
	}
	for (var i=0;i<x.length;i++) {
		if (x[i].getAttribute('href') && x[i].href == thispg) {
			x[i].className = thispgClassName;
		}
	}	
}
function getURLparam(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec(document.location.href);
  return (results == null) ? '' : results[1];
}
var pageLoaded = function() {
	// Add the "Show All Link"
	var showAllDiv = document.getElementById('showAll-div');
	if (showAllDiv) {
		showAllDiv.innerHTML = '<p class="link" onmousedown="showallids()" id="showAll">Show All</p>';
	}
	hideallids(); // Hide the answer divs
	var anchorLink = document.location.hash;
	var subPage = getURLparam('pg');
	if (subPage !== '') {
		targetDiv = subPage+'-div';
		switchid(targetDiv);
		//Load external scrolling JS to make sure the requested div is scrolled into view
		var fileref=document.createElement('script');
		//fileref.setAttribute("type","text/javascript");
		fileref.setAttribute("src","/acad/schdiv/scripts/scrollToPosition.js");
		document.getElementsByTagName("head")[0].appendChild(fileref);
		setTimeout("scrollToId(targetDiv)", 100);
	} else if (anchorLink !== ''){
		anchorLink = anchorLink.slice(1);
		targetDiv = anchorLink+'-div';
		showdiv(targetDiv);
		//Load external scrolling JS to make sure the requested div is scrolled into view
		var fileref=document.createElement('script');
		//fileref.setAttribute("type","text/javascript");
		fileref.setAttribute("src","/acad/schdiv/scripts/scrollToPosition.js");
		document.getElementsByTagName("head")[0].appendChild(fileref);
		setTimeout("scrollToId(targetDiv)", 100);
	} else {
		if(ids !== null) {
			//showdiv(ids[0]);
		}
	}
	//setTimeout("pageLoaded()", 1000);
};
function addLoadEvent(func) { // Source & Documentation: http://simonwillison.net/2004/May/26/addLoadEvent/
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(pageLoaded);
//window.onload = pageLoaded;