var quoteflipdelay = 8000; //in milliseconds, the duration of the quote's display
var quotefadeduration = 0.5; //the duration of the fade out and in of the quote
var quotedivname = "quote"; // the name of the div containing the quote

var quotediv;
var quotes;
var lastQuote = 0;
var lastQuoteClient = null;

//request the quotes xml file
function get_quotes()
{
	var quoteXML = new Ajax.Request("/data/customerquotes.xml",{method:'get',onSuccess:storeXML});
}

//store the entry tags in an array
function storeXML(req)
{
	quotes = req.responseXML.getElementsByTagName("entry");
	start_flip();
}

//start the flip interval
function start_flip()
{
	window.setInterval("fade_quote_out()",quoteflipdelay);
}

//fade the div
function fade_quote_out()
{
	Effect.Fade(quotedivname,{afterFinish:parse_quotes,duration:quotefadeduration});
}

//choose a new quote
function parse_quotes()
{
	var x = Math.floor(Math.random()*quotes.length)
	if (x == lastQuote) { parse_quotes();return false;}
	var quote = quotes[x].getElementsByTagName("quote")[0].firstChild.nodeValue;
	var client = quotes[x].getElementsByTagName("customername")[0].firstChild.nodeValue;
	if (client == lastQuoteClient) { parse_quotes();return false;}
	lastQuote = x;
	lastQuoteClient = client;
	render_quote(quote,client);
}

//render the quote
function render_quote(quote,client)
{
	quotediv.innerHTML = "<p class=\"customerquote\">" + quote + "</p><p class=\"customername\">" + client + "</p>";
	fade_quote_in();
}

//fade in the div
function fade_quote_in()
{
	Effect.Appear(quotedivname,{duration:quotefadeduration});
}

function quoteflip_init()
{
	quotediv = (document.getElementById(quotedivname)) ? document.getElementById(quotedivname) : null ;
	get_quotes();
}

addEvent(window,"load",quoteflip_init,false);
