/**
 * @author Marco Alionso Ramirez, marco@onemarco.com
 */

var IS_IE = false;//@cc_on IS_IE = true;

/**
 * 
 * @param {Object} e
 * @return {Element}
 */
function jsonToDom(e){
	if(null != e.txt) return document.createTextNode(e.txt);
	if(null == e.el) return null;
	var node = document.createElement(e.el.toUpperCase());
	if(null != e.att){
		for(var i in e.att){
			if(IS_IE){		
				node = applyAttribute(node,i,e.att[i]);
			}else{
				applyAttribute(node,i,e.att[i]);
			}
		}
	}
	if(null != e.ch){
		for(var j in e.ch){
			var childNode = jsonToDom(e.ch[j]);
			if(null !== childNode) node.appendChild(childNode);
		}
	}
	return node;
}

function applyAttribute(node,attribute,value){
	if(IS_IE){
		return ieApplyAttribute(node,attribute,value);
	}else{
		node.setAttribute(attribute,value);
	}	
}

function ieApplyAttribute(node,attribute,value){
	switch(attribute.toLowerCase()){
		case 'for':
			node.htmlFor = value;
			break;
		case 'class':
			node.className = value;
			break;
		case 'style':
			node.style.textCss = value;
			break;
		case 'name':
			node = document.createElement(node.outerHTML.replace(">"," name=" + value + ">"));
			break;
		default:
			node.setAttribute(attribute,value);
	}
	return node;
}

function addClassToElement(element, className){
	if((new RegExp('\\b' + className + '\\b')).test(element.className)) return;
	element.className += ' ' + className;
}
function removeClassFromElement(element, className){
	element.className = element.className.replace(
		new RegExp('(\\W+)?\\b' + className + '\\b(\\W+)?','g'),' ');
}
