/**
 *  WSM DOM Extension Library, version 1.0.0
 *  (c) 2006 Arturo La Monaca <artlam74@gmail.com>
 */
/*===================================================================
 * Some functions extracted from TinyMCE
 * © 2004-2006, Moxiecode Systems AB, All rights reserved.
 ===================================================================*/ 

var DOM = (function(){
	var dom = {};

	dom.getNodeTree = function(n, na, t, nn) {
		return dom.selectNodes(n, function(n) {
			return (!t || n.nodeType == t) && (!nn || n.nodeName == nn);
		}, na ? na : new Array());
	};
	
	dom.getAbsPosition = function(n, cn) {
		var l = 0, t = 0;
	
		while (n && n != cn) {
			l += n.offsetLeft;
			t += n.offsetTop;
			n = n.offsetParent;
		}

		return {absLeft : l, absTop : t};
	};	

	dom.selectNodes = function(n, f, a) {
		var i;
	
		if (!a)
			a = new Array();
	
		if (f(n))
			a[a.length] = n;
	
		if (n.hasChildNodes()) {
			for (i=0; i<n.childNodes.length; i++)
				dom.selectNodes(n.childNodes[i], f, a);
		}
	
		return a;
	};
	
	dom.selectElements = function(n, na, f) {
		var i, a = [], nl, x;
	
		for (x=0, na = na.split(','); x<na.length; x++)
			for (i=0, nl = n.getElementsByTagName(na[x]); i<nl.length; i++)
				(!f || f(nl[i])) && a.push(nl[i]);
	
		return a;
	};
	
	dom.getOuterHTML = function(e) {
		if (jtask.isIE)
			return e.outerHTML;
	
		var d = e.ownerDocument.createElement("body");
		d.appendChild(e.cloneNode(true));
		return d.innerHTML;
	};
	
	dom.setOuterHTML = function(e, h, d) {
		var d = typeof(d) == "undefined" ? e.ownerDocument : d, i, nl, t;
	
		if (jtask.isIE && e.nodeType == 1)
			e.outerHTML = h;
		else {
			t = d.createElement("body");
			t.innerHTML = h;
	
			for (i=0, nl=t.childNodes; i<nl.length; i++)
				e.parentNode.insertBefore(nl[i].cloneNode(true), e);
	
			e.parentNode.removeChild(e);
		}
	};
	
	dom.prevNode = function(e, n) {
		var a = n.split(','), i;
		while ((e = e.previousSibling) != null) {
			for (i=0; i<a.length; i++) {
				if (e.nodeName == a[i])
					return e;
			}
		}
		return null;
	};
	
	dom.nextNode = function(e, n) {
		var a = n.split(','), i;
		while ((e = e.nextSibling) != null) {
			for (i=0; i<a.length; i++) {
				if (e.nodeName == a[i])
					return e;
			}
		}
		return null;
	};
	
	return dom;
})(); 