
var play = 1;
var highestid = 0;
var highestattid = 0;
var no_offtopic = 0;

var whats = new Array();
var whens = new Array();
var titles = new Array();
var previews = new Array();
var posters = new Array();
var threadids = new Array();
var postids = new Array();
var lastposts = new Array();
var userids = new Array();
var forumids = new Array();
var forumnames = new Array();
var attids = new Array();
var filenames = new Array();
var filesizes = new Array();
var descriptions = new Array();
var apostids = new Array();
var counters = new Array();
var thumbnails = new Array();
var rowClass = 'alt1';

/* Effect delay of fade-in */
var fadetime = 1;
var imgfadetime = 2;

var processXML = function(request) {
	var events;
	var event;

	try {
		events = request.responseXML.getElementsByTagName("events")[0];
		event = events.getElementsByTagName("event");
		if (event.length == 0) {
			setTimeout("getXML()", xmldelay);
			return;
		}
	}
	catch (e) {
		setTimeout("getXML()", xmldelay);
		return;
	}

	for (i = 0; i < event.length; i++) {
		try {
			whats[i] = event[i].getElementsByTagName("what")[0].firstChild.nodeValue;
		}
		catch (e) {
			whats[i] = "Unknown";
		}

		try {
			whens[i] = event[i].getElementsByTagName("when")[0].firstChild.nodeValue;
		}
		catch (e) {
			whens[i] = "";
		}

		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 {
			posters[i] = event[i].getElementsByTagName("poster")[0].firstChild.nodeValue;
		}
		catch (e) {
			posters[i] = "Guest";
		}

		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 {
			lastposts[i] = event[i].getElementsByTagName("lastpost")[0].firstChild.nodeValue;
		}
		catch (e) {
			lastposts[i] = "";
		}

		try {
			userids[i] = event[i].getElementsByTagName("userid")[0].firstChild.nodeValue;
		}
		catch (e) {
			userids[i] = "";
		}

		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();
}


var processAttachmentXML = function(request) {
	var attachments;
	var attachment;

	try {
		attachments = request.responseXML.getElementsByTagName("attachments")[0];
		attachment = attachments.getElementsByTagName("attachment");
		if (attachment.length == 0) {
			setTimeout("getAttachmentXML()", attachmentdelay);
			return;
		}
	}
	catch (e) {
		setTimeout("getAttachmentXML()", attachmentdelay);
		return;
	}

	for (i = 0; i < attachment.length; i++) {
		try {
			attids[i] = attachment[i].getElementsByTagName("attid")[0].firstChild.nodeValue;
		}
		catch (e) {
			attids[i] = "0";
		}

		try {
			filenames[i] = attachment[i].getElementsByTagName("filename")[0].firstChild.nodeValue;
		}
		catch (e) {
			filenames[i] = "";
		}

		try {
			filesizes[i] = attachment[i].getElementsByTagName("filesize")[0].firstChild.nodeValue;
		}
		catch (e) {
			filesizes[i] = "";
		}

		try {
			descriptions[i] = attachment[i].getElementsByTagName("description")[0].firstChild.nodeValue;
		}
		catch (e) {
			descriptions[i] = "";
		}

		try {
			apostids[i] = attachment[i].getElementsByTagName("postid")[0].firstChild.nodeValue;
		}
		catch (e) {
			apostids[i] = "0";
		}

		try {
			counters[i] = attachment[i].getElementsByTagName("counter")[0].firstChild.nodeValue;
		}
		catch (e) {
			counters[i] = "0";
		}

		try {
			thumbnails[i] = attachment[i].getElementsByTagName("thumbnail")[0].firstChild.nodeValue;
		}
		catch (e) {
			thumbnails[i] = "";
		}
	}

	pushatts();
}

function pushatts() {
	if (play == 0 || attids.length == 0) {
		setTimeout("getAttachmentXML()", attachmentdelay);
		return;
	}

	var cell;
	var text;

	var attid = attids.shift();
	var filename = filenames.shift();
	var filesize = filesizes.shift();
	var description = descriptions.shift();
	var postid = apostids.shift();
	var counter = counters.shift();
	var thumbnail = thumbnails.shift();

	YAHOO.util.Dom.setStyle('acell1', 'opacity', 0.0); 

	highestattid = Math.max(parseInt(attid), highestattid);

	text = '<div class="content">' +
		'<span class="corners-top"><span></span></span>' +
		'<div style="text-align:center"><a href="attachment.php?attachmentid=' + attid + '">' +
		'<img class="thumbnail" src="' + thumbnail + '" border="0" alt="' + description + '" /></a></div>' +
		'<div class="smallfont">' + description + '</div>' +
		'<span class="smallfont">' + filesize + '&middot; Views: ' + counter + '</span>' +
		'</div>';

	for (i = (attmax - 1); i >= 1; i--) {
		cell = document.getElementById("acell" + i);
		cellnext = document.getElementById("acell" + (i + 1));
		if (cell.innerHTML != "") {
			cellnext.innerHTML = cell.innerHTML;
			cellnext.style.display = '';
		}
	}
	document.getElementById("acell1").innerHTML = text;

	var anim = new YAHOO.util.Anim( 'acell1', { opacity: { to: 1 } }, imgfadetime, YAHOO.util.Easing.easeOut);
	anim.animate();

	if (attids.length > 0) {
		setTimeout("pushatts()", pushtime);
	} else {
		setTimeout("getAttachmentXML()", attachmentdelay);
	}
}


function push() {
	if (play == 0 || whats.length == 0) {
		setTimeout("push()", pushtime);
		return;
	}

	var cell;
	var cellnext;
	var text;
	var clip;
	var poster_clip = '';
	var where = '';
	var post_url;

	var what = whats.shift();
	var when = whens.shift();
	var title = titles.shift();
	var preview = previews.shift();
	var poster = posters.shift();
	var threadid = threadids.shift();
	var postid = postids.shift();
	var userid = userids.shift();
	var forumid = forumids.shift();
	var forumname = forumnames.shift();
	var lastpost = lastposts.shift();

	YAHOO.util.Dom.setStyle('row1', 'opacity', 0.0); 

	rowClass = rowClass == 'alt2' ? 'alt1' : 'alt2';
	highestid = Math.max(parseInt(postid), highestid);

	if (what == 'New 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) {
		clip = '<strong>Unknown</strong>';
	} else {
		clip = '<strong><a href="' + post_url + '" target="_blank">' + title + '</a></strong>';
	}

	if (preview.length > 0) {
	    clip = clip + '<div>' + preview + '</div>';
	}
	clip = '<div class="smallfont">' + clip + '</div>';

	if (userid.length > 0) {
		poster_clip = 'by <a href="member.php?u=' + userid + '" target="_blank">' + poster + '</a>';
	}

	if (parseInt(forumname.length) > 0) {
		if (poster_clip.length > 0) where = '<br />';
		where = where + 'in <strong><a href="forumdisplay.php?f=' + forumid + '" target="_blank">' + forumname + '</a></strong>';
	} else {
		where = '';
	}
	poster_where = '<div class="smallfont">' + poster_clip + where + '</div>';
	what = '<div class="smallfont">' + what + '</div>';

    text = '<td class="' + rowClass + '" align="center" height="55">' + what + '</td><td class="' + rowClass + '" height="55">' + poster_where + '</td><td class="' + rowClass + '" height="55">' + clip + '</td>';

	if (is_ie) {
		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 = what;
		document.getElementById("row1").cells[1].innerHTML = poster_where;
		document.getElementById("row1").cells[2].innerHTML = clip;
	} 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;
	}

	var anim = new YAHOO.util.Anim( 'row1', { opacity: { to: 1 } }, fadetime, YAHOO.util.Easing.easeOut);
	anim.animate();

	if (whats.length > 0) {
		setTimeout("push()", pushtime);
	} else {
		setTimeout("getXML()", xmldelay);
	}
}

