var spymax;
var play = 1;
var whats = new Array();
var titles = new Array();
var previews = new Array();
var whos = new Array();
var threadids = new Array();
var postids = new Array();
var userids = new Array();
var spyids = new Array();
var forumids = new Array();
var forumnames = new Array();
var highestid = 0;
var fadetime = .25;
var pushtime = 1500;
var fadetime_end = 1.5;
var pushtime_end = 4000
var initalfill_flag = 0;

function init() {

	if (is_ie) { // IE is dog crap
		// First run, let's add cells to rows
		for (i = spymax; i >= 1; i--){
			document.getElementById("spy_table").rows[i].insertCell();
			document.getElementById("spy_table").rows[i].insertCell();
			document.getElementById("spy_table").rows[i].insertCell();
//			document.getElementById("spy_table").style.background = '#FF0000';
			
			if (i + 5 > spymax) {
				document.getElementById("spy_table").rows[i].cells[0].style.filter = 'alpha(opacity=' + (((spymax - i) * 20) + 5) + ')';
				document.getElementById("spy_table").rows[i].cells[1].style.filter = 'alpha(opacity=' + (((spymax - i) * 20) + 5) + ')';
				document.getElementById("spy_table").rows[i].cells[2].style.filter = 'alpha(opacity=' + (((spymax - i) * 20) + 5) + ')';
//				document.getElementById("spy_table").rows[i].className = "alt1";
//				document.getElementById("spy_table").rows[i].style.background = '#EAF4E9';
//				document.getElementById("spy_table").rows[i].cells[1].style.display = "none";
//				document.getElementById("spy_table").rows[i].cells[2].style.display = "none";
			}
		}
	}

	getXML();
}

function pauseSpy() {
	var playimg = document.getElementById("vsplay");
	var pauseimg = document.getElementById("vspause");

	playimg.src = "images/misc/play_up.gif";
	playimg.alt = "Click to Play";
	pauseimg.src = "images/misc/pause_down.gif";
	pauseimg.alt = "Paused...";

	pause();
}

function playSpy() {
	var playimg = document.getElementById("vsplay");
	var pauseimg = document.getElementById("vspause");

	playimg.src = "images/misc/play_down.gif";
	playimg.alt = "Playing...";
	pauseimg.src = "images/misc/pause_up.gif";
	pauseimg.alt = "Click to Pause";

	resume();
}

function pause() {
	play = 0;
}

function resume() {
	play = 1;
}

function getXML() {

	if (highestid > 0) {
		var querystring = "do=xml&last=" + highestid;
	} else {
		var querystring = "do=xml";
	}

	if (document.getElementById("subscribed_flag").checked) {
		querystring = querystring + "&subscribed=1";
	}
	querystring = querystring + '&r=' + Math.random(); 
		
	var url = "spy.php";
	var myAjax = null;     

	myAjax = new Ajax.Request(
	url,
		{
			method: 'get',
			asynchronous: true,
			parameters: querystring,
			onComplete: processXML
		});
}

function processXML(request) {
	var events;
	var event;

	try {
		events = request.responseXML.getElementsByTagName("events")[0];
		event = events.getElementsByTagName("event");
	}
	catch (e) {
		setTimeout("getXML()", 30000);
		return;
	}

	for (i = 0; i < event.length; i++) {
		try {
			whats[i] = event[i].getElementsByTagName("what")[0].firstChild.nodeValue;
		}
		catch (e) {
			whats[i] = "Who Knows";
		}

		try {
			titles[i] = event[i].getElementsByTagName("title")[0].firstChild.nodeValue;
		}
		catch (e) {
			titles[i] = "";
		}
			
		try {
			previews[i] = event[i].getElementsByTagName("preview")[0].firstChild.nodeValue;
		}
		catch (e) {
			previews[i] = "";
		}

		try {
			whos[i] = event[i].getElementsByTagName("who")[0].firstChild.nodeValue;
		}
		catch (e) {
			whos[i] = "Anonymous";
		}
		
		try {
			threadids[i] = event[i].getElementsByTagName("threadid")[0].firstChild.nodeValue;
		}
		catch (e) {
			threadids[i] = "";
		}
		
		try {
			postids[i] = event[i].getElementsByTagName("postid")[0].firstChild.nodeValue;
		}
		catch (e) {
			postids[i] = "";
		}

		try {
			userids[i] = event[i].getElementsByTagName("userid")[0].firstChild.nodeValue;
		}
		catch (e) {
			userids[i] = "";
		}

		try {
			spyids[i] = event[i].getElementsByTagName("id")[0].firstChild.nodeValue;
		}
		catch (e) {
			spyids[i] = 1;
		}

		try {
			forumids[i] = event[i].getElementsByTagName("forumid")[0].firstChild.nodeValue;
		}
		catch (e) {
			forumids[i] = 1;
		}

		try {
			forumnames[i] = event[i].getElementsByTagName("forumname")[0].firstChild.nodeValue;
		}
		catch (e) {
			forumnames[i] = 1;
		}
	}

	push();
}

