var actionAjaxWaiting = false;
var actionCounter = 0;
var actionTimeStamp = 0;
var actionQueue = new Array(0,'');
var actionInterval = 0;
var actionWaitingInterval = 0;

if(typeof ajax != 'function') {
	var noXML = false;
	if( !window.XMLHttpRequest ) XMLHttpRequest = function() {
		try{ return new ActiveXObject("MSXML3.XMLHTTP") }catch(e){}
		try{ return new ActiveXObject("MSXML2.XMLHTTP.3.0") }catch(e){}
		try{ return new ActiveXObject("Msxml2.XMLHTTP") }catch(e){}
		try{ return new ActiveXObject("Microsoft.XMLHTTP") }catch(e){}
		noXML = true;
	};

	function ajax(url,handler) {
		var request = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
		if(noXML) return true;
		request.open("GET", url, true);
		request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	 
		request.onreadystatechange = function() {
			if (request.readyState == 4 && request.status == 200) {
				if (request.responseText) {
					eval(handler+'(request.responseText);');
				}
			}
		};
		request.send(null);
		return false;
	}
}

function getActions() {
	if(!actionAjaxWaiting) { // only get new action if not already waiting for one. Prevents a pile up for slow connections.
		actionAjaxWaiting = true;
		actionWaitingInterval = setInterval("actionAjaxWaiting = false", 12000); // 12 second timeout before trying again for slow connections
		ajax("actions.ajax.php?ts="+actionTimeStamp, "actionHandler");
	}
}

function actionHandler(txt) {
	actions = txt.split('|',2);
	actionTimeStamp = actions[0];
	actionQueue = actions[1].split('<<<<<<<<<<');
	actionCounter = 0;
	
	actionAjaxWaiting = false;
	clearInterval(actionWaitingInterval);
	actionWaitingInterval = 0;
	clearInterval(actionInterval);
	actionInterval = 0;
	
	// no new actions
	if(actionQueue[0] == 'X') {
		actionInterval = setInterval("getActions()", 3000);
	} else {
		changeAction();
	}
}

function changeAction() {
	if(actionInterval == 0)	actionInterval = setInterval("changeAction()", 3000);
	
	if(actionCounter >= actionQueue.length) {
		clearInterval(actionInterval);
		actionInterval = 0;
		getActions();
	} else {	
		document.getElementById('actions').innerHTML = '<marquee behavior="slide" direction="up" width="100%" height="1.1em" style="height: 1.1em; text-align: center; overflow: hidden">'+actionQueue[actionCounter]+'</marquee>';
		actionCounter++;
	}
}

window.onload=function() {
	getActions();
}
