/*	Largly borrowed from http://www.elektronaut.no/articles/2006/02/21/getelementsbyclassname
	but rewritten the way I like it, and also includes an IE behaviors hack I wrote and two
	additional functions: getClass() and setClass().
	Otherwise written David Beveridge.  All code is free for personal or commercial use, but
	I'm never opposed to free money, so if you feel like donating, paypal is davidjbeveridge@mail.com
	If you have any suggestions for code, please add it to the appropriate blog, or post it on a forum
	and send me the link rather than emailing submissions.
*/
var $ = function(element)	{
	e = ((typeof element).toLowerCase() == "string") ? document.getElementById(element) : (((typeof element).toLowerCase() == "object") ? element : undefined);
	return e;
}

if(!Array.prototype.push)	{
	Array.prototype.push = function(e)	{
		this[this.length] = e;
	}
}

document.hasClass = function(className, rootElement)	{
	var e = (rootElement) ? rootElement : this;
	if(className != "*")
		return ( document.getClass(e) && document.getClass(e).match( new RegExp("\\b"+className+"\\b") ) ) ? true : false;
	else
		return document.getClass(e) ? true : false;
}

document.getClass = function(rootElement)	{
	rootElement = (rootElement) ? rootElement : document.body;
	return (rootElement.className).toString();
}

document.setClass = function(className, rootElement)	{
	rootElement = (rootElement) ? rootElement : document.body;
	rootElement.className = className;
	return rootElement.className;
}

document.getElementsByClassName = function(className, tagName, rootElement)	{
	var elements = new Array();
	var tName = (tagName) ? tagName : "*";
	var root = (rootElement) ? rootElement : document.getElementsByTagName("body")[0];
	var children = root.getElementsByTagName(tName.toLowerCase());
	for(i = 0; i < children.length; i++)	{
		if(document.hasClass(className, children[i]))
			elements.push(children[i]);
	}
	return elements;
}

document.addClass = function(className, rootElement)	{
	rootElement = (rootElement) ? rootElement : document.body;
	if(!document.hasClass(className, rootElement))	{
		if(rootElement.className && rootElement.className.length > 0)
			className = rootElement.className+" "+className;
		document.setClass(className, rootElement);
		return document.getClass(rootElement);
	}
}

document.removeClass = function(className, rootElement)	{
	var REclassName = new RegExp("\\b" + className + "\\b");
	if(document.hasClass(className, rootElement))
		document.setClass(rootElement,(rootElement.className.replace(REclassName, "")));
	return document.getClass(rootElement);
}