function push() {

	if (play == 0) {
		setTimeout("push()", 1000);
		return;
	}

	var cell;
	var cellnext;
	var text;
	var clip;
	var who_clip = '';
	var where = '';
	var post_url;
	var style = "";
	var what = whats.pop();
	var title = titles.pop();
	var preview = previews.pop();
	var who = whos.pop();
	var threadid = threadids.pop();
	var postid = postids.pop();
	var userid = userids.pop();
	var spyid = spyids.pop();
	var forumid = forumids.pop();
	var forumname = forumnames.pop();

	highestid = Math.max(parseInt(spyid), highestid);

	if (vB_PHP_Emulator.prototype.stripos(what, 'Thread')) {
		post_url = 'showthread.php?t=' + threadid;
	} else {
		post_url = 'showthread.php?p=' + postid + '#post' + postid ;
	}

	
	if (what == 'New User') {
		clip = '<strong>New User Registration</strong>';
	} else if(title.length == 0) { // Why??
		clip = '<strong>Unknown</strong>';
	} else {
		clip = '<strong><a href="' + post_url + '">' + title + '</a></strong>';
	}
	if (preview.length > 0) clip = clip + '<br /><div class=\"smallfont\">' + preview + '</div>';
	
	if (userid.length > 0) {
		who_clip = 'by <a href="member.php?u=' + userid + '">' + who + '</a>';
	}
	
	if (parseInt(forumname.length) > 0) {
		if (who_clip.length > 0) where = '<br />';
		where = where + '<strong><a href="forumdisplay.php?f=' + forumid + '">' + forumname + '</a></strong>';
	} else {
		where = '';
	}

	text = '<td class="alt1"><div class="smallfont"><strong>' + what + '</strong></div></td><td class="alt2"><div class="smallfont">' + who_clip + where + '</div></td><td class="alt1Active">' + clip + "</td>";


	Element.setOpacity('row1', 0.0);

	if (is_ie) { // Because IE is a big pile of dog crap
	
		var split1 = vB_PHP_Emulator.prototype.stripos(text, '>') + 1;
		var split2 = vB_PHP_Emulator.prototype.stripos(text, '</td>');
		var split3 = vB_PHP_Emulator.prototype.stripos(text, '>', split2 + 5) + 1;
		var split4 = vB_PHP_Emulator.prototype.stripos(text, '</td>', split3 + 1);
		var split5 = vB_PHP_Emulator.prototype.stripos(text, '>', split4 + 5) + 1;
		var split6 = vB_PHP_Emulator.prototype.stripos(text, '</td>', split5 + 1);
		
		for (i = (spymax - 1); i >= 1; i--) {
			cell = document.getElementById("spy_table").rows[i];
			cellnext = document.getElementById("spy_table").rows[i + 1];
			if (cell.innerHTML.length > 31) {
				cellnext.cells[0].innerHTML = cell.cells[0].innerHTML;
				cellnext.cells[1].innerHTML = cell.cells[1].innerHTML;
				cellnext.cells[2].innerHTML = cell.cells[2].innerHTML;
				cellnext.style.display = '';
			}
		}

		document.getElementById("row1").cells[0].innerHTML = text.substring(split1, split2);
		document.getElementById("row1").cells[1].innerHTML = text.substring(split3, split4);
		document.getElementById("row1").cells[2].innerHTML = text.substring(split5, split6);
		
	} else {
		for (i = (spymax - 1); i >= 1; i--) {
			cell = document.getElementById("row" + i);
			cellnext = document.getElementById("row" + (i + 1));
			if (cell.innerHTML != "") {
				cellnext.innerHTML = cell.innerHTML;
				cellnext.style.display = '';
			}
		}
		document.getElementById("row1").innerHTML = text;
	}

	Effect.Appear('row1', { duration: fadetime });

	if (whats.length > 0) {
		setTimeout("push()", pushtime);
	}
	else {
		fadetime = fadetime_end;
		pushtime = pushtime_end;
		setTimeout("getXML()", 5000);
	}
}