/*
sbtd.js
Steve Boerner
Modified Nov. 29, 2010
*/

var QuoteXML = function (theFile, random) {
	var that = this;
	this.currentQuote;
	this.maxQuotes;
	this.toggle;
	this.a;
	this.b;
	this.random = random;
	
	$.ajax({
		type: 'GET',
		url: theFile,
		dataType: 'xml',
		error: that.error,
		success: function (data) {
			that.success(data);
		}
	});
}

QuoteXML.FADE = 500;

QuoteXML.prototype.error = function () {
	alert('Error: Unable to load XML file.');
}

QuoteXML.prototype.success = function (data) {
	this.toggle = true;
	this.maxQuotes = $(data).find('quote').length;
	this.currentQuote = this.random?
	Math.floor(Math.random() * (this.maxQuotes)): 0;
	this.updateQuote($(data).find('quote').eq(this.currentQuote));
}

QuoteXML.prototype.updateQuote = function (quote) {
	var that = this;
	this.a = (this.toggle)? 0: 1;
	this.b = (this.toggle)? 1: 0;
	this.toggle = ! this.toggle;
	var text = $(quote).find('quote_text').text();
	var author = $(quote).find('quote_author').text();
	$('.quote').eq(this.a).fadeOut(QuoteXML.FADE, 'swing', function () {
		$('.quote div.text').eq(that.a).find('span.text').remove();
		$('.quote div.author').eq(that.a).find('span.author').remove();
	});
	$('.quote div.text').eq(this.b).append('<span class="text">' + text + '</span>');
	$('.quote div.author').eq(this.b).append('<span class="author">' + author + '</span>');
	$('.quote').eq(this.b).fadeIn(QuoteXML.FADE, 'swing');
}

