var request = false;

// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function hideFadeBackground()
{
	objOverlay = document.getElementById('overlay');
	objHint = document.getElementById('hint');
	objOverlay.style.display = 'none';
	objHint.style.display = 'none';
	document.variables.disabled.value = 0;
	doRefresh();
}

function setRequest(option, serverurl) {
	if (window.XMLHttpRequest) {
		request = new XMLHttpRequest(); // Mozilla, Safari, Opera
	} else if (window.ActiveXObject) {
		try {
			request = new ActiveXObject('Msxml2.XMLHTTP'); // IE 5
		} catch (e) {
			try {
				request = new ActiveXObject('Microsoft.XMLHTTP'); // IE 6
			} catch (e) {}
		}
	}
	if (!request) {
		alert("Could not create a XMLHTTP instance! Ajax is not working.");
		return false;
	} else {
		working = true;
		request.open('post', 'scripts/php/get_serverlist_data.php', true);
		request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		request.send('option=' + option + '&serverurl=' + serverurl + '&<?php echo htmlspecialchars(SID); ?>');
		request.onreadystatechange = interpretRequest;
	}
}

function doRefresh(){
	if (document.variables.disabled.value == 0)
	{
		setRequest(0, 0);
		setTimeout('doRefresh();',10000);
	}
}

function DisableAjax(){
	document.variables.disabled.value = 1;
	var objOverlay = document.getElementById('overlay');
	var objHint = document.getElementById('hint');
	var arrayPageSize = getPageSize();
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';
	objHint.style.left = (arrayPageSize[2] / 2) - 150 + 'px';
	objHint.style.top = (arrayPageSize[3] / 2) - 100 + 'px';
	objHint.style.display = 'block';
}

function interpretRequest() {
	switch (request.readyState) {
		case 4:
			if (request.status != 200) {
				
			} else {
				var xmldoc	= request.responseXML;
				var option = xmldoc.getElementsByTagName('option').item(0);
				var content = xmldoc.getElementsByTagName('content').item(0);
				switch (option.firstChild.nodeValue) {
					case 'onlineservers':
						if (content.firstChild != null)
						{
							$('online_servers').innerHTML = content.firstChild.nodeValue;
							var timercount = xmldoc.getElementsByTagName('timercount').item(0).firstChild.nodeValue;
							for (var i = 0; i < timercount; i++){
								var timername = xmldoc.getElementsByTagName('timmername' + i).item(0).firstChild.nodeValue;
								var timerinterval = xmldoc.getElementsByTagName('timmerinterval' + i).item(0).firstChild.nodeValue;
								if (timerinterval > 0){
									new countdown(timerinterval, timername);
								} else {
									$(timername).innerHTML = 'over';
								}
							}
						}
						setRequest(3, 0);
						break;
					case 'serverusers':
						if (content.firstChild != null)
						{
							var url = '';
							if (xmldoc.getElementsByTagName('closewindow').item(0).firstChild.nodeValue == 'false'){
								url = xmldoc.getElementsByTagName('url').item(0).firstChild.nodeValue;
								$('server_users').innerHTML = content.firstChild.nodeValue;
								if (document.variables.serverusers.value == '')	{
									setPositionToMouse('server_users');
									swapZIndex($('server_users'), $('server_talk'));
								}
								showElement('server_users');
							} else {
								hideElement('server_users');				
							}						
							document.variables.serverusers.value = '1' + url;
						}
						if (document.variables.servertalk.value != '') setRequest(2, document.variables.servertalk.value);
						break;
					case 'servertalk':
						if (content.firstChild != null)
						{
							var url = '';
							if (xmldoc.getElementsByTagName('closewindow').item(0).firstChild.nodeValue == 'false'){
								url = xmldoc.getElementsByTagName('url').item(0).firstChild.nodeValue;
								$('server_talk').innerHTML = content.firstChild.nodeValue;
								if (document.variables.servertalk.value == ''){
									setPositionToMouse('server_talk');
									swapZIndex($('server_talk'), $('server_users'));
								}
								showElement('server_talk');
							} else {
								hideElement('server_talk');				
							}						
							document.variables.servertalk.value = '1' + url;
						}
						break;
					case 'runningservers':
						if (content.firstChild != null)
						{
							$('running_servers').innerHTML = content.firstChild.nodeValue;
						}
						if (document.variables.serverusers.value != '')  {
							setRequest(1, document.variables.serverusers.value);
						} else if (document.variables.servertalk.value != '') setRequest(2, document.variables.servertalk.value);
						break;
					default:
						break;
				}
			}
			break;
		default:
			break;
	}
}