/*

	Wimbo Ajax Engine
	Copyright 2007 Wimbomedia

*/


var AjaxEngine = {
	state : "idle",
	item : {callback:null, url:null, canvas:null, randomize:null, loadMsg:false},
	queu : new Array()
};


/*   CREATE THE HTTPREQUEST   */
AjaxEngine._call = function(callback, script, c, doR, loadingMsg)
{
	var url = (script.indexOf("www.") > -1 || script.indexOf("scripts/") > -1)
			? script
			: "scripts/"+script;

	var doRand = (doR != null) ? doR : true;

	if(loadingMsg != false)
	{
		var area = document.getElementById(c);
		displayLoadingMsg(area);
		var loadMsg = true;
	}
	else
	{
		var loadMsg = loadingMsg;
	}

	/*
	    Check if the engine is busy with another item
	    If it is, then queu this new item and wait...
	*/
	if(AjaxEngine.state == "busy")
	{
		AjaxEngine.queu.push({callback: callback, url:url, canvas:c, randomize:doRand, loadMsg:loadMsg});
		return;
	}
	else
	{
		AjaxEngine.item = {callback: callback, url:url, canvas:c, randomize:doRand, loadMsg:loadMsg};
	}

	if(window.XMLHttpRequest)
	{
		AjaxEngine.xmlHttp = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		AjaxEngine.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}

	if(AjaxEngine.xmlHttp != null)
	{
		if(AjaxEngine.item.randomize != false)
		{
			var rand = Math.random();
			var myCall = (AjaxEngine.item.url.indexOf("?") > -1) ? AjaxEngine.item.url : AjaxEngine.item.url + "?=";
			myCall = myCall + "&rand=" + rand;
		}
		else
		{
			var myCall = AjaxEngine.item.url;
		}

		var xmlHttp = AjaxEngine.xmlHttp;
		xmlHttp.onreadystatechange = AjaxEngine.item.callback;
		xmlHttp.open("GET", myCall, true);
		xmlHttp.send(null);

		// Set the engine state to "busy" i.e. "DO NOT DISTURB"
		AjaxEngine.state = "busy";
	}
}


/*   CALLBACK FUNCTION TO PRINT THE RESPONSE TEXT   */
AjaxEngine._print = function()
{
	var xmlHttp = AjaxEngine.xmlHttp;

	if(xmlHttp.readyState == 4)
	{
		try
		{
			if(xmlHttp.status == 200)
			{
				AjaxEngine.response = xmlHttp.responseText;
			}
			else
			{
				AjaxEngine.response = "<p><center><div class='feedback0'>Error</div></center></p>";
			}

		}
		catch(e)
		{
			AjaxEngine.state = "idle";

			if(AjaxEngine.queu.length > 0)
			{
				AjaxEngine._call(AjaxEngine.queu[0].callback, AjaxEngine.queu[0].url, AjaxEngine.queu[0].canvas, AjaxEngine.queu[0].randomize, AjaxEngine.queu[0].loadMsg);
				AjaxEngine.queu.splice(0,1);
			}

			return;
		}

		var canvas = document.getElementById(AjaxEngine.item.canvas);

		try
		{
			if(AjaxEngine.item.canvas == AlertBox.name)
			{
				AlertBox.show(AjaxEngine.response);
			}
			else
			{
				canvas.innerHTML = AjaxEngine.response;
			}
		}
		catch(e)
		{
			AjaxEngine.state = "idle";

			if(AjaxEngine.queu.length > 0)
			{
				AjaxEngine._call(AjaxEngine.queu[0].callback, AjaxEngine.queu[0].url, AjaxEngine.queu[0].canvas, AjaxEngine.queu[0].randomize, AjaxEngine.queu[0].loadMsg);
				AjaxEngine.queu.splice(0,1);
			}

			return;
		}

		AjaxEngine.state = "idle";

		if(AjaxEngine.queu.length > 0)
		{
			AjaxEngine._call(AjaxEngine.queu[0].callback, AjaxEngine.queu[0].url, AjaxEngine.queu[0].canvas, AjaxEngine.queu[0].randomize, AjaxEngine.queu[0].loadMsg);
			AjaxEngine.queu.splice(0,1);
		}
	}
}