var currentHash;
if(!document.ajaxHistory) {
	document.ajaxHistory = new Array();
}
var httpReq = {'default': null};

function sendRequest(page, queryString, postData, callback, skipLoadStatus, channel, stopOnOverride) {
	if(!page)
    	page = 'view_controller.php';
	if(!channel)
		channel = 'default';
	
	if(stopOnOverride && httpReq[channel] && httpReq[channel].status) {
		return;
	} else if(httpReq[channel]) {
		httpReq[channel].abort();
	} else {
		httpReq[channel] = window.ActiveXObject ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
	}
    
    if(httpReq[channel]) {
    	if(!skipLoadStatus)
			setLoadStatus(true);
    	
        httpReq[channel].onreadystatechange = function() {processRequest(channel, callback, skipLoadStatus);};		
		// setup to handle GET or a POST, based on the data
		httpReq[channel].open((postData == null ? "GET" : "POST"), page+'?'+queryString+'&ajax=yes', true);
		if(postData && postData.length > 0) {			
			httpReq[channel].setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');			
		}
		
		httpReq[channel].send(postData);
    }
}

function processRequest(channel, callback, skipLoadStatus) {
	if(httpReq[channel] && httpReq[channel].readyState == 4) {
		if(!skipLoadStatus)
			setLoadStatus(false);
		
		if(httpReq[channel].status == 200) {
			eval(callback);
		} else if(httpReq[channel].status != 0 && typeof console != 'undefined' && console.log) {
			console.log('HTTP error: '+httpReq[channel].status);
			console.log(httpReq[channel].responseText);
		}
	}
}

function getPageDataDirect(url) {
	var req = window.ActiveXObject ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
	
	if(req) {
		req.open('GET', url, false);
		req.send(null);
		
		return req.responseText == null ? '' : req.responseText;
	}
}

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)), 'processEvalScripts();');
	}
}

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(location.hash == '#' && currentHash == '') {
		currentHash = '#';
	}

	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);
}

function evalScripts(data) {
	var index = data.indexOf('<script');
	var endIndex;
	
	while(index != -1) {
		index = data.indexOf('>', index) + 1;
		endIndex = data.indexOf('</script>', index);
		
		eval(data.substring(index, endIndex));
		
		index = data.indexOf('<script', endIndex);
	}
}

function processEval(channel) {
	if(!channel)
		channel = 'default';
		
	eval(httpReq[channel].responseText);
}

function processEvalScripts(channel) {
	if(!channel)
		channel = 'default';
		
	evalScripts(httpReq[channel].responseText);
}

currentHash = location.hash;
checkCurrentState();
