/*
	Blackout Ajax Library (Stripped Version)
	
	This library contains the basic code for using ajax.
	Based on the Ajax Gold framework from "Ajax for Dummies". Extended by Jeremy Bell.
	
	Copyright (C) 2007 Jeremy Bell
	jeremy@blackout.biz
	Blackout Entertainment Limited
*/

var ajaxObjects = Array();
var ajaxObjectsCancelled = Array();
var nextAjaxRequest = 0;
var ajaxEnabled = true; // Turn this off to halt all ajax communication

function enableAjax(){
	ajaxEnabled = true;
}

function disableAjax(){
	ajaxEnabled = false;
}

function getDataReturnText(url, callback, servererrorcallback, cache, interactivecallback, id)
{ 
  if(!ajaxEnabled) return;
  
	// If we're calling a page, refresh ads
	if(window.refreshAd) refreshAd(url);
  
  // Make sure we're online, otherwise call the error callback function
  if(!navigator.onLine){
	  servererrorcallback(0,url);
  }
	
	// Manage id
	if(!id) id=null;
  
  var index = nextAjaxRequest;
  nextAjaxRequest++;
  ajaxObjects[index] = false; 
  
  if (window.XMLHttpRequest) {
    ajaxObjects[index] = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    ajaxObjects[index] = new 
    ActiveXObject("Microsoft.XMLHTTP");
  }

  if(ajaxObjects[index]) {
		
		ajaxObjectsCancelled[index] = false;
		
		if (!cache) {
			if(url.indexOf('ajaxcache=')>=0){ url = url.substr(0,url.indexOf('ajaxcache=')-1); }
			if(url.indexOf('?')>=0){ url += '&'; } else { url += '?'; }
			url += 'ajaxcache=' + Math.floor(Math.random()*99999999);
		}
		
    ajaxObjects[index].open("GET", url, true); 

    ajaxObjects[index].onreadystatechange = function() 
    {
			if (ajaxObjects[index].readyState == 3){
				if(interactivecallback) interactivecallback(id);
			} else if (ajaxObjects[index].readyState == 4 && 
				ajaxObjects[index].status == 200) {
					if(ajaxEnabled&&!ajaxObjectsCancelled[index]) callback(ajaxObjects[index].responseText,url,id);
					delete ajaxObjects[index];
					ajaxObjects[index] = null;
			} else if (ajaxObjects[index].readyState == 4 && ajaxObjects[index].status){
				if(ajaxEnabled&&!ajaxObjectsCancelled[index]) servererrorcallback(ajaxObjects[index].status,url,id);  
			}
    };

    ajaxObjects[index].send(null);
  }
  return index;
}

function postDataReturnText(url, data, callback, servererrorcallback, cache, interactivecallback, id)
{ 
  if(!ajaxEnabled) return;
  
	// If we're calling a page, refresh ads
	if(window.refreshAd) refreshAd(url);
  
  // Make sure we're online, otherwise call the error callback function
  if(!navigator.onLine){
	  servererrorcallback(0,url);
  }
	
	// Manage id
	if(!id) id=null;
  
  var index = nextAjaxRequest;
  nextAjaxRequest++;
  ajaxObjects[index] = false; 

  if (window.XMLHttpRequest) {
    ajaxObjects[index] = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    ajaxObjects[index] = new 
     ActiveXObject("Microsoft.XMLHTTP");
  }

  if(ajaxObjects[index]) {
		
		ajaxObjectsCancelled[index] = false;
		
		if (!cache) {
			if(url.indexOf('x=')>=0){ url = url.substr(0,url.indexOf('x=')-1); }
			if(url.indexOf('?')>=0){ url += '&'; } else { url += '?'; }
			url += 'x=' + Math.floor(Math.random()*99999999);
		}
	
    ajaxObjects[index].open("POST", url, true); 
    ajaxObjects[index].setRequestHeader('Content-Type', 
      'application/x-www-form-urlencoded'); 

    ajaxObjects[index].onreadystatechange = function() 
    { 
      if (ajaxObjects[index].readyState == 3){
		if(interactivecallback) interactivecallback(id);
	  } else if (ajaxObjects[index].readyState == 4 && 
				ajaxObjects[index].status == 200) {
				if(ajaxEnabled&&!ajaxObjectsCancelled[index]) callback(ajaxObjects[index].responseText,url,id); 
				delete ajaxObjects[index];
				ajaxObjects[index] = null;
      } else if(ajaxObjects[index].readyState == 4){
		if(ajaxEnabled&&!ajaxObjectsCancelled[index]) servererrorcallback(ajaxObjects[index].status,data,url,id);  
	  }
    };

    ajaxObjects[index].send(data); 
  }
  return index;
}

