/*  Prototype JavaScript framework, version 1.4.0
 *  (c) 2005 Sam Stephenson <sam@conio.net>
 *
 *  Prototype is freely distributable under the terms of an MIT-style license.
 *  For details, see the Prototype web site: http://prototype.conio.net/
 *
 *  Modified for vBulletin use.
/*--------------------------------------------------------------------------*/

var spymax;
var highestid;
var play = 1;

var whats = new Array();
var titles = new Array();
var links = new Array();
var descriptions = new Array();
var pubdates = new Array();
var guids = new Array();
var authors = new Array();
var images = new Array();
var previews = new Array();
var posters = 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 views = new Array();
var replies = new Array();
var whens = new Array();
var lastposts = new Array();
var statusicons = new Array();
var rowClass = 'alt1';
var count=0;

var is_ie='';
/* Effect delay of fade-in */
var fadetime = .25;
var fadetime = 1.00;

/* Time delay between updates to table while scrolling */
var pushtime = 1000;

/*--

This must include both slashes: i.e.

var vburl='/forum/';

Use this is you integrate outside /forum with a portal or other product

*/
var vburl = '';

/* Delay between polls to the XML feed for new updates to page */
//var xmldelay = 7000;
var xmldelay = 15000;

function spyinit() {
	getXML();
}

function getXML() {
	if (highestid > 0) {
		var querystring = "do=xml&last=" + highestid;
		var querystring2 = 'do: "xml", last: "'+highestid +'"';
	} else {
		var querystring = "do=xml";
		var querystring2 = 'do: "xml"';
	}

	querystring = querystring + '&r=' + Math.random();
	querystring2 = querystring2 + ', r: "' + Math.random() + '"';
	var math = Math.random();
	if (term)
	{
		querystring = querystring + '&a=' + term;
		querystring2 = querystring2 + ', a: "' + term + '"';
	}

    if (document.getElementById("subscribe")) {
        if (document.getElementById("subscribe").checked) { querystring = querystring + "&subscribe=1"; }
    }

	var url = vburl + "index.php";
/*
	var myAjax = null;
//alert(querystring);

	myAjax = new Ajax.Request(
	url,
		{
			method: 'get',
			asynchronous: true,
			parameters: querystring,
			onComplete: processXML
		});
//alert(querystring2);
$.get(url, { do: "xml", last: highestid, r: math, a: term },
   function(data){
	//alert(data);
     processXML(data);
   }
  );
*/
$.ajax({
    url: url + '?' + querystring,
    type: 'GET',
    dataType: 'xml',
    timeout: 1000,
    error: function(){
        processXML('<?xml version="1.0" encoding="ISO-8859-1"?><events />');
    },
    success: function(xml){
	processXML(xml);
    }
});


}

function processXML(request) {
//alert(request);
	var events;
	var event;

var i=0;
$(request).find("event").each(function()
  {
	titles[i]=$(this).find("title").text();
	links[i]=$(this).find("link").text();
	descriptions[i]=$(this).find("description").text();
	pubdates[i]=$(this).find("pubdate").text();
	guids[i]=$(this).find("guid").text();
	authors[i]=$(this).find("author").text();
	images[i]=$(this).find("image").text();
	spyids[i]=$(this).find("spyid").text();
	whats[i] = 'X';

	i++;
});

/*
	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 {
			titles[i] = event[i].getElementsByTagName("title")[0].firstChild.nodeValue;
		}
		catch (e) {
			titles[i] = "";
		}

		try {
			links[i] = event[i].getElementsByTagName("link")[0].firstChild.nodeValue;
		}
		catch (e) {
			links[i] = "";
		}

		try {
			descriptions[i] = event[i].getElementsByTagName("description")[0].firstChild.nodeValue;
		}
		catch (e) {
			descriptions[i] = "Guest";
		}

		try {
			pubdates[i] = event[i].getElementsByTagName("pubdate")[0].firstChild.nodeValue;
		}
		catch (e) {
			pubdates[i] = "";
		}

		try {
			guids[i] = event[i].getElementsByTagName("guid")[0].firstChild.nodeValue;
		}
		catch (e) {
			guids[i] = "";
		}

		try {
			authors[i] = event[i].getElementsByTagName("author")[0].firstChild.nodeValue;
		}
		catch (e) {
			authors[i] = "";
		}

		try {
			images[i] = event[i].getElementsByTagName("image")[0].firstChild.nodeValue;
		}
		catch (e) {
			images[i] = "";
		}

		try {
                        spyids[i] = event[i].getElementsByTagName("spyid")[0].firstChild.nodeValue;
                }
                catch (e) {
                        spyids[i] = "";
                }

		whats[i] = 'X';

alert(titles[i]);
	}
*/

	count++;
	if (count==60)
	{
		//location.reload(); 
		history.go(0)
	}
	if (whats.length > 0)
	{
		push();
	}
	else
	{
		setTimeout("getXML()", xmldelay);
	}
}

