/* ---------------------------------------------------
 * AJAX WebControls 0.1.1
 * ---------------------------------------------------
 *  HTML Builder
 */
 
 
function Set()
{
	var set = {}, i = arguments.length;
	while (i--)
		set[arguments[i]] = 1;

	return set;
}

var idCache = {};
function $(id)
{
	if (!(id in idCache))
		idCache[id] = document.getElementById(id);

	return idCache[id];
}

function $$(id)
{
	return new ObjectHolder( id );
}



function attachEventHandler( element, type, handler )
{
	element[type] = handler;
}


function registerFunction( bla )
{
	return function( )
	{
		alert(bla);
	}
}

function ObjectHolder( id )
{
	this.objID = id;

	this.hide = function( ){ $( this.objID ).style.display = 'none'; }
	this.show = function( ){ $( this.objID ).style.display = 'block'; }
	this.get = function( pointer ) { if( this.objID[pointer] ) return this.objID[pointer]; }
	this.set = function( pointer, value ) { this.objID[pointer] = value; }
	this.attachEventHandler = function( type, callback ){ this.objID[type] = callback; }
}

function WebControlsBuilder( )
{
	this.eventHandlers = Set('onblur', 'onchange', 'onclick', 'ondblclick', 'onfocus', 'onkeydown','onkeypress', 'onkeyup', 'onload', 'onmousedown', 'onmousemove', 'onmouseout','onmouseover', 'onmouseup', 'onreset', 'onselect', 'onsubmit', 'onunload');
	
	this.built = function( elements ) 
	{
		return this.builtRecursive( elements );	
	}
	
	this.builtRecursive = function ( object )
	{
		var element = null;
		if( object.obj )
		{
			element = object.obj;	
		}
		else if( object.add )
		{
			if( object.add == 'text' ) 
			{
				element = document.createTextNode(object.value);	
			}
			else
			{
				element = document.createElement(object.add);
				if( object.attributen )
				{
					for( att in object.attributen ) 
					{
						if( object.attributen[att] !== null )
						{
							if( att in this.eventHandlers )
							{
								var attVal = att;
								// Handler toevoegen
								element[att] = new registerFunction( object.attributen[att] );
							}
							else
							{
								if( att == 'style' ) 	
									element.style.cssText = object.attributen[att];
								else
									element[att] = object.attributen[att];
							}
						}
					}	
				}
			}
		
		
			if( object.create ) 
			{
				var childNode;
				for( x = 0; x < object.create.length; x++ )
				{
				
					if( object.create[x] && (childNode = this.builtRecursive(object.create[x])))	
						element.appendChild( childNode );
				
				}
			}
		}
		
		return element;
	}	
}
 
 
 