/*
	DOMAss: http://www.robertnyman.com/domass
	This module by Robert Nyman, http://www.robertnyman.com
*/
DOMAss.initContent = function (){
	this.addContentMethods();
};

DOMAss.addContentMethods = function (){
	if(typeof HTMLElement == "function"){
		HTMLElement.prototype.prev = DOMAss.prev;
		HTMLElement.prototype.next = DOMAss.next;
		HTMLElement.prototype.create = DOMAss.create;
		HTMLElement.prototype.setAttributes = DOMAss.setAttributes;
		HTMLElement.prototype.addContent = DOMAss.addContent;
		HTMLElement.prototype.replaceContent = DOMAss.replaceContent;
		HTMLElement.prototype.remove = DOMAss.remove;
	}
	this.methodsToAdd.push(["prev", this.prev]);
	this.methodsToAdd.push(["next", this.next]);
	this.methodsToAdd.push(["create", this.create]);
	this.methodsToAdd.push(["setAttributes", this.setAttributes]);
	this.methodsToAdd.push(["addContent", this.addContent]);
	this.methodsToAdd.push(["replaceContent", this.replaceContent]);
	this.methodsToAdd.push(["remove", this.remove]);
};

DOMAss.prev = function (){
	var prevSib = this.previousSibling;
	while(prevSib && prevSib.nodeType != 1){
		prevSib = prevSib.previousSibling;
	}
	return prevSib;
};

DOMAss.next = function (){
	var nextSib = this.nextSibling;
	while(nextSib && nextSib.nodeType != 1){
		nextSib = nextSib.nextSibling;
	}
	return nextSib;
};

DOMAss.create = function (name, attr, append, content){
	var elm = document.createElement(name);
	elm = $(elm);
	if(attr){
		elm.setAttributes(attr);
	}
	if(typeof content != "undefined"){
		elm.addContent(content);
	}
	if(append){
		this.addContent(elm);
	}
	return elm;
};

DOMAss.setAttributes = function (attr){
	for(var i in attr){
		if(/class/i.test(i)){
			this.className = attr[i];
		}
		else{
			this.setAttribute(i, attr[i]);
		}	
	}
};

DOMAss.addContent = function (content){
	var retVal = null;
	if(typeof content == "string"){
		retVal = this.innerHTML += content;
	}
	else{		
		retVal = this.appendChild(content);
	}
	return retVal;
};

DOMAss.replaceContent = function (newContent){
	for(var i=(this.childNodes.length - 1); i>=0; i--){
    	this.childNodes[i].parentNode.removeChild(this.childNodes[i]);
    }
	this.addContent(newContent);
};

DOMAss.remove = function (){
	this.parentNode.removeChild(this);
};

DOMAss.initContent();
