AM.DOM = {
   /**
    * 
    * 
    * Inspired by
    * http://spudly.shuoink.com/2008/02/20/using-the-xml-dom-without-writing-150000-lines-of-code/
    * 
    * @author Jürgen Cito
    *
    */  
   create : function(args) 
   {  
      var el;  
      
      if( typeof(args) == "string" )
        el = document.createTextNode(args);  
      else if ( typeof(args) == "object" )
      {
        el = document.createElement( args.tag );  
        for (i in args.attributes)
          el.setAttribute(i, args.attributes[i]);
        for ( i in args.style )
          el.style[i] = args.style[i];
        for (var i = 0; i < args.children.length; i++ )
          el.appendChild( AM.DOM.create(args.children[i]));    
      }    
      return el;  
   },  

	methods : 
	{
	 /**
	  * Returns the following DOM HTMLElement of the current element
	  * If a parameter is committed, then the next element of this type is returned.
	  * Otherwise the next non text-node element
	  *
	  * @param {String} tag The -optional- type of the next element
	  */
		next : function(tag) 
		{
			var el = this.nextSibling;
			if(!tag) 
				while(el!==null && el.nodeType==3) // 3 -> text-node
					el=el.nextSibling;
			else
				while(el !== null && el.nodeName != tag.toUpperCase())
					el = el.nextSibling;
			return $get(el);
		},

	 /**
	  * Returns the previous DOM HTMLElement of the current element.
	  * If a parameter is committed, then the previous element of this type is returned.
	  * Otherwise the previous non text-node element
	  *
	  * @param {String} tag The -optional- type of the previous element
	  */
		prev : function(tag) 
		{
			var el = this.previousSibling;
			if(!tag)
				while(el!==null && el.nodeType==3) // 3 -> text-node
					el=el.previousSibling; 
			else
				while(el !== null && el.nodeName != tag.toUpperCase())
					el = el.previousSibling;
			return $get(el);
		},

	 /**
	  * Returns the first DOM-HTML-childelement of the current element.
	  * If a parameter is committed, then the first childelement of this type is returned.
	  * Otherwise the first non text-node childelement
	  *
	  * Note: Due to a bug in IE we CAN'T extend a text object (DOM) with our DOM Methods
	  *
	  * @param {String} tag The -optional- type of the first childelement
	  */
		down : function() 
		{
			var el = this.firstChild;
			
		   while(el!==null && el.nodeType==3)
				el=el.nextSibling;
			return $get(el);
		},

	 /**
	  * Returns an array of DOM-HTML-childelements of the current element.
	  * If a parameter is committed, then only childelements with this type are returned.
	  * Otherwise an array of all non text-node childelements
	  *
	  * @param {String} tag The -optional- type of the childelements
	  */
		getChildren : function(tag) 
		{
			var el = this.down();
			var ret=[];
			if(tag && el.nodeName!=tag.toUpperCase())
				el=el.next(tag);
			while(el!==null) {
				ret.push($get(el));
				el=el.next(tag);
			}
	 		return ret;		
		},

		addClass : function(c)
		{
			var cCaption = AM.Env.ie ? "className" : "class";
			this.setAttribute(cCaption, (this.getAttribute(cCaption) || '') + c);
		}
	}
};

/**
 * Get the DOM HTMLElement with the given name(s) and use the AM.extend function
 * to add the AM.DOM.methods. If there are more arguments, than
 * an array of AM.DOM.method added elements is returned. 
 *
 * @param {String} el The name of a DOM HTMLElement
 */
function $get(el) 
{
	if(arguments.length > 1)
	{
		ret = [];
		for(var i=0; i<arguments.length; i++)
			ret[i] = $get(arguments[i]);
		return ret;
	}
	if (typeof el == 'string') el = document.getElementById(el);
	if(!el)
		return null;
	return AM.extend(el, AM.DOM.methods);
}

function $getClass(classname, dom) { //improve -> mieser code - funkt aber ^^
	var tree=$get(dom || document.body);
	if(tree.className==classname)
		return tree;
	else {
		var children=tree.getChildren();
		for(var i=0;i<children.length;i++) {
			if(children[i].className==classname)
				return $get(children[i]);
			else if(children[i].childNodes.length!=0) {
				var c=$get(children[i]).getChildren();
				if(c.length>0) {
					var d= $getClass(classname,children[i]);
					if(d) return d;
				}
			}		
		}	
	}
}

  function domReady(elid)
  {
    this.n = typeof this.n == 'undefined' ? 0 : this.n + 1;
    if(typeof document.getElementsByTagName != 'undefined' 
         && (document.getElementsByTagName('body')[0] != null || document.body != null))
    {
       if(elid == null || (elid != null && document.getElementById(elid) != null))
         return true;
    }
    else if(this.n < 60)
      setTimeout('domReady()', 250);
  };