var XMLcallback =
{
	success: processXML, 
	failure: function(o) {}, 
	argument: ['foo']
}

var attachmentXMLcallback =
{
	success:processAttachmentXML, 
	failure:function(o) {}, 
	argument:[]
}

function getXML() {
	if (play == 0) {
		setTimeout("getXML()", pushtime);
		return;
	}

	if (highestid > 0) {
		var querystring = "last=" + highestid;
		if (no_offtopic > 0) {
			querystring = querystring + "&no=1";
		}
	} else {
		return;
	}

	// querystring = querystring + '&r=' + Math.random();

    if (document.getElementById("subscribe")) {
        if (document.getElementById("subscribe").checked) { querystring = querystring + "&subscribe=1"; }
    }

	var sURL = "spyxml.php?" + querystring;

	var myAjax = YAHOO.util.Connect.asyncRequest('GET', sURL, XMLcallback, null); 
}

function getAttachmentXML() {
	if (play == 0) {
		setTimeout("getAttachmentXML()", pushtime);
		return;
	}

	if (highestattid > 0) {
		var querystring = "alast=" + highestattid;
	} else {
		return;
	}

	var sURL = "spyxml.php?" + querystring;

	var myAjax = YAHOO.util.Connect.asyncRequest('GET', sURL, attachmentXMLcallback, null); 
}

function spyinit() {
	getXML();
	getAttachmentXML();
}


