//
// $Id$
//
// Site javascript code
//

//google.load("jquery", "1");
google.setOnLoadCallback(quote_fader);

//
// xml quote fetcher/fader
//

var qId;
var	qText;
var qBy;
var qWordCount;
var qTimer;
var stepdir = '+';
var dirty = false;

function quote_fader() {
	if (!dirty) {
		$("#quote").hide();
	}
	qText = undefined;
	show_quote();
}

function show_quote() {
	if (qText != undefined) {
		if (dirty) {
			$("#quote").fadeOut(2000, show_next);
		} else {
			show_next();
		}
	} else {
		getQuote();
		qTimer = setTimeout("show_quote()", 250);
	}
}

function show_next() {
	$("#qt").html(qText + '<br /><br /><span>&mdash; <i>' + qBy + "</i></span>");
	dirty = true;
	$("#quote").fadeIn(2000);
	var pause = 10000 + (qWordCount * 200);
	qTimer = setTimeout("quote_fader()", pause);
}	

function getQuote() {
	var qReq = "";
	if (qId >= 0) {
		qReq = '?' + qId + stepdir;
	}
	$.ajax({
		type: "GET",
				url: "xml/quote.php" + qReq,
				dataType: "xml",
				success: function(xml) {

					qId = $(xml).find('id').text();
					qText = $(xml).find('text').text();
					qBy = $(xml).find('by').text();
					qWordCount = $(xml).find('wordcount').text();

					qText = qText.replace(/\n/g, "<br />");
					qText = qText.replace(/--/g, "&mdash;");

				}
	});
}
