$file = (function(){

	function create_callback_onload($key)
	{
		return function(){ file_onload($key) };
	}

	function create_callback_readystatechange($xmlhttp, $key)
	{
		return function(){ file_readystatechange($xmlhttp, $key); };
	}

	function file_onload($key)
	{
		for(key in arrFiles[$key].onload)
		{
			arrFiles[$key].onload[key]();
		}
	}

	function file_readystatechange($xmlhttp, $key)
	{
		try
		{
			if($xmlhttp.readyState == 4)
			{
				if ($xmlhttp.responseText == "")
					throw new $error("File [" + $key + "] is empty");
				var domParser = new DOMParser();
				arrFiles[$key].dom = domParser.parseFromString($xmlhttp.responseText, "text/xml");
				setTimeout(arrFiles[$key].callback, 1);
			}
		}
		catch(ex)
		{
			alert("Exception : file_ReadyStateHandle - " + ex);
		}
	}

	function file_load($key)
	{
		try
		{
			if(arrFiles[$key])
			{
				if(arrFiles[$key].dom == null)
				{
					var xmlhttp = new XMLHttpRequest();
					xmlhttp.onreadystatechange = create_callback_readystatechange(xmlhttp, $key);
					xmlhttp.open("GET", $config.file[$key].url, true);
					xmlhttp.send(null);
				}
				else
					setTimeout(arrFiles[$key].callback, 1);
			}
			else
				throw new $error("Unkown File [" + $key + "]");
		}
		catch(ex)
		{
			alert("Exception : file_Load - " + ex);
		}
	}

	function file_register_onload($srcKey, $destKey, $destCallback)
	{
		if(!arrFiles[$srcKey])
			return;
		arrFiles[$srcKey].onload[$destKey] = $destCallback;
	}

	function file_getdom($key)
	{
		if(arrFiles[$key] && arrFiles[$key].dom != null)
			return arrFiles[$key].dom;
		return null;
	}

	var arrFiles = {};
	for(key in $config.file)
	{
		arrFiles[key] = {
			dom : null
			,onload : {}
			,callback : create_callback_onload(key)
		}
	}

	return {
		load : file_load
		,registerOnLoad : file_register_onload
		,getDom : file_getdom
	};

})();

