function XmlFiles()
{
	this.files = new Object();
	this.notifyer = new Array();
	
	this.IsHttpReady = function isHttpReady(xmlHttp)
	{
			if(xmlHttp.readyState == 4)
			{ 
				if (xmlHttp.status == 200 || xmlHttp.status == 0)
				{
					return true;
				}
			}
			
			return false;
	}
	
	this.IsFileDownload = function(file)
	{
		if(!this.files[file])
			return false;
		
		return this.IsHttpReady(this.files[file]);
	}
	
	this.IsNotifyReady = function(nf)
	{
		for(var i in nf.needfiles)
		{
			if(!this.IsFileDownload(nf.needfiles[i]))
				return false;
		}
		
		return true;
	}
	
	this.TryNotifyItem = function(nf)
	{
		if(nf.notifyed)
			return true;
			
		if(this.IsNotifyReady(nf))
		{
			for(var i in nf.needfiles)
			{
				nf.needfiles[i] = this.files[nf.needfiles[i]];
			}
			
			nf.notifyed = true;
			nf.OnFileReady();
			return true;
		}	
		
		return false;				
	}
	
	this.TryNotify = function()
	{
        var allnotifyed = true;
		for(var i in this.notifyer)
		{
			if(!this.TryNotifyItem(this.notifyer[i]))
                allnotifyed = false;
		}
        
        if(allnotifyed && this.notifyer.length > 50)
        {
            this.files = new Object();
            this.notifyer = new Array();     
        }				
	}
	
	this.OnHttpStateChange = function()
	{
		window.xmlfiles.TryNotify();
	}
	
	this.CreateHttpResponse = function()
	{
		var xmlHttp;
		if(window.ActiveXObject)
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		else if(window.XMLHttpRequest)
		{
			xmlHttp = new XMLHttpRequest();
		}
		
		xmlHttp.onreadystatechange = this.OnHttpStateChange;

		return xmlHttp;
	}
	
	this.GetXmlFile = function(file)
	{
		var xmlHttp = this.CreateHttpResponse();
		xmlHttp.open("GET", file, true);
		xmlHttp.send(null); 
		this.files[file] = xmlHttp;
	}
	
	this.GetNotifyFiles = function(nf)
	{
		for(var i in nf.needfiles)
		{
			if(!this.files[nf.needfiles[i]])
			{
				this.GetXmlFile(nf.needfiles[i]);
			}
		}
	}
	
	this.AddNotify = function(nf)
	{
		if(this.TryNotifyItem(nf))
			return;
		
		nf.notifyed = false;
		try
		{
			this.GetNotifyFiles(nf);
			this.notifyer.push(nf);								
		} 
		catch(e)
		{
			nf.e = e;
			nf.notifyed = true;
			nf.OnError(e);
		}
	}		
}

window.xmlfiles = new XmlFiles();

function XmlIsladLoader(xmlfile, xslfile, itemid)
{
	this.needfiles = new Object();
	this.needfiles.xmlfile = xmlfile;
	this.needfiles.xslfile = xslfile;
	this.itemid = itemid;
				
	this.OnLoaded = function(htmlText)
	{
		this.itemid.innerHTML = htmlText;
	};
	
	this.OnError = function(e)
	{
		this.OnLoaded(e);
	}
	
	this.OnFileReady = function()
	{
		var xmlHttp = this.needfiles.xmlfile;
		var xslHttp = this.needfiles.xslfile;
		try
		{
			if(window.ActiveXObject)
			{
				this.OnLoaded(xmlHttp.responseXML.transformNode(xslHttp.responseXML));
			}
			else if(window.XSLTProcessor && window.XMLSerializer)
			{
				var xsltProcessor = new XSLTProcessor();
				xsltProcessor.importStylesheet(xslHttp.responseXML);
				var xmls = new XMLSerializer();
				this.OnLoaded(xmls.serializeToString(
					xsltProcessor.transformToFragment(
					xmlHttp.responseXML, document)));	
			}
			else
			{
				this.OnLoaded("<center><b><font color=red>²»Ö§³Ö´Ëä¯ÀÀÆ÷</font></b></center>");
			}
		}
		catch(e)
		{
			this.OnLoaded(e);
		}
	}
		
	this.Start = function()
	{
		window.xmlfiles.AddNotify(this);
	};	
};

function SingleFileLoader(url, itemid)
{
	this.needfiles = new Object();
	this.needfiles.httpfile = url;
	this.itemid = itemid;
				
	this.OnLoaded = function(htmlText)
	{
		this.itemid.innerHTML = htmlText;
	};
	
	this.OnError = function(e)
	{
		this.OnLoaded(e);
	}
	
	this.OnFileReady = function()
	{
		var httpfile = this.needfiles.httpfile;
		this.OnLoaded(httpfile.responseText);
	}
		
	this.Start = function()
	{
		window.xmlfiles.AddNotify(this);
	};	
};