/* -------------------------------------------
For controlling page onload and unload events.
addLoadEvent fires in sequential order.
addEvent fires in random non-sequential order in IE.
useage: addLoadEvent(nameOfFunctionToRun);
---------------------------------------------- */

function addLoadEvent(func) { 
	if(!func) return false;
	var oldonload = window.onload; 
	if (typeof window.onload != 'function') { 
		window.onload = func; 
	} else { 
		window.onload = function() { 
			oldonload(); 
			func(); 
		} 
	} 
} 
	 
function addUnloadEvent(func) { 
	if(!func) return false;
	var oldonunload = window.onunload; 
		if (typeof window.onunload != 'function') { 
		window.onunload = func; 
	} else { 
		window.onunload = function() { 
			oldonunload(); 
			func(); 
		} 
	} 
} 


/* ----------------------------------------------
For inserting a new element after an existing one
Written by Jeremy Keith
http://www.domscripting.com/
------------------------------------------------- */

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


/* -----------------------------------------------------------
For controlling display values of alternating table rows and
optionally highlighting rows with mouseover.

Original code by Jeremy Keith
http://www.domscripting.com/

Revised by Philip Hutchison

Requires CSS classes "tr.odd" and "tr.highlight"
Requires helper function addClass

Add following code to <head> of document containing table
addLoadEvent(stripeAndHighlightTableRows);
-----------------------------------------------------*/

function stripeAndHighlightTableRows(){
	if(!document.getElementsByTagName) return false;
	var tbodies = document.getElementsByTagName("tbody");
	for (var i=0, tbody; tbody = tbodies[i]; i++) {
		var odd = true,
			rows = tbody.getElementsByTagName("tr");
		for (var j=0, row; row = rows[j]; j++) {
			//Add striping
			if(odd == false) {
				odd = true;
			} else {
				addClass(row, "odd");
				odd = false;
			}
			//Add highlights
			row.oldClassName = row.className
			row.onmouseover = function() {
				addClass(this, "highlight");
			}
			row.onmouseout = function() {
				this.className = this.oldClassName;
			}
		}
	}
}

function addClass(element, value){
	if(!element || !value) return false;
	if(!element.className){
		element.className = value;
	} else {
		element.className += " " +value;
	}
}

function removeClass(element, value){
	if(!element || !value) return false;
	if(element.className.indexOf(" " +value) != -1){
		value = " " +value;
	}
	element.className = element.className.replace(new RegExp( value, "gi" ), "");
}


/* ------------------------------------------

For easily accessing elements by class name

Written by Dustin Diaz
http://www.dustindiaz.com/getelementsbyclass/

Modified by Philip Hutchison

Sample useage: 
getElementsByClass('foo', '*', document);
'node' and 'tag' are optional parameters

var x = getElementsByClass('myClassName');
var x = getElementsByClass('myClassName', 'li');

var myEls = getElementsByClass('myClass');
for ( i=0;i<myEls.length;i++ ) {
	// do stuff here with myEls[i]
}

------------------------------------------ */

function getElementsByClass(searchClass, tag, node) {
	
	if(!searchClass || !document.getElementsByTagName) return false;
	
	if (node == null){ node = document; }
	if (tag == null){  tag = '*'; }
	
	var classElements = new Array(),
		els = node.getElementsByTagName(tag),
		elsLen = els.length,
		pattern = new RegExp("(^|\\s)" +searchClass +"(\\s|$)");
		
	for (var i = 0, j = 0; el = elsLen[i]; i++) {
		if (pattern.test(el.className)) {
			classElements[j] = el;
			j++;
		}
	}
	return classElements;
}

//Stripe table rows on all pages with tables
addLoadEvent(stripeAndHighlightTableRows);


//Google Analytics
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1394306-2']);
_gaq.push(['_trackPageview']);
(function() {
	var ga = document.createElement('script');
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	ga.setAttribute('async', 'true');
	document.documentElement.firstChild.appendChild(ga);
})();