
// Usage: DOMElement(String(), [String(), Object(), String(), String(), Array()]);

function DOMElement(type, id, parent, css, text, events) {
	this.type	= type	 || "div";
	this.id		= id 	 || "DOMElement";
	this.parent = parent || document.getElementsByTagName("body")[0];
	this.css 	= css 	 || "";
	this.text	= text	 || "";
	this.events = events || []; // events = [{ type: String(), callback: Object(), bubbling: Boolean(), }];
	
	this.node = {};
	this.textNode = {};
	
	this.setIsIE();
}
DOMElement.prototype = {
	
	create: function() {
		this.node = this.parent.ownerDocument.createElement(this.type);
		this.textNode = this.parent.ownerDocument.createTextNode(this.text);
		this.node.setAttribute("id", this.id);
		
		if(this.isIE == true) this.node.style.cssText = this.css;
		else this.node.setAttribute("style", this.css);
	},
	
	append: function() {
		this.parent.appendChild(this.node);
		if(this.text != null && this.text != undefined && this.text != "") {
			this.node.appendChild(this.textNode);
		}
		this.addEvents();
	},
	
	remove: function() {
		this.removeEvents();
		this.parent.removeChild(this.node);
	},
	
	addEvents: function() {
		
		if(this.events[0] == null) return;		
		
		for(var i = 0; i < this.events.length; i++) {
			if(this.isIE == true) {
				this.node.attachEvent("on" + this.events[i].type, this.events[i].callback);
			}
			else {
				this.node.addEventListener(this.events[i].type, this.events[i].callback, this.events[i].bubbling);
			}
		}
	},
	
	removeEvents: function(events) {
		
		if(this.events[0] == null) return;
		
		for(var i = 0; i < this.events.length; i++) {
			if(this.isIE == true) {
				this.node.detachEvent("on" + this.events[i].type, this.events[i].callback);
			}
			else {
				this.node.removeEventListener(this.events[i].type, this.events[i].callback, this.events[i].bubbling);
			}
		}
	},
	
	setIsIE: function() {
		try { window.attachEvent(); this.isIE = true; }
		catch(e) { this.isIE = false; }
	}
};