function push() {
	if (play == 0) {
		setTimeout("push()", 1000);
		return;
	}

	var cell;
	var cellnext;
	var text;
	var clip;
	var user_clip;
	var image_clip;
	var poster_clip = '';
	var where = '';
	var post_url;
	var style = '';
	var is_opera = '';
	var is_ie = '';

	var title = titles.pop();
	var link = links.pop();
	var description = descriptions.pop();
	var pubdate = pubdates.pop();
	var guid = guids.pop();
	var author = authors.pop();
	var image = images.pop();
	var what = whats.pop();
	var spyid = spyids.pop();

	rowClass = rowClass == 'alt2' ? 'alt1' : 'alt2';
	highestid = Math.max(parseInt(spyid), highestid);
//alert(highestid);

	if(description.length == 0) {
		clip = '<strong>Unknown</strong>';
	} else {
		clip = description;
	}

	if (image.length > 0) {
	    image_clip = '<img src="' + image + '" width="50" height="50">';
	}

	if (author.length > 0) {
		user_clip = author;
	}

	if (pubdate.length > 0) {
		pub_clip = pubdate;
	}

    if (is_opera) {
        text = '<table cellpadding="3" cellspacing="0" border="0" width="100%"><tr><td width="20" class="' + rowClass + ' smallfont" nowrap="nowrap" align="center">' + what + '</td><td class="' + rowClass + ' smallfont" width="225" nowrap="nowrap" align="left">' + poster_clip + '</td><td class="' + rowClass + ' smallfont" width="80%">' + clip + '</td><td class="' + rowClass + ' smallfont" width="200" nowrap="nowrap" align="center">' + where + '</td></tr></table>';
	} else {
	    text = '<td class="image">' + image_clip + '</td><td class="name">' + user_clip + '</td><td class="tweet">' + clip + '<div class="pubdate">' + pub_clip + '</div></td>';
    }

	//Element.setOpacity('row1', 0.0);
	$("#row1").fadeTo("fast", 0.0);

	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.cells[3].innerHTML = cell.cells[3].innerHTML;
				cellnext.style.display = '';
			}
		}

		document.getElementById("row1").cells[0].innerHTML = '<div class="smallfont">' + what + '</div>';
		document.getElementById("row1").cells[1].innerHTML = '<div class="smallfont">' + poster_clip + '</div>';
		document.getElementById("row1").cells[2].innerHTML = '<div class="smallfont">' + clip + '</div>';
		document.getElementById("row1").cells[3].innerHTML = '<div class="smallfont">' + where + '</div>';
	} 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 });
	$("#row1").fadeTo("slow", 1.0);
	//$('#row1').fadeIn();

	if (whats.length > 0) {
		setTimeout("push()", pushtime);
	}
	else {
		setTimeout("getXML()", xmldelay);
	}
}


function print_r(theObj){
var x;
  if(theObj.constructor == Array ||
     theObj.constructor == Object){
    x+="<ul>";
    for(var p in theObj){
      if(theObj[p].constructor == Array||
         theObj[p].constructor == Object){
	x+="<li>["+p+"] => "+typeof(theObj)+"</li>";
        x+="<ul>";
        print_r(theObj[p]);
        x+="</ul>";
      } else {
	x+="<li>["+p+"] => "+theObj[p]+"</li>";
      }
    }
    x+="</ul>";
  }
alert(x);
}

