/*
    COPYRIGHT Phrazit, INC
    ALL RIGHTS RESERVED, 2008
*/

var ticker = getEl('randomticker');
ticker.style.left = '0px';
ticker.style.width = '823px';
var supportsWidth = 0;
var widthPerLoad = 823*2;
var loadedOnce = false;
var speed = 3;
var timer,timer2;
var runningTime = 0;

function loadRandomWords()
{
	if(runningTime > 60*5)
	{
		clearTimeout(timer);
		clearTimeout(timer2);
		return;
	}
	if(supportsWidth <= (widthPerLoad/2 - parseInt(ticker.style.left)))
	{
		AJAXRequestGET('backend/randomtopics.php',incomingWords);
	}
	runningTime += 1;
	timer = setTimeout("loadRandomWords()",2000);
}

function incomingWords()
{
	var HTML = '';
	var nodes = AJAXResponseXML().getElementsByTagName('topic');
	for (var i = 0; i < nodes.length; i++)
	{
		var id = nodes[i].getElementsByTagName('id')[0].firstChild.nodeValue;
		var title = nodes[i].getElementsByTagName('title')[0].firstChild.nodeValue;
		HTML += "<a href='http://www.PhrazIt.com/"+title.replace(" ", "_")+"'>"+title+"</a> &bull; ";		
	}
	if(loadedOnce) ticker.innerHTML += HTML;
	else
	{
		ticker.innerHTML = HTML;
		loadedOnce = true;
		slideTicker();
	}
	supportsWidth += widthPerLoad;
	ticker.style.width = (parseInt(ticker.style.width)+widthPerLoad) + 'px';

}

function slideTicker()
{
	if(slide) ticker.style.left = (parseInt(ticker.style.left)-speed) + 'px';
	timer2 = setTimeout("slideTicker()",75);
}

var slide = true;
function deactivateSlide() { slide = false; }
function activateSlide() { slide = true; }

$('body').ready(loadRandomWords);

