var lastQuote = -1;

function updateRandomQuote() {
    // 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( "quoteBox" );

    // If the quote box couldn't be found, just quit
    if( !quoteBox ) return;

    // Setup communications back to the server for retrieving the quote
    var http;
    if( navigator.appName == "Microsoft Internet Explorer" )
        http = new ActiveXObject("Microsoft.XMLHTTP");
    else
        http = new XMLHttpRequest();

    // 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.
    http.open( "get", "/cgi-bin/random_quote.cgi" + "?avoid=" + lastQuote + "&cacheavoidance=" + new Date().getTime() );
    http.onreadystatechange = function () {
        if( http.readyState == 4 ) {
            var newQuote = http.responseText.replace( /^\s+|\s+$/g, "" ).split("\n");

            // Roughly double check the data just for sanity sake
            if( newQuote.length == 2 && newQuote[0].match(/^\d+$/) ) {
                lastQuote = newQuote[0];

                // Fade out the old quote, replace the text and fade the new quote in
                setTimeout( function () { var box = document.getElementById( "quoteBox" ); box.style.filter = "alpha(opacity=90)"; box.style.opacity = ".9"; }, 50 );
                setTimeout( function () { var box = document.getElementById( "quoteBox" ); box.style.filter = "alpha(opacity=80)"; box.style.opacity = ".8"; }, 100 );
                setTimeout( function () { var box = document.getElementById( "quoteBox" ); box.style.filter = "alpha(opacity=70)"; box.style.opacity = ".7"; }, 150 );
                setTimeout( function () { var box = document.getElementById( "quoteBox" ); box.style.filter = "alpha(opacity=60)"; box.style.opacity = ".6"; }, 200 );
                setTimeout( function () { var box = document.getElementById( "quoteBox" ); box.style.filter = "alpha(opacity=50)"; box.style.opacity = ".5"; }, 250 );
                setTimeout( function () { var box = document.getElementById( "quoteBox" ); box.style.filter = "alpha(opacity=40)"; box.style.opacity = ".4"; }, 300 );
                setTimeout( function () { var box = document.getElementById( "quoteBox" ); box.style.filter = "alpha(opacity=30)"; box.style.opacity = ".3"; }, 350 );
                setTimeout( function () { var box = document.getElementById( "quoteBox" ); box.style.filter = "alpha(opacity=20)"; box.style.opacity = ".2"; }, 400 );
                setTimeout( function () { var box = document.getElementById( "quoteBox" ); box.style.filter = "alpha(opacity=10)"; box.style.opacity = ".1"; }, 450 );
                setTimeout( function () { var box = document.getElementById( "quoteBox" ); box.style.filter = "alpha(opacity=0)";  box.style.opacity = "0"; },  500 );

                setTimeout( new Function( "document.getElementById( 'quoteBox' ).innerHTML = '" + newQuote[1].replace(/'/g, "\\'") + "'" ), 505 );

                setTimeout( function () { var box = document.getElementById( "quoteBox" ); box.style.filter = "alpha(opacity=10)"; box.style.opacity = ".1"; }, 510 );
                setTimeout( function () { var box = document.getElementById( "quoteBox" ); box.style.filter = "alpha(opacity=20)"; box.style.opacity = ".2"; }, 560 );
                setTimeout( function () { var box = document.getElementById( "quoteBox" ); box.style.filter = "alpha(opacity=30)"; box.style.opacity = ".3"; }, 610 );
                setTimeout( function () { var box = document.getElementById( "quoteBox" ); box.style.filter = "alpha(opacity=40)"; box.style.opacity = ".4"; }, 660 );
                setTimeout( function () { var box = document.getElementById( "quoteBox" ); box.style.filter = "alpha(opacity=50)"; box.style.opacity = ".5"; }, 710 );
                setTimeout( function () { var box = document.getElementById( "quoteBox" ); box.style.filter = "alpha(opacity=60)"; box.style.opacity = ".6"; }, 760 );
                setTimeout( function () { var box = document.getElementById( "quoteBox" ); box.style.filter = "alpha(opacity=70)"; box.style.opacity = ".7"; }, 810 );
                setTimeout( function () { var box = document.getElementById( "quoteBox" ); box.style.filter = "alpha(opacity=80)"; box.style.opacity = ".8"; }, 860 );
                setTimeout( function () { var box = document.getElementById( "quoteBox" ); box.style.filter = "alpha(opacity=90)"; box.style.opacity = ".9"; }, 910 );
                setTimeout( function () { var box = document.getElementById( "quoteBox" ); box.style.filter = "alpha(opacity=100)"; box.style.opacity = "1"; }, 960 );

//                quoteBox.innerHTML = newQuote[1];

                // After a certain amount of time, do it all again
                setTimeout( new Function( "updateRandomQuote()" ), secondsBetweenQuotes*1000 );
            }
        }
    };
    http.send(null);
}