/* Scripts by JEREMY KEITH and SIMON WILLISON
	http://www.adactio.com
	http://simon.incutio.com
*/

function insertAfter(newElement, targetElement){
	var parent = targetElement.parentNode;
	if(parent.lastChild == targetElement){
		parent.appendChild(newElement);
	}else{
		parent.insertBefore(newElement, targetElement.nextSibling);
	}
}

function addLoadEvent(func){
	var oldonload = window.onload;
	if(typeof window.onload != 'function'){
		window.onload = func;
	}else{
		window.onload = function(){
			oldonload();
			func();
		}
	}
}

/* end scripts by JK and SW */


/*=========================================*/

/* Custom Scripts for wwww.jervo.com
	written by Dan Mall
		http://www.danielmall.com */
		
/*------------------------------------
	SYLLABUS PAGES
------------------------------------*/
function hideHomework(){
	if(!document.getElementById){return;}
	if(!document.getElementsByTagName){return;};

	var hw = document.getElementsByTagName('div');
	
	for(i=0; i<hw.length; i++){
		if(hw[i].className == 'shown'){
			hw[i].className = 'hidden';
		}
	}
}

function findToggles(){
	if(!document.getElementsByTagName){return;}

	var anchors = document.getElementsByTagName('a');

	for(j=0; j<anchors.length; j++){
		if(anchors[j].className == 'closeMe'){
			//initial state is open, so close the hw
			anchors[j].className = 'openMe';
			
			//onclick handler
			anchors[j].onclick = function(){
				var myParent = this.parentNode;
				var hiddenHW = myParent.getElementsByTagName('div');
				
				if(hiddenHW[0].className == 'hidden'){
					hiddenHW[0].className = 'shown';
					this.className = 'closeMe';
				}else{
					hiddenHW[0].className = 'hidden';
					this.className = 'openMe';
				}
			}
		}
	}
}

/*------------------------------------
	SECTION PAGES
------------------------------------*/

function hideTables(){
	if(!document.getElementById){return;}
	if(!document.getElementsByTagName){return;}
	
	if(document.body.className.indexOf('section') == -1){
		return;
	}
	
	var tables = document.getElementsByTagName('table');
	for(var j = 0; j < tables.length; j++){
		tables[j].style.display = 'none';
	}
	
	var links = document.getElementsByTagName('a');
	
	for(var i = 0; i < links.length; i++){
		if(links[i].className == 'toggle'){
			links[i].className = 'openMe';
			links[i].onclick = function(){
				if(this.className == 'openMe'){
					this.className = 'closeMe';
					this.parentNode.getElementsByTagName('table')[0].style.display = 'block';
				}else{
					this.className = 'openMe';
					this.parentNode.getElementsByTagName('table')[0].style.display = 'none';
				}
			}
		}
	}
}

addLoadEvent(hideHomework);
addLoadEvent(findToggles);
addLoadEvent(hideTables);