/**
 * JavaScript WMDOM Library
 * Copyright 2005 by Mathias Karstaedt
 * adapted and expanded by Gregor Kofler
 * 
 * @version 1.1.1 2007-06-07
 */

String.prototype.domTags = "address applet area a base basefont big blockquote body br b caption center cite code dd dfn dir div dl dt em font form h1 h2 h3 h4 h5 h6 head hr html img input isindex i kbd link li map menu meta ol option param pre p samp script select small strike strong style sub sup table td textarea th title tr tt ul u var".split(" ");

/**
 * creates DOM element, adds previously set attributes and adds optional child node(s) or text node
 * e.g. "div".DomCreateElement("p".domCreateElement("Ich bin ein Absatz"));
 */
String.prototype.domCreateElement = function(children) {
	var i, a, tag = document.createElement(this);

	if(this.attr) {
		for(i = 0; i < this.attr.length; i++) {
			if(this.attr[i].name == "name") {
				try	{ tag = document.createElement("<"+this+" name="+this.attr[i].value+">"); }
				catch(e) { }
				break;
			}
		}

		for(i = 0; i < this.attr.length; i++) {
			a = this.attr[i];
			switch(a.name) {
				case "class": tag.className = a.value; break;
				default: tag.setAttribute(a.name, a.value);
			}
		}
	}
	return domAppendChildren(tag, children);
}

/**
 * sets one or more attributes
 * e.g.: "img".domSetAttrib("src","test.jpg");
 * e.g.: "input".domSetAttrib([["class", "format"], ["type", "submit"]]);
 */
String.prototype.domSetAttrib = function(n, v) {
	var i;
	if (!this.attr) { this.attr = []; }

	if (arguments.length > 1) {
		this.attr.push({name: n, value: v});
	}
	else if (arguments.length == 1 && typeof n == "object") {
		for (i = 0; i < n.length; i++) {
			this.attr.push({name : n[i][0], value : n[i][1]});
		}
	}
	return this;
}

/**
 * returns element with given id,
 * appends optional children
 */
String.prototype.domId = function(children) {
	return domAppendChildren(document.getElementById(this), children);
}

/**
 * returns element with given name and optional index,
 * appends optional children
 */
String.prototype.domName = function(ndx, children) {
	var elems = document.getElementsByName(this);
	if(typeof(ndx) == "number") {
		return children ? domAppendChildren(elems[ndx], children) : elems[ndx];
	}
	return elems;
}

/**
 * creates DOM elements for each element in array
 * e.g. ["td","td","td"].domCreateElement();
 */
Array.prototype.domCreateElement = function(children) {
	var i, tmp = [];
	for (i = 0; i < this.length; i++) { tmp[i] = this[i].domCreateElement(children); }
	return tmp;
}

/**
 * sets one or more attributes for each element in array
 * e.g. ["td","td"].domSetAttrib("style","color:blue");
 */
Array.prototype.domSetAttrib = function(n, v) {
	for (var i = 0; i < this.length; i++) { this[i] = this[i].domSetAttrib(n, v); }
	return this;
}

/**
 * encloses array elements with given tags
 * if argument is an array every element is enclosed in all tags given in array
 * e.g. ["id","name","address"].domWrapWithTag("th");
 *      ["id","name","address"].domWrapWithTag(["a","b","div".domSetAttrib("id","1")]);
 */
Array.prototype.domWrapWithTag = function(tag) {
	var i, j;
	if (typeof tag == "string") {
		for (i = 0; i < this.length; i++) {
			if ("".domTags.inArray(this[i]))	{ this[i] = tag.domCreateElement(this[i].domCreateElement()); }
			else								{ this[i] = tag.domCreateElement(this[i]); }
		}
	}
	else {
		for (i = 0; i < this.length; i++) {
			for (j = 0; j < tag.length; j++) { this[i] = tag[j].domCreateElement(this[i]); }
		}
	}
	return this;
}

/**
 * adds child element
 */
domAppendChildren = function(tag, children) {
	var i, c;

	if (children != null) {
		if (typeof children == "string" || typeof children == "number") {
			tag.appendChild(document.createTextNode(children));
		}
		else if (typeof children.length != "undefined" && typeof children.nodeName == "undefined") {
			for (i = 0; i < children.length; i++) {
				c = children[i];
				if (typeof c == "string" || typeof c == "number") {
					tag.appendChild(document.createTextNode(c));
				}
				else { tag.appendChild(c); }
			}
		}
		else {
			tag.appendChild(children);
		}
	}
	return tag;
}

/**
 * remove all ChildNodes of a node
 */
domDeleteChildNodes = function(elem) {
	var i, l = elem.childNodes.length;
	for (i = l; i > 0; i--) { elem.removeChild(elem.childNodes[i-1]); }
}

/**
 * getElementsByClassName
 * @param {string} class name
 * @param {object} optional parent object
 * @return {array}
 */
document.getElementsByClassName = function(clsName, parentElem) {
	var i, elem, f = [];
	var childElems	= arguments.length < 2 ? document.getElementsByTagName("*") : parentElem.getElementsByTagName("*");

	for (i = 0;( elem = childElems[i]); i++) {
		if(elem.className.match(new RegExp("(^|\\s)" + clsName + "(\\s|$)"))) { f.push(elem); }
	}
    return f;
}

/**
 * get offset of element
 * @param {object} DOM object
 * @return {array} x and y offset
 */
document.getElementOffset = function(elem) {
	var pos = [elem.offsetLeft, elem.offsetTop];
	while(elem = elem.offsetParent) {
		pos = pos.cAdd([elem.offsetLeft, elem.offsetTop]);
	}
	return pos;
}

/**
 * get pos of element via css
 * @param {object} DOM object
 * @return {array} x and y position
 */
document.getElementPosition = function(elem) {
	var x = parseInt(elem.style.left), y = parseInt(elem.style.top);
	return [isNaN(x) ? 0 : x, isNaN(y) ? 0 : y];
}

/**
 * get size of element
 * @param {object} DOM object
 * @return {array} width and height
 */
document.getElementSize = function(elem) {
	return [elem.offsetWidth, elem.offsetHeight];
}

/**
 * set x/y-position of element
 * @param {object} DOM object
 * @param {array} x and y position
 */
document.setElementPosition = function(elem, pos) {
	elem.style.left	= parseInt(pos[0]).toString() + "px";
	elem.style.top	= parseInt(pos[1]).toString() + "px";
}

document.nextNeighbor = function(n) {
	var nName = n.nodeName;
	n = n.nextSibling;
	while(n != null) {
		if(n.nodeName == nName) { return n; }
		n = n.nextSibling;
	}
	return null;
};

document.prevNeighbor = function(n) {
	var nName = n.nodeName;
	n = n.previousSibling;
	while(n != null) {
		if(n.nodeName == nName) { return n; }
		n = n.previousSibling;
	}
	return null;
}

document.moveBefore = function(n1, n2) {
	var p = n1.parentNode;
	p.removeChild(n1);
	p.insertBefore(n1, n2);
};

document.moveAfter = function(n1, n2) {
	var p = n1.parentNode;
	p.removeChild(n1);
	p.insertBefore(n1, n2 ? n2.nextSibling : null);
}