var dom = {
	init : function () {
		document.getElementsByClassName = this.getElementsByClassName;
		HTMLElement.prototype.getElementsByClassName = this.getElementsByClassName;
		HTMLElement.prototype.setAttributes          = this.setAttributes;	
		HTMLElement.prototype.setStyles              = this.setStyles;	
		HTMLElement.prototype.create                 = this.create;	
		HTMLElement.prototype.addClass               = this.addClass;	
		HTMLElement.prototype.removeClass            = this.removeClass;
		window.$                                     = this.$;
	},
	addLoadEvent : function (func) {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {	
			window.onload = func;
		}
		else {
			window.onload = function() {
				oldonload();
				func();
			}
		}
	},
	getElement : function (element) {
		if (element.indexOf(".") == 0) {
			this.elements               = document.getElementsByTagName("body")[0].getElementsByClassName(element.replace(/./,""));
			this.elements.setStyles     = this.setStyles;
			this.elements.setAttributes = this.setAttributes;
			this.elements.create        = this.create;
			this.addAnimate(this.elements);
			return this.elements;
		}
		else if (element.indexOf("#") == 0) {
			this.element       = document.getElementById(element.replace(/#/,""));
			this.addAnimate(this.element);
			return this.element;
		}
		else {
			this.element       = document.getElementById(element.replace(/#/,""));
			this.addAnimate(this.element);
			return this.element;
		}
	},
	$ : function (element) {
		return dom.getElement(element);
	},
	getElementsByClassName : function (className) {
		this.elements    = this.getElementsByTagName("*");
		this.elementList = new Array();
		
		for (var i=0; i<this.elements.length; i++) {
			this.classes = this.elements[i].getAttribute("class") ? this.elements[i].getAttribute("class").split(" ") : null;
			if (this.classes) {
				for (this.j=0; this.j<this.classes.length; this.j++) {
					if (this.classes[this.j] == className) {
						this.elementList[this.elementList.length] = this.elements[i];
					}
				}
			}
		}
		return this.elementList;
	},
	addClass : function (className) {
		this.classes = this.getAttribute("class") ? this.getAttribute("class").split(" ") : null;
		if (this.classes) {
			for (var i=0; i<this.classes.length; i++) {
				if (this.classes[i] == className) {
					return false;
				}
			}
		}
		
		this.className += " "+className;
		return true;
	},
	removeClass : function (className) {
		this.classes    = this.getAttribute("class") ? this.getAttribute("class").split(" ") : null;
		this.newClasses = null;
		if (this.classes) {
			for (var i=0; i<this.classes.length; i++) {
				if (this.classes[i] != className) {
					this.newClasses = this.newClasses ? this.newClasses+" "+this.classes[i] : this.classes[i];
				}
			}
		}
		
		this.className = this.newClasses;
		return true;
	},
	addAnimate : function (element) {
		if (element) {
			if (element.nodeType) {
				element.setOpacity  = animate.setOpacity;
				element.setWidth    = animate.setWidth;
				element.setHeight    = animate.setHeight;
				element.setPosition = animate.setPosition;
				element.move  = animate.move;
				element.scale = animate.scale;
				element.fade  = animate.fade;
			}
			else {
				for (var i=0; i<element.length; i++) {
					element.move  = function (newY, newX, time) {
						for (var i=0; i<this.length; i++) {
							dom.addAnimate(this[i]);
							this[i].move(newY, newX, time);
						}
					};
					element.scale = function (newWidth, newHeight, time) {
						for (var i=0; i<this.length; i++) {
							dom.addAnimate(this[i]);
							this[i].scale(newWidth, newHeight, time);
						}
					};
					element.fade  = function (newOpacity, time) {
						for (var i=0; i<this.length; i++) {
							dom.addAnimate(this[i]);
							this[i].fade(newOpacity, time);
						}
					};
				}
			}
		}
	},
	createText : function (text) {
		return document.createTextNode(text);
	},
	create : function (element, attributes, innerText, styles) {
		//create element
		this.element = document.createElement(element);
		
		//set attributes
		this.element.setAttributes(attributes);
		
		//set styles
		this.element.setStyles(styles);
		
		//set inner text
		if (innerText) {
			if (innerText.nodeType) {
				this.element.appendChild(innerText);
			}
			else {
				this.element.innerHTML = innerText;
			}
		}
		
		if (this.nodeType || this.length) {
			if (this.length) {
				for (var i=0; i<this.length; i++) {
					this[i].appendChild(this.element.cloneNode("true"));
				}
			}
			else {
				this.appendChild(this.element);
			}
		}
		else {
			return this.element;
		}
	},
	setAttributes : function (attributes) {
		if (this.length) {
			for (var i=0; i<this.length; i++) {
				for (var j in attributes) {
					if (attributes[j]) {
						if (j == "className") {
							this[i].setAttribute("class",attributes[j]);
						}
						else {
							this[i].setAttribute(j,attributes[j]);
						}
					}
				}
			}
		}
		else {
			for (var i in attributes) {
				if (attributes[i]) {
					if (i == "className") {
						this.setAttribute("class",attributes[i]);
					}
					else {
						this.setAttribute(i,attributes[i]);
					}
				}
			}
		}
	},
	setStyles : function (styles) {
		if (this.length) {
			for (var i=0; i<this.length; i++) {
				for (j in styles) {
					eval("this["+i+"].style."+j+"   = styles[\""+j+"\"]");
				}
			}
		}
		else {
			for (var i in styles) {
				eval("this.style."+i+"   = styles[\""+i+"\"]");
			}
		}
	}
};
dom.init();