/* *****************************************************************************
Contains common Javascript functions.
***************************************************************************** */

/* ***************************************************************************
Add event function
***************************************************************************** */
function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
	} else if (elm.attachEvent) {
		r = elm.attachEvent('on' + evType, fn);
		return r;
	} else {
		elm['on' + evType] = fn;
	}
}

/* ***************************************************************************
Add event listeners to certain named UL's
***************************************************************************** */
function addListener(objectId) {
	if (!document.getElementsByTagName) return;
	if (!document.getElementById) return;
	
	var obj = document.getElementById(objectId);
	if (obj==null) return;
	var objPs = obj.getElementsByTagName("p");
		for (var p=0; p< objPs.length; p++) {
		objPs[p].style.display = "none";
		}
	var objLIs = obj.getElementsByTagName("li");
		for(var l=0; l< objLIs.length; l++) {
			var getA = objLIs[l].getElementsByTagName("a");
			for (a=0; a<getA.length; a++) {
				addEvent(getA[a], 'click', openClose, false);
				getA[a].title = "Click to view contact information";
			}
		}
}

/* ***************************************************************************
Function that opens and closes div's
***************************************************************************** */
function openClose(e) {
	if (e && e.target) {
		zlink = e.target;
	}
	if (window.event && window.event.srcElement) {
		zlink = window.event.srcElement;
	}
	if (!zlink) return;
	
	// find the parent LI of the link
	theLiPar = zlink;
	while (theLiPar.nodeName.toLowerCase() != "li" && theLiPar.nodeName.toLowerCase() != 'html'){
		theLiPar = theLiPar.parentNode;
	}
	if(theLiPar == null) return;
	
	if (theLiPar.childNodes) {
		for (n=0; n<theLiPar.childNodes.length; n++) {
			if(theLiPar.childNodes[n].nodeName.toLowerCase() == 'p') {
				var p = theLiPar.childNodes[n];
				p.style.display = (p.style.display == "none") ? "block" : "none";	
			}
		}
	}
	// removed the dotted lines from the link
	zlink.blur();
	
	if (zlink.rel != "external") {
		// keep links from working if they're not external site links
		if (window.event) {
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		} else if (e) {
			e.stopPropagation();
			e.preventDefault();
		}
	}
}

/* *****************************************************************************
Loads links in a new window, either a full browser or a sized popup.
Usage:
<a href="..." rel="external">
<a href="..." rel="popup|200|300"> (200 is width, 300 is height)
***************************************************************************** */
function windowLinks()
{
	if(!document.getElementsByTagName){ return; }
	
	var anchors = document.getElementsByTagName("a");
	for(var i = 0; i < anchors.length; i++)
	{ 
		var anchor = anchors[i];
		var relIndex = anchor.rel;
		if(relIndex)
		{
			var relSplit = relIndex.split("|");          
			if(relSplit[0] == "external")
			{
				anchor.target = "_blank";
				anchor.title = "Loads in new window: "+ anchor.href;  
			}
			else if(relSplit[0] == "popup")
			{
				anchor.title = "Loads in a Popup Window";
				anchor.popupWidth = relSplit[1];
				anchor.popupHeight = relSplit[2];
				anchor.onclick = function(){ winPop(this.href, 'console', this.popupWidth, this.popupHeight); return false; };
			}
			else if(relSplit[0] == "backOne")
			{
				anchor.onclick = function() { window.history.back(); return false };
			}
		}
	}
}
/* ************************************************************************** */


/* *****************************************************************************
Opens new windows
***************************************************************************** */
function winPop(strURL, strType, strWidth, strHeight)
{
	var strOptions = "";
	if(strType == "console"){ strOptions = "scrollbars=yes,resizable,height=" + strHeight + ",width=" + strWidth; }
	window.open(strURL, '', strOptions);
}
/* ************************************************************************** */

/* *****************************************************************************
Simon Willison's addLoadEvent function allows you to stack up 'window.onload' events 
without them stepping on each other's toes. 
It's explained here - http://www.sitepoint.com/blog-post-view.php?id=171578
***************************************************************************** */
function addLoadEvent(func)
{
	var oldonload = window.onload;
	if(typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else
	{
		window.onload = function()
		{
			oldonload();
			func();
		}
	}
}
/* ************************************************************************** */


/* *****************************************************************************
// Initialize list of onload functions
/* ************************************************************************** */
addLoadEvent(function() { 
	windowLinks();
	addListener('ulIndyParts');
	addListener('ulParts');
});

/* ************************************************************************** */

