/*
	Javascript for Phil R. Harris
	Author:	David Correll 
			http://www.davidcorrell.net/
*/


/* 	

	*** COMMON JAVASCRIPT ***
	---------------------------------------------------------------------
	Below are common JS functions used throughout the site. Functions are as follows:
	
	addLoadEvent
			What it Does: Allows for multiple body.onload events.
			Usage: addLoadEvent(functionName) causes functionName to run on body.onload
			Requires: nothing
			
	insertAfter
			What is Does: inserts newElement after targetElement.
			Usage: insertAfter(newElement,targetElement);
			Requires: newElement and targetElement
			
	setNewWindow
			What it Does: Opens a link in a new window without the need for target="_blank".
			Usage: Simply add rel="external" to an anchor.
							ex: <a href="filename.php" rel="external">
			Requires: Support for document.getElementsByTagName. Runs onload.

*/


function addLoadEvent(func) {
	var oldOnLoad = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldOnLoad();
			func();
		}
	}
}


function insertAfter(newElement,targetElement) {
	var parent = targetElement.parentNode;
	if (parent.lastChild == targetElement) {
		parent.appendChild(newElement);
	} else {
		parent.insertBefore(newElement,targetElement.nextSibling);
	}
}


function setNewWindow() {
	// make sure the browser has what we need
	if (!document.getElementsByTagName) return false;
	// get the elements
	var links = document.getElementsByTagName("a");
	// make sure there are any
	if (links.length < 1) return false;
	for (a=0; a < links.length; a++) {
		var link = links[a];
		if ((link.getAttribute("href")) && (link.getAttribute("rel") == "external")) {
			link.onclick = function() {
				var location = this.getAttribute("href");
				window.open(location, "newWindow");				
				return false;
			}
		}
	}
}
addLoadEvent(setNewWindow);


// getElementsByClassName taken from http://www.robertnyman.com/index.php?p=256
function getElementsByClassName(className, tag, elm){
	var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}
