var ajaxRequest;
var ajaxInProgress = false;
var customAjaxInProgress = false;
var preLoadImages = new Array();
unFocus.History.addEventListener('historyChange', ajaxCheckLocation);

function stateChanged(){
	
	if (ajaxRequest.readyState == 4){
		
		var commands;
		try{
			commands = eval('(' + ajaxRequest.responseText + ')');
		}
		catch(e){
			var newDoc=document.open("text/html");
			newDoc.write(ajaxRequest.responseText);
			newDoc.close();
			document.body.innerHTML += "<p style='font-weight: bold'>The above text was returned by the sever during an AJAX request. It is not valid JSON.</p>";
			//window.alert(ajaxRequest.responseText);
			return;
		}
		
		for(var i = 0; i < commands.length; i++){
			/*if(commands[i].wait){
				var c = commands[i];
				window.setTimeout(function(){processCommand(c)}, c.wait);
			}
			else*/
				processCommand(commands[i]);
		}
		ajaxInProgress = false;
	}
}

function processCommand(command){
	
	if(command.wait){
		window.setTimeout(function(){processCommand(command)}, command.wait);
		command.wait = 0;
	}
	else{
		var target;
		switch(command.target){
			
			case '':
				break;
			
			case 'body':
				target = document.body;
				break;
			
			default:
				target = document.getElementById(command.target);
		}
		
		switch(command.name){
			
			case 'innerHTML':
				target.innerHTML = command.value;
				break;
			
			case 'className':
				target.className = command.value;
				break;
			
			case 'title':
				document.title = command.value;
				break;
			
			case 'preload':
				preLoadImages.push(new Image());
				if(command.onload)
					preLoadImages[preLoadImages.length - 1].onload = function(){processCommand(command.onload)};
				preLoadImages[preLoadImages.length - 1].src = command.value;
				break;
			
			/*case 'replace':
				var newElement = document.createElement("div");
				newElement.innerHTML = command.value;
				target.parentNode.replaceChild(newElement, target);
				break;*/
			
			default:
				if(typeof customCommands == 'function')
					customCommands(command, target);
		}
	}
}

function GetXmlHttpObject()
{
	var xmlHttp = null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function ajaxClick(link){
	
	if(ajaxInProgress || customAjaxInProgress || '#' + link.href.replace('http://' + window.location.hostname, '') == window.location.hash){
		
		if(typeof customAjaxClickCurrent == 'function')
			customAjaxClickCurrent(link);
		return false;
	}
	
	ajaxInProgress = true;
	unFocus.History.addHistory(link.href.replace('http://' + window.location.hostname, ''));
	if(typeof customAjaxClick == 'function')
		customAjaxClick(link);
	
	return false;
}

/*function ajaxLoad(){
	ajaxInProgress = true;
	//unFocus.History.addHistory(unFocus.History.getCurrent());
	if(typeof customAjaxClick == 'function')
		customAjaxClick(link);
}*/

function ajaxCheckLocation(historyHash){
	
	var value = "1";
	if(arguments.length == 2)
		value = arguments[1];
	
	if(value != "onload" && value.indexOf("login") != 0 && value.indexOf("register") != 0)
		fadeOutContentChange(hasContent, document.getElementById("top_text").style, document.getElementById('panel_mid').style, document.getElementById('panel_mid_content').style, 100);
	
	ajaxRequest = GetXmlHttpObject();
	ajaxRequest.onreadystatechange = stateChanged;
	ajaxRequest.open("GET", 'http://' + window.location.hostname + historyHash + '?ajax=' + value + '&rnd=' + Math.floor(Math.random() * 1000), true);
	ajaxRequest.send(null);
}