/*
	Property: KP Media bigmir)net
	Author: Karpinskiy Alexey (alex_kiy@bigmir.net)
	Release date: november 2007
*/

/* prevent DOM redeclaration if included more than once */
if(typeof window.DOM == 'undefined') {

	/* JS Class to manipulate with DOM */

	window.DOM = {

		// properties
		nodeTypeDesc : {
			UNKNOWN : undefined,
			ELEMENT_NODE : 1,
			ATTRIBUTE_NODE : 2,
			TEXT_NODE : 3,
			CDATA_SECTION_NODE : 4,
			ENTITY_REFERENCE_NODE : 5,
			ENTITY_NODE : 6,
			PROCESSING_INSTRUCTION_NODE : 7,
			COMMENT_NODE : 8,
			DOCUMENT_NODE : 9,
			DOCUMENT_TYPE_NODE : 10,
			DOCUMENT_FRAGMENT_NODE : 11,
			NOTATION_NODE : 12
		},
		
		// methods
		isDomNode : function(node) {
			return (node) ? (node.nodeType > 0) : false;
		},

		/*recursive dom children removal*/
		removeNodeChilds : function(node) {
			if (this.isDomNode(node)) {
				while(node.hasChildNodes()) {
					node.removeChild(node.firstChild);
				}
			}
		},
		
		removeNodeItself : function(node) {
			if (this.isDomNode(node)) {
				var parentNode = node.parentNode;
				if(parentNode != null) {
					parentNode.removeChild(node);
				}
			}
		},

		getCloneNode : function(node) {
			if (this.isDomNode(node)) {
				return node.cloneNode(true);
			}
		},

		getCloneNodeDivWrapped : function(node) {
			if (this.isDomNode(node)) {
				var div = document.createElement('DIV');
				div.appendChild(node.cloneNode(true));
				return div;
			}
		},
		
		cutNodeItself : function(node) {
			if (this.isDomNode(node)) {
				var clone = node.cloneNode(true);
				this.removeNodeItself(node);
				return clone;
			}
		},

		getNodeFirstChild : function(node) {
			if (this.isDomNode(node)) {
				var content = null;
				var childNodes = node.childNodes;
				for(var i=0; i < childNodes.length; i++) {
					if (childNodes.item(i).nodeType == this.nodeTypeDesc.ELEMENT_NODE) {
						content = this.getCloneNode(childNodes.item(i));
						break;
					}
				}
				return content;
			}
		},

		cutNodeFirstChild : function(node) {
			if (this.isDomNode(node)) {
				var content = this.getNodeFirstChild(node);
				this.removeNodeChilds(node);
				return content;
			}
		},

		appendNode : function(sourceNode, childNode) {
			if (this.isDomNode(sourceNode) && this.isDomNode(childNode)) {
				sourceNode.appendChild(childNode);
			}
		},

		getNodeAttributeValue : function(node, attribName) {
			if (this.isDomNode(node) && attribName) {
				return node.getAttribute(attribName);
			}
		},
		
		setNodeAttributeValue : function(node, attribName, attribValue) {
			if (this.isDomNode(node) && attribName) {
				node.setAttribute(attribName, attribValue);
			}
		},
		
		getElementByTagNameAttrib : function(node, tagName, attribName, attribValue) {
			if (this.isDomNode(node) && tagName && attribName) {
				var tags_array = node.getElementsByTagName('input');
				for (var i=0; i < tags_array.length; i++) {
					var tag = tags_array[i];
					if (this.getNodeAttributeValue(tag, attribName) == attribValue) {
						return tag;
					}
				}
			}
		}


	} // end class declaration

} // end if