/*
This javascript library is solely to allow our product to work on IE6, which as of January 2008, still has a 32% market share.
Unfortunatley, IE does not allow for protoypes off of Elements, and the workaround used for IE7 (seen here: http://www.it-base.ro/2007/07/30/elementprototype-in-internet-explorer/)
does not work for IE6. We're replacing these methods with functional definitions.
*/

//IE FIX
//if (!window.Element) Element = function() {};
//END IE FIX


/* clearChildren()
Correct way:
	Element.prototype.clearChildren = function() {
		while (this.firstChild != null) this.removeChild(this.firstChild);
	}
*/
//To accommodate shitty IE6:
function clearChildren(node) {
	while (node.firstChild != null) node.removeChild(node.firstChild);
}

/**
 * Sets 'window.location' with support for relataive path. TODO: At the time of writing, I'm under the impression that 
 * setting a relative path directly, as in "window.location='foo.html'" does not work, though this memory may be 
 * erroneous. If so, remove this function and rewrite calls without worry.
 */
function setLocation(newPath) {
	var absPath = window.location.pathname;
	if (newPath.charAt(0) == '/') window.location.pathname = newPath;
	else {
		var path = absPath.substring(0, absPath.lastIndexOf('/') + 1);
		window.location = path + newPath;
	}
}

/* ------------ CLASS MANIPULATION FUNCTIONS ---------------*/

//TODO: would love to simply add these, like clear children, to the 'Element' object, but IE is shit

/**
 * Safely adds a class to an element. Deals with duplicates, other classes. Expects that the class name contains no 
 * special characters. If new class contains white space, then it is broken up and each element is added.
 */
function _he_addClass(element, newClass) {
	if (newClass == null) return;
	//first, trim the newClass
	newClass = newClass.replace(/^\s+/, '').replace(/\s+$/, '');
	if (newClass.length == 0) return;
	if (newClass.match(/\s+/)) {
		var classes = newClass.split(/\s+/);
		for (var i = 0; i < classes.length; i++) _he_addClass(element, classes[i]);
		return;
	}
	var matcher = new RegExp("(^| )" + newClass + "( |$)");
	if (matcher.test(element.className)) return;//it's already got the indicated class
	if (element.className != null && element.className.length > 0)
		element.className = element.className + " " + newClass;
	else element.className = newClass;
}

function _he_removeClass(element, oldClass) {
	if (oldClass == null) return;
	//first, trim the newClass
	oldClass = oldClass.replace(/^\s+/, '').replace(/\s+$/, '');
	if (oldClass.length == 0) return;
	if (oldClass.match(/\s+/)) {
		var classes = oldClass.split(/\s+/);
		for (var i = 0; i < classes.length; i++) _he_removeClass(element, classes[i]);
		return;
	}
	var matcher = new RegExp("(^| )" + oldClass + "( |$)");
	element.className = element.className.replace(matcher, '');
}

function _he_swapClass(element, oldClass, newClass) {
	_he_removeClass(element, oldClass);
	_he_addClass(element, newClass);
}

function _he_resetClass(element, newClass) {
	element.className = newClass;
}

//naming the var 'className' causes IE to choke
function _he_hasClass(element, className) {//true if it has all the classes
	if (className == null) return element.className == null || element.className.match(/^\s*$/);
	//first, trim the className
	className = className.replace(/^\s+/, '').replace(/\s+$/, '');
	if (className.length == 0) return element.className == null || element.className.match(/^\s*$/) == 0;
	else if (element.className == null || element.className.match(/^\s*$/) == 0) return false;
	if (className.match(/\s+/)) {
		var classes = className.split(/\s+/);
		for (var i = 0; i < classes.length; i++) 
			if (!_he_hasClass(element, classes[i])) return false;
		return true;
	}
	var matcher = new RegExp("(^| )" + className + "( |$)");
	return element.className.match(matcher);
}

/* ------------ END CLASS MANIPULATION FUNCTIONS ---------------*/
