// Here is a shortcut to document.getElementById.  Pass it one object, pass it many.
// Pass it a string with an object's ID.  Pass it the object itself.
// Pass it some combination of the above.  If you pass it one thing, it returns one element.
// If you pass it many things, it returns an array of elements.
// This function comes from http://www.dustindiaz.com/top-ten-javascript/
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;
}

//  IE has a uniqueID function built in, Mozilla does not. Here is a cross-browser implementation.
//  If the object given already HAS an ID, then the object is left unchanged and the id is returned.
//  If the object given does not have an ID, then this function assigns a unique ID to the object and
//  returns the new ID as a string.
//  This is useful if, for instance, you want to keep track of a parentNode or childNode of a known element.
function fnUniqueID(o) {
	if (($(o)) && (o.id)) { return o.id; }
	var strNewID;
	var intNewCode;
	var intCounter;
	strNewID = "x";
	do {
		for (intCounter = 1; intCounter <= 10; intCounter++) {
			intNewCode = "A".charCodeAt(0) + ("Z".charCodeAt(0) - "A".charCodeAt(0)) * Math.random()
			strNewID += String.fromCharCode(intNewCode);
		}
	} while (document.getElementById(strNewID) != null);
	try { o.id = strNewID; } catch(NoID) { throw "Attempted to get ID of an invalid node type:\n" + o; }
	return strNewID;
}

// Does what it says.  The node and tag arguments are optional, and default to searching the whole
// document.  On a huge document, that could take some time, so it's best to limit it as much as possible.
// This function comes from http://www.dustindiaz.com/top-ten-javascript/
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 ((els[i].className) && ( pattern.test(els[i].className) )) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

// Use this to add a function to the OnLoad event without having to edit the OnLoad event.
// This function comes from http://www.dustindiaz.com/top-ten-javascript/
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

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);
