$dhtml = (function(){
	// Resource Type
	var $arrSpecTypes = {};
	function register_resource_type($type, $callback)
	{
		if(typeof($type) == "string" && typeof($callback) == "function")
			$arrSpecTypes[$type] = $callback;
	}
	// Html Tag
	var $arrSpecHtmlTags = {};
	function register_html_tag($tag, $callback)
	{
		if(typeof($tag) == "string" && typeof($callback) == "function")
			$arrSpecHtmlTags[$tag] = $callback;
	}
	function html_tag_skip($rc) {}
	function html_tag_default($rc)
	{
		return document.createElement($rc.$tag);
	}
	function html_tag_input($rc)
	{
		var $dom = html_tag_default($rc);
		$dom.type = $rc.$inputType || "text";
		return $dom;
	}
	// Html Key
	var $arrSpecHtmlKeys = {};
	function register_html_key($key, $callback)
	{
		if(typeof($key) == "string" && typeof($callback) == "function")
			$arrSpecHtmlKeys[$key] = $callback;
	}
	function html_key_skip($dom, $key, $value) {}
	function html_key_default($dom, $key, $value)
	{
		$dom[$key] = $value;
	}
	function html_key_childs($dom, $key, $value)
	{
		for(var i=0;i<$value.length;i++)
			create($dom, $value[i]);
	}
	function html_key_style($dom, $key, $value)
	{
		for($key in $value)
		{
			switch($key)
			{
				case "$float":
					if(Sarissa._SARISSA_IS_IE)
						$dom.style.styleFloat = $value[$key];
					else
						$dom.style.cssFloat = $value[$key];
					break;
				default:
					$dom.style[$key] = $value[$key];
					break;
			}
		}
	}
	function create_callback_html_event($dom, $param)
	{
		return function(){ return $param.callback($dom, $param.param) };
	}
	function html_key_events($dom, $key, $value)
	{
		for($key in $value)
		{
			$dom[$key] = create_callback_html_event($dom, $value[$key]);
		}
	}
	//
	function create_html($parent, $rc)
	{
		try
		{
			if(typeof($rc.$ref) == "object")
			{
				return create($parent, $rc.$ref);
			}
			else if(typeof($rc.$tag) == "string")
			{
				$parent = $parent || document.body;
				var $dom;
				if(typeof($arrSpecHtmlTags[$rc.$tag]) == "function")
					$dom = $arrSpecHtmlTags[$rc.$tag]($rc);
				else
					$dom = html_tag_default($rc);

				for($key in $rc)
				{
					if(typeof($arrSpecHtmlKeys[$key]) == "function")
						$arrSpecHtmlKeys[$key]($dom, $key, $rc[$key]);
					else
						html_key_default($dom, $key, $rc[$key]);
				}
				
				if($dom)
				{
					if($dom.tagName.toLowerCase() == "tr")
					{
						var tbody = document.createElement("tbody");
						tbody.appendChild($dom);
						$parent.appendChild(tbody);
					}
					else
						$parent.appendChild($dom);
					return $dom;
				}
			}
			else
				throw new $error("Invalid Html Resource Data!");
		}
		catch(ex)
		{
			alert("Exception : $dhtml.create - [html] " + ex);
		}
		return null;
	}

	function create($parent ,$rc)
	{
		try
		{
			if(typeof($rc) != "object" || $rc == null)
				throw new $error("Require Resource Data!");

			$type = $rc.$type || "html";

			if(typeof($arrSpecTypes[$type]) == "function")
				return $arrSpecTypes[$type]($parent, $rc);
			else
				throw new $error("Unknown Resource Type [" + $type + "]!");
		}
		catch(ex)
		{
			alert("Exception : $dhtml.create - " + ex);
		}
	}

	register_resource_type("html", create_html);

	register_html_tag("input", html_tag_input);

	register_html_key("$type", html_key_skip);
	register_html_key("$tag", html_key_skip);
	register_html_key("$ref", html_key_skip);
	register_html_key("$inputType", html_key_skip);

	register_html_key("$childs", html_key_childs);
	register_html_key("$style", html_key_style);
	register_html_key("$events", html_key_events);

	return {
		create : create
		,registerRcType : register_resource_type
		,registerHtmlTag : register_html_tag
		,registerHtmlKey : register_html_key
	};
})();
