var defaultQuoteTimeout;

function setupDefaultQuote() {
  defaultQuoteTimeout = setTimeout( new Function("setQuoteToDefault();"), 2000 );
}

function setQuoteToDefault() {
  // Get access to the box the quote will be placed in
  var quoteBox = document.getElementById( "quote_box" );

  // If the quote box couldn't be found, just quit
  if( !quoteBox ) return;

  defaultQuoteHTML = "&nbsp;";

  fade( 'quote_box', 100, 0, 500 );

  setTimeout( new Function( "document.getElementById( 'quote_box' ).innerHTML = '" + defaultQuoteHTML.replace(/'/g, "\\'") + "'" ), 505 );

  setTimeout( new Function("fade( 'quote_box', 0, 100, 500 )"), 510 );

}

function updateRandomQuote( lastNum ) {
  if( typeof( lastNum ) != 'number' || lastNum < 0 ) lastNum = -1;

  // Feel free to change this to suit your needs
  var secondsBetweenQuotes = 15; // (Including the nearly one second transition)

  // Get access to the box the quote will be placed in
  var quoteBox = document.getElementById( "quote_box" );

  // If the quote box couldn't be found, just quit
  if( !quoteBox ) return;

  // Setup communications back to the server for retrieving the quote
  var xmlHttp = getXmlHttpObject();

  // Request a new quote from the server.  Once the quote is received,
  // post it on the page (in an elegant fashion) and setup a timer
  // for the next request.
  xmlHttp.open( "get", "/random_quote.php" + "?avoid=" + lastNum + "&cacheavoidance=" + new Date().getTime() );
  xmlHttp.onreadystatechange = function () {
    if( xmlHttp.readyState == 4 ) {
      var response = xmlHttp.responseXML;

      if( response == null ) return;

      var curNum = response.getElementsByTagName( 'Number' );
      curNum = parseInt( getXmlNodeValue( curNum, 0, '-1' ), 10 );

      var quote = response.getElementsByTagName( 'Text' );
      quote = getXmlNodeValue( quote, 0, '' );

      if( quote == '' ) return;

      if( defaultQuoteTimeout != null );
      clearTimeout( defaultQuoteTimeout );

      var authors = Array();
      var authorTags = response.getElementsByTagName( 'Author' );
      if( authorTags.length > 0 ) {
        for( var i = 0; i < authorTags.length; i++ ) {
        var author = getXmlNodeValue( authorTags, i, '' );
        if( author != '' ) authors.push( '<span style="padding-left: 1em; font-weight: bold; font-style: italic;">-- ' + author + '</span>' );
        }
      }
      else {
        authors.push( '<span style="padding-left: 1em; font-weight; bold; font-style: italic">-- Annonymous</span>' );
      }

      var newQuoteHTML = '';
      newQuoteHTML = quote + "<br/>" + authors.join("<br/>");

      fade( 'quote_box', 100, 0, 500 );

      setTimeout( new Function( "document.getElementById( 'quote_box' ).innerHTML = '" + newQuoteHTML.replace(/'/g, "\\'") + "'" ), 505 );

      setTimeout( new Function("fade( 'quote_box', 0, 100, 500 )"), 510 );

      setTimeout( new Function( "updateRandomQuote(" + curNum + ")" ), secondsBetweenQuotes*1000 );
    }
  };
  xmlHttp.send(null);
}