/*
* Copyright mtr 2008
* @desc prototyped dEl lib. 
*/
/*
* @desc create dElement
* @param object parent 		= 0
* @param string tagName		= 0 default "div"
* @param object JSON		= 0
*/
function dEl( _parent, _el, _json )
 {
 	//create
 	this.___tagName 	= (_el)?_el:'div';
	this.prototype 		= document.createElement(this.___tagName);
	//merge
	if(this.prototype)
		this.prototype  = ___prototype(this.prototype, _json);
	//append	
	if(_parent)
 		_parent 		= (_parent.prototype)?_parent.prototype:_parent;
 	this.prototype.___parent = (_parent)?_parent:document.body;
 	if(this.prototype.___parent)
 		this.prototype.___parent.appendChild( this.prototype );

	//parse childs
	/*
 	if(_json.___jfcChilds)
			this.prorotype = ___appendChilds(this, _json.___jfcChilds );
	 */		
	return this;
 }

 /*
 * @desc get ptototype of constructed object
 * @param dEl Object
 */
function DOMel( _dEl )
{
	return ((_dEl)?(_dEl.prototype?_dEl.prototype:0):0);
}
/*
*
*/
function ___appendChilds(_parent, _json )
{
	for( child_json in _json )
	{
		eval('_parent.'+child_json+'=new Object; _parent.'+child_json+' = new dEl(_parent,_json.'+child_json+'.___tagName,_json.'+child_json+')');
	}		
	return _parent;
}
/*
* @desc merge prototypes
* @param object prototype
* @param object to merge
* @attent be carefull to except noend recursion
*/
function ___prototype( _prototype, _obj )
{
	if(!_prototype||!_obj) return;
	for( _memb in _obj )
	{
		var re = /^\d/;
		if (_memb.match(re))
			continue;
		eval('_m=_obj.'+_memb+';')
		if((typeof(_m)=='object') || (typeof(_m)=='array') )
		{
			if(_memb == '___jfcChilds')
			{
				eval('___appendChilds(_prototype,_obj.'+_memb+')');
			}
			else
			if(_memb!='prototype')
			{
					//be carefull to except noend recursion  
					eval('if(!_prototype.'+_memb+')_prototype.'+_memb+'=new Object();');					
					eval('_prototype.'+_memb+'.prototype=___prototype(_prototype.'+_memb+',_m);');
			}
			else
			{

				eval('_prototype.'+_memb+'=___prototype(_prototype.'+_memb+',_m);');
			}
		}
		else
		{
			eval('try{_prototype.'+_memb+'=_m;}catch(e){}');
		}
	}
	return _prototype;
}
/*
* @desc dEl rpc handler
*/
function rpc_dElHandler( req, parent  )
{
	return dEl(parent,0,req.responseJS.dEl); 
}

/**
* @desc
*/

function ___applyJSON( _json )
{
    for( json in _json)
    {
        if(_json[json].id)
        {
            el = document.getElementById(_json[json].id);
            if( el )
            {
                el = ___prototype(el,_json[json].prototype);
            }
        }
    }
}

/**
*
*/
function ___show( _obj, _display )
{
   _display = _display?_display:'block';
    if(_obj.parentNode.className=='jfcContainer')
    {
        _obj.parentNode.style.display == _display;
    }
    else
    {
        _obj.style.display == _display;
    }
}

/**###########################################
* 
*###########################################*/
function debugObject( _obj, _level )
{
    _level = _level?_level:0;
    document.write('<div style="padding-left:'+_level+'em">');
    for(a in _obj)
    {
        document.write(a+"<br>");
        
        if(typeof(_obj[a])=='object' || typeof(_obj[a])=='array')
            debugObject(a,_level+1);
        
    }
    document.write('</div>');
}