function getNextAjaxRequest(){
	/*
		The purpose for retrieving this index before making the ajax request is because when IE caches the response,
		it doesn't run any other code until the response is returned and dealt with. Therefore, if we returned the index
		in the request function, IE would not receive it until after the response is dealt with and indexes are managed.
	*/
	return nextAjaxRequest;
}

function cancelAjaxRequest(index){
	ajaxObjectsCancelled[index] = true;
	if(ajaxObjects[index]){
		ajaxObjects[index].abort();
		delete ajaxObjects[index];
		ajaxObjects[index] = null;
	}
}

//============================================= Ajax History Manager =============================================//

// Variables
var ajaxHistoryDefaultHash;
var ajaxHistoryExpectedHash;
var ajaxHistoryMasterHash;
var ajaxHistoryIEHash;
var ajaxHistoryCallback;

// Functions
function ajaxHistoryInitiate(callback,def){
	if(isMSIE()) document.write('<iframe style="border: 0px; width: 1px; height: 1px; position: absolute; bottom: 0px; right: 0px; visibility: visible; display:none;" name="historyframe" id="historyframe" src="'+(String(window.location).indexOf("/game/")>=0?"../":"")+'client/blank.html"></iframe>');
	ajaxHistoryCallback = callback;
	ajaxHistoryDefaultHash = def;
	ajaxHistoryCheckHash();
}

function ajaxHistoryCheckHash(){
	if(!ajaxHistoryExpectedHash && window.location.hash!=''){
		ajaxHistoryMasterHash=window.location.hash.substr(1);
	} else if(!ajaxHistoryExpectedHash){
		ajaxHistoryMasterHash=ajaxHistoryDefaultHash;
	} else {
		if(isMSIE()) {
			if(ajaxHistoryIEHash != ajaxHistoryMasterHash && window.location.hash.substr(1)==ajaxHistoryMasterHash){
				ajaxHistoryMasterHash = ajaxHistoryIEHash;
			} else if(window.location.hash.substr(1)!=ajaxHistoryMasterHash){
				ajaxHistoryMasterHash = window.location.hash.substr(1);
			}
		} else {
			ajaxHistoryMasterHash = window.location.hash.substr(1);
		}
	}
	if(ajaxHistoryMasterHash){
		if(ajaxHistoryMasterHash!=ajaxHistoryExpectedHash){
			ajaxHistoryCallback(ajaxHistoryMasterHash);
		}
		setTimeout("ajaxHistoryCheckHash();",40);
	} else {
		window.history.go(-2);
	}
}

function ajaxHistoryAdd(hash){
	window.location.hash = hash;
	ajaxHistoryMasterHash = hash;
	ajaxHistoryExpectedHash = hash;
	ajaxHistoryIEHash = hash;
	if(isMSIE()) document.getElementById('historyframe').contentWindow.location = (String(window.location).indexOf("/game/")>=0?"../":"")+'client/blank.html?hash='+hash;
}

function ajaxHistoryGetCurrentHash(){
	return ajaxHistoryExpectedHash;
}

function isMSIE(){
	var userAgent = navigator.userAgent.toLowerCase();
	return document.all && userAgent.indexOf('msie')!=-1 ? true : false;
}

function escapeAjax(str){
	str = escape(str);
	while(str.indexOf('+')>=0) str = str.substr(0,str.indexOf('+'))+'%2B'+str.substr(str.indexOf('+')+1);
	return str;
}