var xmlDoc;
var currentQuote;

function prepCyclingText() {
	var xmlHttp;
	try	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				return false;
			}
		}
	}
	
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4)
		{
			xmlDoc = xmlHttp.responseXML;
			//alert(xmlHttp.responseText);	
			setTimeout('fadeCyclingText()',15000);
		}
	}
	
	xmlHttp.open("GET","/cycleText.xml",true);
	xmlHttp.send('');	
}

function fadeCyclingText() {	
	Effect.Fade('cyclingTextInner',{duration:2.0});
	//alert(0);
	setTimeout('changeCyclingText()',3000);
	//alert(1);
}

function changeCyclingText() {
	//alert(1);
	var quotes = xmlDoc.getElementsByTagName("quote");
	currentQuote = randomNumber(currentQuote,quotes.length);
	//alert(currentQuote);
	//Object.inspect(quotes);
	var contributor = "<nobr>" + quotes[currentQuote].getElementsByTagName('contributor')[0].firstChild.nodeValue + "</nobr>";
	//alert(quotes[currentQuote]);
	var theActualQuote = quotes[currentQuote].getElementsByTagName('text')[0].firstChild.nodeValue;
    //alert(3);
	var charLimit = 180;
	if(theActualQuote.length > charLimit) {		
		theActualQuote = theActualQuote.substring(0,charLimit);
		theActualQuote += ".... [More]";
	}
	//alert(4);
	//alert(theActualQuote);
	
	var newQuote = theActualQuote + " - " + contributor;
	
	document.getElementById('cyclingTextInner').innerHTML = newQuote;	 
	 
	Effect.Appear('cyclingTextInner',{duration:2.0});	
	setTimeout('fadeCyclingText()',15000);	
}

function randomNumber(current,maxValue) {
	var newNumber = current;
	while(newNumber == current) {
		newNumber = Math.floor(Math.random()*maxValue);
	}
	
	return newNumber;	
}