// Returns full date: Dayname, Month nn, yyyy

function fulldate() {
 var mm=new String("January,February,March,April,May,June,July,August,September,October,November,December").split(",");
 var dd= new String("Sun,Mon,Tues,Wednes,Thurs,Fri,Satur").split(",");
 var now = new Date();
 var y = ((y = now.getYear()) < 2000 ? 1900 + y : y);
 return dd[now.getDay()] + "day, " +
	mm[now.getMonth()] + " " +
	now.getDate() + ", " + y;

}

//
// xml quote fetcher/fader
//

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

google.load("jquery", "1");
google.setOnLoadCallback(init_quotes);



function init_quotes() {
	$("#dateline").hide();
	$("#dateline").html(fulldate());
	$("#dateline").fadeIn(2000, quote_fader);
}

function quote_fader() {
	$("#dateline").html(fulldate());
	if (!dirty) {
		$("#qt").hide();
	}
	qText = undefined;
	show_quote();
}

function show_quote() {
	if (qText != undefined) {
		if (dirty) {
			$("#qt").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;
	$("#qt").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;");

				}
	});
}

function quoteCtl(dir) {
	clearTimeout(qTimer);
	$("#qt").stop(true, true);
	stepdir	= dir;
	dirty = false;
	if (dir != 'x') {
		quote_fader();
	}
}
