/*
 * Used to Load Flash on home page
 * Randomly chooses from 1 of x flash files to display
 */
function writeFlash(){	
	items = 5; //Set the total number of flash files to pick from
	rand = 1 + Math.floor(Math.random() * items);
	
	flashSrc = "/assets/flash/whittier"+ rand +".swf";
	flashWidth = 232;
	flashHeight = 288;
	document.write( '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" height="' + flashHeight + '" width="' + flashWidth + '" align="top"><param name="movie" value="' + flashSrc + '"><param name="quality" value="best"><param name="play" value="true"><embed align="top" height="' + flashHeight + '" pluginspage="http://www.macromedia.com/go/getflashplayer" src="' + flashSrc + '" type="application/x-shockwave-flash" width="' + flashWidth + '" quality="best" play="true"><\/embed><\/object>' );
}

/*
 * Code that will run on every page load
 */
addLoadEvent(function() {				  
	/* 
	 * Adds jsEnabled as a Class to the body,
	 * use this for CSS selectors that will only be used
	 * if Javascript is enabled 
	 */
	e = document.getElementsByTagName("body");
	for(i=0; i<e.length; i++) {
		addClass(e[i], "jsEnabled");
	}
	
	openOnClick("nav-search-link", true);
	openOnHover("nav-audience", false);
});

/*
 * Click item to toggle a class of "open"
 * Use CSS to actaully show/hide the content
 */
function openOnClick(id) {
	var el = document.getElementById(id);
	el2 = el.parentNode;
	el.onclick = function() {toggleClass(el2, "open"); return false;};
}

/*
 * Mouseover item to toggle between a class of "open" and "closed"
 * Use CSS to actaully show/hide the content
 */
function openOnHover(id, targetParent) {
	var el = document.getElementById(id);
	if(targetParent) el = el.parentNode;
	if(el) {
		el.className = "closed";
		el.onmouseover = function() {el.className = "open"; return false;};
		el.onmouseout = function() {el.className = "closed"; return false;};
	}
}

/*
 * Creates tabbed based content
 * Clicking a tab shows the tab's content and
 * hides the content of all other tabs
 *
 *	className - class name assigned to each "tab"
 *	node - (optional) specify a parent element to search in, default is document
 *	tag - (optional) only search specific tags
 */
function tabify(className, node, tag) {
 	var tabs = getElementsByClass(className, node, tag);
	for(var i=0;i<tabs.length;i++) {
		tabs[i].onclick = function() {
			var el = this;
			var className = el.parentNode.id;
			el.parentNode.parentNode.className = className;
		};
	}
}

/*
 * Assigns a class to a container on mouseover and mouseout
 * Used to replicate hover affects on groups of objects
 * Clicking any object in the container will load a new page based on the href of an a tag in the container
 *	className - all elements with the specified class will be affected
 *	node - (optional) specify a parent element to search in, default is document
 *	tag - (optional) only search specific tags
 */
function hoverBox(hoverClass, node, tag) {
	var els = getElementsByClass(hoverClass, node, tag);
	for(var i=0;i<els.length;i++) {
		els[i].onmouseover = function(){toggleClass(this, "hover");};
		els[i].onmouseout = function(){toggleClass(this, "hover");};
		els[i].onclick = function() {window.location = this.getElementsByTagName("a")[0].getAttribute("href");};
	}
}

/*
 * Assigns class of "odd" to alternating rows in a table
 * targetClass - target tables that have the provided class leave blank to target ALL tables on the page
 */
function stripeTable(targetClass) {
	var tables, rows;
	tables = (typeof targetClass == "undefined") ? document.getElementsByTagName("TABLE") : getElementsByClass(targetClass);
	for(var i=0; i<tables.length; i++) {
		rows = tables[i].getElementsByTagName("TR");
		for(var j=0; j<rows.length; j++) {
			if ((j & 1) == 0) addClass(rows[j], "odd");
		}
	}
}



/*====================================================
	Core Functions
====================================================*/	

/*
 * addLoadEvent function written by Simon Willison:
 * http://simonwillison.net/2004/May/26/addLoadEvent/
 * Replacement for window.onload which can cause issues with multiple functions being loaded at once overriding each other
 *
 * Use 
 *	 addLoadEvent(nameOfSomeFunctionToRunOnPageLoad);
 * or
 * 	addLoadEvent(function() {
 * 		more code to run on page load
 * 	});
*/
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

/*
 * Returns an array of all items that have the specified class
 *	searchClass - the class to search for
 *	node - (optional) specify a parent element to search in, default is document
 *	tag	- (optional) only search specific tags
 */
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null ) node = document;
	if ( tag == null ) tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

/*
 * Adds specified class to an element if it does not already exist
 *	el - element to target
 *	theClass - class name to add
 */
function addClass(el, theClass) {
	var curClass = el.className;
	var pattern = new RegExp("(^|\\s)"+theClass+"(\\s|$)");
	if (!pattern.test(curClass)) {
		el.className += " " + theClass;
		return true;
	}
	else return false;
}

/*
 * Removes specified class from an element if it exists
 *	el - element to target
 *	className - class name to remove
 */
function removeClass(el, theClass) {
	var curClass = el.className;
	var pattern = new RegExp("(^|\\s)"+theClass+"(\\s|$)");
	if (pattern.test(curClass)) {
		el.className = curClass.replace(pattern, "");
		return true;
	}
	else return false;
}

/*
 * Toggles specified class on an element
 *	el - element to target
 *	className - class name to add or remove
 */
function toggleClass(el, className) {
	var curClass = el.className;
	var pattern = new RegExp("(^|\\s)"+className+"(\\s|$)");	
	if ( pattern.test(curClass) )
		el.className = curClass.replace(pattern, "");
	else
		el.className += " " + className;
}

/*
 * Mimics the :hover css psuedo class for IE6 and below
 * which do not properly support it
 */
function addIEHover(id) {
	var el = document.getElementById(id);
	el.onmouseover = function(){toggleClass(this, "hover");};
	el.onmouseout = function(){toggleClass(this, "hover");};
}

// Fixes IE caching problem that can cause background images to flicker 
try {document.execCommand('BackgroundImageCache', false, true);} catch(e) {}