/**
 * JavaScript WMDOM Library
 * Copyright 2005 by Mathias Karstädt
 **/

String.prototype.tags = "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(" ");

/**
 * Erstellt ein HTMLDOMElement, setzt die vorhandenen Attribute und bindet
 * automatisch Kinderelemente ein, sofern welche übergeben wurden.
 * Wenn ein String übergeben wird, wird dies als TextNode eingebunden.
 *
 * Bsp: "p".t("Ich bin ein Absatz");
 * Bsp: "div".t("p".t("Ich bin ein Absatz"));
 **/
String.prototype.t = function(children) 
{
	var tag = document.createElement(this);
	if (this.attribs != null) 
	{
		for(var i = 0; i < this.attribs.length; i++) 
		{
			var attr = this.attribs[i];
			if (attr.name == "class") {
				tag.className = attr.value;
			}else{
				tag.setAttribute(attr.name, attr.value);
			}
		}
	}
	return appendChildren(tag, children);
}

/**
 * Setzt ein oder mehrere Attribute.
 * wenn der erste Parameter ein Array ist, dann wird dieses verwendet.
 *
 * Bsp: "img".a("src","test.jpg");
 **/
String.prototype.a = function(n, v) 
{
	if (this.attribs == undefined || this.attribs == null) this.attribs = new Array();
	if (arguments.length > 1) 
	{
		this.attribs.push({name : n, value : v});
	} 
	else if (arguments.length == 1)
	{
		if (n.length != undefined) 
		{
			for (var i = 0; i < n.length; i++) 
			{
				this.attribs.push({name : n[i][0], value : n[i][1]});
			}
		}
	}
	return this;
}

/**
 * Sucht nach einem HTML Element mit der ID.
 *
 * Bsp: "test".id();
 * Bsp: "test".id("h1".t("Header"));
 **/
String.prototype.id = function(children) 
{
	return appendChildren(document.getElementById(this), children);
}

/**
 * Sucht nach einem HTML Element mit dem übergebenen NAME Attribut.
 *
 * Bsp: "test".name();
 * Bsp: "test".name(1);
 * Bsp: "test".name(2,"h1".t("Header"));
 **/
String.prototype.name = function(nr, children) 
{
	var elements = document.getElementsByName(this);
	if (nr != null && typeof(nr) == "number" && children != null)
	{
		return appendChildren(elements[nr], children);
	}
	else if (nr != null && typeof(nr) == "number" && children == null)
	{
		return elements[nr];
	}
	else
	{
		return elements;
	}
}

/**
 * Hilfsfunktion um einem Element ChildElemente hinzuzufügen.
 **/
appendChildren = function(tag, children) {
	if (children != null) 
	{
		if (typeof(children) == "string" || typeof(children) == "number") 
		{
			var text = document.createTextNode(children);
			tag.appendChild(text);
			return tag;
		}
		else if (children.length != undefined && children.nodeName == undefined) 
		{
			for (var i = 0; i < children.length; i++)
			{
				var child = children[i];
				if (typeof(child) == "string" || typeof(children) == "number") 
				{
					var text = document.createTextNode(child);
					tag.appendChild(text);
				}
				else
				{
					tag.appendChild(child);
				}
			}
			return tag;
		}
		else 
		{
			tag.appendChild(children);
		}

	}
	return tag;
}

/**
 * Ermöglicht es die Funktion t() auch auf Arrays anzuwenden.
 *
 * Bsp: ["td","td","td"].t();
 **/
Array.prototype.t = function(children) 
{
	var tmp = new Array();
	for (var i = 0; i < this.length; i++) 
	{
		tmp[i] = this[i].t(children);
	}
	return tmp;
}

/**
 * Ermöglicht es die Funktion a() auch auf Arrays anzuwenden.
 *
 * Bsp: ["td","td"].a("style","color:blue");
 **/
Array.prototype.a = function(name, value) 
{
	for (var i = 0; i < this.length; i++) 
	{
		var t = this[i];
		this[i] = t.a(name, value);
	}
	return this;
}

/**
 * Umschließt die Inhalte des Arrays mit dem angegebenen Tag.
 * Wird ein Array als Parameter übergeben, versucht die Funktion
 * jedes Element aus dem ersten Array mit den Elementen aus
 * dem übergebenen Array zu umschließen.
 *
 * Bsp: ["id","name","address"].s("th");
 * Bsp: ["id","name","address"].s(["a","b","div".a("id","1")]);
 **/
Array.prototype.s = function(tag) 
{
		if (typeof(tag) == "string" || (tag.length == undefined && tag.nodeName == undefined))
		{
			//Wenn kein Array
			for (var i = 0; i < this.length; i++) 
			{
					if ("".tags.contains(this[i])) 
					{
						this[i] = tag.t(this[i].t());
					}
					else
					{
						this[i] = tag.t(this[i]);
					}
			}
		}
		else
		{
			//Wenn ein Array
			for (var i = 0; i < this.length; i++) 
			{
				for (var j = 0; j < tag.length; j++)
				{
					this[i] = tag[j].t(this[i]);
				}
			}
		}
	return this;
}

/**
 * Durchsucht das Array nach dem übergebenen Objekt und liefert true
 * zurück, wenn es gefunden wurde.
 **/
Array.prototype.contains = function(obj)
{
	for (var i = 0; i < this.length; i++)
	{
		var o = this[i];
		if (o == obj)
		{
			return true;
		}
	}
	return false;
}

/**
 * alle ChildNodes einer Node löschen
 **/
deleteChildNodes = function(element)
{
	var childrenLength = element.childNodes.length;
	if (childrenLength > 0)
	{
		for (var i = childrenLength; i > 0; i--)
		{
			var childNode = element.childNodes[i-1];
			element.removeChild(childNode);
		}
	}
}
