

/* =================================================================== */
// function to add events crossbrowser
// note: probably needs a removeEvent function at some point
function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
addEvent(window,'unload',EventCache.flush);
/* =================================================================== */
/* =================================================================== */
// various link functionality - popups, external, onstate
var anchors = {
	a : Object,
	doPopups : function() {
		if (!document.getElementsByTagName) return false;
		var links = document.getElementsByTagName("a");
		for (var i=0; i < links.length; i++) {
			var anchor = links[i];
			if (anchor.getAttribute('href') && anchor.rel.match('external')) {
				anchor.target = '_blank';
				cssjs('add',anchor,'external');
			}
		    if (anchor.className.match("popup")) {
				anchor.onclick = function() {
		        	window.open(this.href, "newwin","toolbar=no");
		        	return false;
				}
		    }
			if (anchor.className.match("popupFull")) {
				anchor.onclick = function() {
		        	window.open(this.href, "newwin","");
		        	return false;
				}
		    }
			if (anchor.className.match("file")) {
				anchor.onclick = function() {
		        	window.open(this.href, "newwin","");
		        	return false;
				}
		    }
			if (anchor.href == location.href) anchor.className += " onstate";
		}
	}
}
addEvent(window,'load',anchors.doPopups);
/* =================================================================== */
/* =================================================================== */
// get, set, and delete cookies
function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+"="+escape( value ) +
		( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

/* =================================================================== */
/* =================================================================== */
function toggle(obj) {
	if (obj.nodeType != 1) {
		var el = document.getElementById(obj);
	} else {
		var el = obj;
	}
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	}

}
/* =================================================================== */
/* =================================================================== */
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;
}
/* =================================================================== */
/* =================================================================== */
function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}
/* =================================================================== */
/* =================================================================== */
function get_random(topLim)
	{
	    var ranNum= Math.round(Math.random()*topLim);
	    return ranNum;
	}
/* =================================================================== */
/* =================================================================== */
//call like cssjs('add',containerOBJ,classname);
function cssjs(a,o,c1,c2){
		switch (a){
			case 'swap':
				o.className=!cssjs('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
			break;
			case 'add':
				if(!cssjs('check',o,c1)){o.className+=o.className?' '+c1:c1;}
			break;
			case 'remove':
				var rep=o.className.match(' '+c1)?' '+c1:c1;
				o.className=o.className.replace(rep,'');
			break;
			case 'check':
				return new RegExp("(^|\\s)" + c1 + "(\\s|$)").test(o.className)
			break;
		}
	}

/* =================================================================== */



/* =================================================================== */
// use to remove whitespace from the DOM, because whitespace shows as elements within Gecko
function cleanOutWhitespace(theElement)
{
	// Create an array which will hold the needed HTML elements
	var storeHTMLElement = new Array();
	var arrayCounter = 0;
	for (i = 0; i < theElement.childNodes.length; i++)
	{
		// Store HTML elements in the array
		if (theElement.childNodes[i].nodeType == 1) {
			storeHTMLElement[arrayCounter] = theElement.childNodes[i];
			arrayCounter = arrayCounter + 1;
		}
	}
	
	// Remove all child nodes
	// Set a variable to the length, because the length changes every time a node is removed
	var elementLength = theElement.childNodes.length;
	for (i = 0; i < elementLength; i++)
	{
		theElement.removeChild(theElement.childNodes[0]);
	}
	
	// Append HTML elements back onto the parent element
	for (i = 0; i < storeHTMLElement.length; i++)
	{
		theElement.appendChild(storeHTMLElement[i]);
	}
	
	return theElement;
}

/* =================================================================== */