var currentHash;
if(!document.ajaxHistory) {
	document.ajaxHistory = new Array();
}
var httpReq;

function sendRequest(page, queryString, postData, callback, skipLoadStatus) {
	httpReq = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
    
    if(!page) {
    	page = 'view_controller.php';
    }
    
    if(httpReq) {
    	if(!skipLoadStatus)
			setLoadStatus(true);
    	
        httpReq.onreadystatechange = function() {processRequest(callback);};		
		// setup to handle GET or a POST, based on the data
		httpReq.open((postData == null ? "GET" : "POST"), page+'?'+queryString+'&ajax=yes', true);
		if(postData && postData.length > 0) {			
			httpReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");			
		}
		
		httpReq.send(postData);
    }
}

function processRequest(callback) {
	if(httpReq.readyState == 4 && httpReq.status == 200) {
		if(!isAuthPage(httpReq.responseText)) {
			eval(callback);
		}

		setLoadStatus(false);
    } else if (httpReq.readyState == 4 && httpReq.status != 200) {
		// oh noes, http error
		alert('HTTP error: '+httpReq.status);
		// NOTE: This console check looks perfectly valid to me - but it's throwing a Javascript error in FF. Switching to an alert for now.
		//if(console) {
		//	console.log(httpReq.responseText);
		//}
		alert(httpReq.responseText);
		setLoadStatus(false);
		return;
	}
}

function getPageDataDirect(url) {
	var req = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	
	if(req) {
		req.open("GET", url, false);
		req.send(null);
		
		if(isAuthPage(req.responseText)) {
			return null;
		}
		
		return req.responseText == null ? '' : req.responseText;
	}
}

function isAuthPage(data) {
	if(/!--isInAuth-->/.test(data)) {
		var loginSlipContent = document.getElementById('loginSlipContent');
		if(loginSlipContent) {
			loginSlipContent.innerHTML = httpReq.responseText;
			
			showSlip('login');
		}
		
		return true;
	} else {
		return false;
	}
}

function setCurrentState(data) {
	if(data['loginRequired'] == 'yes' && !document.isLoggedIn) {
		document.loginSavedAction = data;
		sendRequest('view_controller.php', 'what=getLoginSlip', null, 'processSlip();');
		
		return false;
	} else {
		if(document.ajaxHistory[location.hash]) {
			document.ajaxHistory[location.hash].scrollTop = window.scrollY ? window.scrollY : document.documentElement.scrollTop;
		}
		document.loginSavedAction = null;
		if(!data.skipSetHistory) {
			document.ajaxHistory[data.hash] = data;
		}
		location.hash = currentHash = data.hash;
		
		return true;
	}
}

function processCurrentState() {
	var currentHistory;
	
	if(location.hash == '' || location.hash == '#') {
		currentHistory = {page: location.pathname, query: '', callback: 'processLoadPage();'};
	} else {
		currentHistory = document.ajaxHistory[location.hash];
	}

	if(currentHistory) {
		if(Browser.Engine.trident) {
			var ieAjaxHistoryFrame = document.getElementById('ieAjaxHistoryFrame');
			
			if(ieAjaxHistoryFrame) {
				ieAjaxHistoryFrame.src = 'ie_ajax_history.html?'+escape(location.hash);
			}
		} else {
			var queryAdd = '&ajaxIndex='+escape(location.hash)+'&ajaxData=';
			for(var i in currentHistory) {
				if((typeof currentHistory[i]) == 'function') {
					continue;
				}
	
				queryAdd += escape(i+'=;='+currentHistory[i]+':-:');
			}
			
			sendRequest(currentHistory.page, currentHistory.query+queryAdd, currentHistory.postData, currentHistory.callback);
		}
	} else {
		sendRequest('view_controller.php', 'what=getAjaxHistory', 'hash='+escape(location.hash.substr(1)), 'eval(httpReq.responseText);');
	}
}

document.processCurrentStateFromIE = function(hash) {
	if(document.ieAjaxFirstRun) {
		document.ieAjaxFirstRun = false;
		return;
	}

	var currentHistory;
	
	if(hash != '') {
		hash = hash.substr(1);
		hash = unescape(hash);
	}
	
	if(hash == '' || hash == '#') {
		currentHistory = {page: location.pathname, query: '', callback: 'processLoadPage();'};
	} else {
		currentHistory = document.ajaxHistory[hash];
	}
	
	if(currentHistory) {
		var queryAdd = '&ajaxIndex='+escape(location.hash)+'&ajaxData=';
		for(var i in currentHistory) {
			if((typeof currentHistory[i]) == 'function') {
				continue;
			}

			queryAdd += escape(i+'=;='+currentHistory[i]+':-:');
		}
		
		location.hash = hash;
		
		sendRequest(currentHistory.page, currentHistory.query+queryAdd, currentHistory.postData, currentHistory.callback);
	}
}

function checkCurrentState() {
	if(currentHash != location.hash) {
		currentHash = location.hash;
		processCurrentState();
	}
	
	setTimeout('checkCurrentState();', 250);
}

function checkJS(what, where) {
	var needLoad;
	eval('needLoad = !document.'+what+';');
	if(needLoad)
		evalJSFrom(where);
}

function evalJSFrom(url) {
	var data = getPageDataDirect(url);
	
	if(data != null)
		eval(data);
}

currentHash = location.hash;