//Tracks the current quote's index.
currentQuote = 0;

//Full array of quotes pulled from the XML source file.
myAuthors = new Array();
myQuotes = new Array();

numQuotes = 0;

//Interval timer object for interval tracking.
quoteInterval = 0;

//delay in miliseconds.
quoteDelay = 8000;

getQuotePath = 'wp-content/themes/daxxterity/xml/quotes.xml';

addEventHandler( window, 'load', initQuotes );

base_url_path = 'http://www.daxxterity.com/';


function addEventHandler( obj, eventName, handler ) {
	if (document.attachEvent) {
		obj.attachEvent("on" + eventName, handler);
	} else if (document.addEventListener) {
		obj.addEventListener(eventName, handler, false);
	}
}

function createRequest() {
	try {
		request = new XMLHttpRequest();
	} catch (tryMS) {
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (otherMS) {
		try {
			request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (failed) {
			request = null;
		}
			}
	}
	return request;
}

function initQuotes() {
	//Get our first quote and then start playback.
	getQuotes();
	playQuotes();

}

function playQuotes() {
	clearInterval(quoteInterval);
	quoteInterval = setInterval('nextQuote()', quoteDelay);
}

function nextQuote() {
	//gets the next quote, and rotates to the beginning if we're at the end of the list.
	if (currentQuote < numQuotes - 1 ) {
		currentQuote++;
	} else {
		currentQuote = 0;
	}
	getQuote();
}

function getQuote() {

	//it does the fade out effect,
	//on completion it executes the callbacks.
	new Effect.Opacity('quote-heading', { 
		duration: 0.5,
		from: 1.0, 
		to: 0,
		afterFinish: replaceAuthor
	});
	new Effect.Opacity('quote-paragraph', { 
		duration: 0.5,
		from: 1.0, 
		to: 0,
		afterFinish: replaceContent
	});
}

function replaceAuthor() {
	var quoteAuthor = document.getElementById("quote-author");

	//empties the current quote,
	if (quoteAuthor.childNodes.length > 0) {
		quoteAuthor.removeChild(quoteAuthor.firstChild);
	}

	//then fills it with the next quote in the array,
	var authorText = document.createTextNode(myAuthors[currentQuote]);
	quoteAuthor.appendChild( authorText );

	//then it fades in.
	new Effect.Opacity('quote-heading', { 
		duration: 0.25,
		from: 0, 
		to: 1.0
	});
}

function replaceContent() {
	var quoteContent = document.getElementById("quote-content");

	//empties the current quote,
	if (quoteContent.childNodes.length > 0) {
		quoteContent.removeChild(quoteContent.firstChild);
	}

	//then fills it with the next quote in the array,
	var contentText = document.createTextNode(myQuotes[currentQuote]);
	quoteContent.appendChild( contentText );

	//then it fades in.
	new Effect.Opacity('quote-paragraph', { 
		duration: 0.25,
		from: 0, 
		to: 1.0
	});
}


function getQuotes() {
	requestQuote = createRequest();
	if (requestQuote == null) {
		alert("Unable to create request");
		return;
	}

	var url= getQuotePath;
	requestQuote.onreadystatechange = getQuoteList;
	requestQuote.open("GET", url, true);
	requestQuote.send(null);
}

Array.prototype.shuffle = function() {
	function randOrd(){return (Math.round(Math.random())-0.5); }
	this.sort( randOrd );
} 

function getQuoteList() {
	if (requestQuote.readyState == 4) {
		if (requestQuote.status == 200) {
			var quoteXML = requestQuote.responseXML;
			//read the entire XML document and stuff the quote information into
			//an easy-to-access array.

			var quoteElements = quoteXML.getElementsByTagName("quote");

			numQuotes = quoteElements.length;

			var quoteAuthors = quoteXML.getElementsByTagName("author");
			var quoteContent = quoteXML.getElementsByTagName("content");

			//To shuffle the quotes:
			//take an existing array, mix it up, then assign the other numbers to it.
			var dqBlizzard = new Array();
			for (var i=0; i < numQuotes; i++ ) {
				dqBlizzard[i] = i;
			}	
			dqBlizzard.shuffle();
			var mixin;

			for (i=0; i < numQuotes; i++ ) {
				mixin = dqBlizzard[i];
				myAuthors[mixin] = quoteAuthors[i].firstChild.nodeValue;
				myQuotes[mixin] = quoteContent[i].firstChild.nodeValue;
			}

			
		}
	}
}