var updateInterval = 600; //in seconds

var updateTimeout = new Array();

function getHTTPObject() {
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				xmlhttp = false;
			}
		}
	@else
	xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}

var http = getHTTPObject(); // We create the HTTP Object

function handleHttpResponse(uid) {
	if (http[uid].readyState == 4) {
		//parse response
		var response = http[uid].responseText.replace(/[^\d]/gi, "");
		
		//make sure there is a response here
		if(!response || response == "") {
			return;
		}
		
		//set status based on response
		if(response == "1") {
			toggleTDStatus(uid, true);
		}
		else {
			toggleTDStatus(uid, false);
		}
		
		//set to update this status later
		updateTimeout[uid] = setTimeout("requestStatusFromServer('" + uid + "')", updateInterval * 1000);
	}
}

function toggleTDStatus(uid, online) {
	var tdEls = document.getElementsByTagName("td");
	//search through all TDs for the ones we need to change
	for(var i = 0; i < tdEls.length; i++) {
		if(tdEls[i].id == "onlineTD_" + uid) {
			if(online) {
				tdEls[i].className = uid == userboxUid ? "onlineUser" : "online";
			}
			else {
				tdEls[i].className = "offline";
			}
		}
	}
}

function requestStatusFromServer(uid) {
	//create the http object if needed
	if(!http[uid])
		http[uid] = getHTTPObject();
		
	//make sure the status for this id is not already being retreived
	if(http[uid].readyState != 0 && http[uid].readyState != 4) //if not uninitialized or complete, we're already getting this status
		return;
		
	//get the status for this uid
	http[uid].open("GET", "http://www.picturetrail.com/online/getOnline.cgi?id=" + uid + "&time="+new Date().getTime(), true);
	http[uid].onreadystatechange = function() {
		handleHttpResponse(uid);
	};
	http[uid].send(null);
}

function setStatusTimeouts(uid) {
	//set timeouts to get status of these
	if(typeof uid == "string") { //uid passed as a string
		requestStatusFromServer(uid);
		updateTimeout[uid] = setTimeout("requestStatusFromServer('" + uid + "')", updateInterval * 1000);
	}
	else if(uid.length > 0) { //uid passed as an array of uid's
		for(var i = 0; i < uid.length; i++) {
			requestStatusFromServer(uid[i]);
			updateTimeout[uid[i]] = setTimeout("requestStatusFromServer('" + uid[i] + "')", updateInterval * 1000);
		}
	}
}

function init() {
	if(http) { //check if ajax is working
		//scrape the uid's we need to check
		uidAry = new Array();
		var pageTDs = document.getElementsByTagName("td");
		for(var i = 0; i < pageTDs.length; i++) {
			if(pageTDs[i].id.indexOf("onlineTD_") != -1) {
				var tempID = pageTDs[i].id.substring(9);
				//check for duplicates
				var dupeFound = false;
				for(var j = 0; j < uidAry.length; j++) {
					if(uidAry[j] == tempID) {
						dupeFound = true;
					}
				}
				if(!dupeFound) {
					uidAry[uidAry.length] = tempID;
				}
			}
		}

		//make http into an array of ajax objects
		http = null;
		http = new Array();
		
		//set timeout to update uid online status later
		setStatusTimeouts(uidAry);
	}
}