var fpAF = [];

fpAF = 
	{
	AjaxLink		: [],
	tasks			: [],
	execute			: false,
	contentUrl		: false,
	currentContentUrl	: false,
	loadingIndicator	: false,
	locationHash		: false,

	init: function()
		{
		this.getHash();
		fpAF.purgeInterval = setInterval("fpAF.purge()",300);
		fpAF.pollHashInterval = setInterval("fpAF.pollHash()",300);
		},

	getHash: function()
		{
		if(window.location.hash)
			{
			locationHash = window.location.hash.slice(2).split("/");
			fpAF.locationHash = window.location.hash;
			if(typeof(fpWC) != 'undefined')
				{
				var ajaxLink = fpWC.dbWindow[locationHash[0]];
				var ajaxLinkParams = locationHash[1];
				if(ajaxLink)
					{
					if(ajaxLinkParams)
						{
						ajaxLink.params = window.location.hash.slice(locationHash[0].length+3);
						}
					fpWC.newWindow(ajaxLink);
					}
				}
			}
		else	{
//			window.location.href = "#/";
			}
		},
	
	pollHash: function()
		{
		if(window.location.hash)
			{
			(window.location.hash != fpAF.locationHash) ? fpAF.getHash() : false;
			}
		},

	addToLine: function(requestData, disableCache)
		{		
		this.tasks.push({
			content		: requestData.content,
			target		: requestData.target,
			onscreen	: requestData.onscreen,
			runafter	: requestData.runafter,
			parameters	: (typeof(requestData.parameters) != 'undefined') ? "?"+requestData.parameters : '?',
			disableCache	: (typeof(requestData.disableCache) != 'undefined') ? "&disableCache="+Math.round(Math.random()*100000) : '',
			ajaxMethod	: (typeof(requestData.ajaxMethod) != 'undefined') ? requestData.ajaxMethod : 'GET',
			imagePreload	: (typeof(requestData.imagePreload) != 'undefined') ? requestData.imagePreload : false,
			passRequest	: (typeof(requestData.passRequest) != 'undefined') ? requestData.passRequest : 'request.responseXML'
			});
		(!fpAF.purgeInterval) ? fpAF.purgeInterval = setInterval("fpAF.purge()",300) : false;
		},

	purge: function()
		{
		if(this.tasks.length > 0)
			{
			if(!this.tasks[0].execute)
				{
				if(this.execute == false)
					{
					this.tasks[0].execute = true;
					this.loadContent(this.tasks[0].parameters);
					}
				}
			}
		else	{
			clearInterval(fpAF.purgeInterval);
			fpAF.purgeInterval = false;
			}
		},

	loadContent: function()
		{
		request = fpAF.ajaxGateWay();
		if(!request) return false;
		if(this.tasks[0].ajaxMethod == 'POST')
			{
			request.open("POST", this.tasks[0].content, true);
			request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			request.onreadystatechange = this.getContent;
			request.send(this.tasks[0].parameters.slice(1));
			}
		else	{
			request.open("GET", this.tasks[0].content+this.tasks[0].parameters+this.tasks[0].disableCache, true);
			request.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
			request.onreadystatechange = this.getContent;
			request.send(null);
			}
		},

	ajaxGateWay: function()
		{
		if (window.ActiveXObject)
			{
			waystation = new ActiveXObject("Microsoft.XMLHTTP");
			}
		else if (window.XMLHttpRequest)
			{
			waystation = new XMLHttpRequest();
			}
		else	{
			 waystation = false;
			}
		
		return waystation;
		},
		
	getContent: function()
		{
		if (request.readyState != 4)
			{
			fpAF.execute = true;
			if(fpAF.loadingIndicator) $(fpAF.loadingIndicator).style.display = "block";
	 		}
	
		if (request.readyState == 4)
			{
			(fpAF.tasks[0].target) ? loadTarget = fpAF.tasks[0].target : false;

			if (request.status == 200)
				{
				if(fpAF.tasks[0].onscreen == true){loadTarget.innerHTML = request.responseText;}
				else if(fpAF.tasks[0].onscreen == false) {}
				else	{
					fpAF.tasks[0].onscreen(request.responseXML);
					}
				}
			else	{
				if(fpAF.tasks[0].onscreen == true)
					{
					loadTarget.innerHTML = "loading..."
					}
				}

			if(fpAF.loadingIndicator) $(fpAF.loadingIndicator).style.display = "none";

			if(fpAF.tasks[0].runafter)
				{
				fpAF.tasks[0].runafter.toString();
				if(fpAF.tasks[0].imagePreload) eval(fpAF.tasks[0].runafter());
				else eval(fpAF.tasks[0].runafter(eval(fpAF.tasks[0].passRequest)));
				};
			fpAF.tasks.remove(fpAF.tasks[0]);
			fpAF.execute = false;
			}
		}
	}

fpAF.init();
