/* #############################################################################

NS: TESCODIRECT.lib.DOM.node
	static: yes

dependancies:
	TESCODIRECT

############################################################################# */

TESCODIRECT.lib.DOM.node = new function() {
	this.VERSION = "1.0.6";
	this.NAME = "TESCODIRECT.lib.DOM.node";

	this.create = function(nodeName, textValue, context, ns) {
		if(nodeName) {
			var oNode;
			if(!context) {
				context = document;
			}
			if(!ns && ns !== "") {
				ns = "http://www.w3.org/1999/xhtml"
			}
			if(context.createElementNS) {
				oNode = context.createElementNS(ns, nodeName);
			} else {
				oNode = context.createElement(nodeName);
			}
			if(textValue) {
				TESCODIRECT.lib.DOM.node.setTextValue(oNode, textValue);
			}
		}
		
		return oNode;
	}

	this.createText = function(textValue, context) {
		if(!context) {
			context = document;
		}
		return context.createTextNode(textValue);
	}

	this.setTextValue = function(node, textValue) {
		if(node.nodeType == 3) {	// Text node
			node.nodeValue = textValue;
		} else {
			while(node.firstChild) {
				node.removeChild(node.firstChild);
			}
			node.appendChild(this.createText(textValue));
		}
	}
	//SJH
	this.getStyle = function(obj, property){
		if (obj.style[property]) {
			return obj.style[property];				// It's been set inline. Nice and quick
		} else if (obj.currentStyle) {
			return obj.currentStyle[property];		//IE's method + Opera 9 (I think - need to test)
		} else if (document.defaultView && document.defaultView.getComputedStyle) {
			property = property.replace(/([A-Z])/g,"-$1"); // follows css model of style writing e.g. text-align instead of textAlign
			property = property.toLowerCase();
			return document.defaultView.getComputedStyle(obj, "").getPropertyValue(property);	// W3C's method
		} else {
			return null; // Some other browser
		}
	}
    // END SJH
    
	this.getTextValue = function(node) {
		try {
			if(node.innerText) {
				return node.innerText;
			} else {
				return node.textContent;
			}
		} catch(e) {
			return node.innerText;
		}
	}

	this.getTextValueNS = function(prefix, local, parentElem, index) {
		var result = TESCODIRECT.lib.DOM.node.getNS(prefix, local, parentElem, index);
		if(result) {
			if (result.childNodes.length > 1) {
				return result.childNodes[1].nodeValue;
			} else {
				return result.firstChild.nodeValue;    		
			}
		} else {
			return "";
		}
	}
	
	this.getNS = function(prefix, local, parentElem, index) {
		var result = null;
		if(prefix && window.ActiveXObject) {
			result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
		} else {
			result = parentElem.getElementsByTagName(local)[index];
		}
		return result;
	}
	
	this.createAttribute = function(name, context, ns) {
		if(name) {
			var oNode;
			if(!context) {
				context = document;
			}
			if(!ns && ns !== "") {
				ns = "http://www.w3.org/1999/xhtml"
			}
			if(context.createAttributeNS) {
				oNode = context.createAttributeNS(ns, name);
			} else {
				oNode = context.createAttribute(name);
			}
		}
		
		return oNode;
	}
	
	this.setNamedItem = function(attribute, context) {
		if(context.setNamedItemNS) {
			return context.setNamedItemNS(attribute);
		} else {
			return context.setNamedItem(attribute);
		}
	}
	
	this.hasClassName = function(obj, name) {
		return !!obj.className.match(new RegExp('(\\s|^)' + name + '(\\s|$)'));
	}


	this.getChildrenByTagName = function(o,tagName,className) {
		var children = o.childNodes;
		var res = [];
		var element;
		tagName = tagName.toLowerCase();

		for(var i = 0; i < children.length; i++) {
			if(((element = children[i]).tagName && element.tagName.toLowerCase() == tagName) &&
				((className == null )|| this.hasClassName(element,className))) {
				res.push(element);
			}
		}
		return res;
	}

}
