/* Dependant on jQuery 1.4.2 */

var currentImage = 1;
var currentQuote = 1;
var quoteXML;
var quoteRotationTime = 12000;
var imageRotationTime = 20000;

$(document).ready(function(){

    $.ajax({
            type: "GET",
            url: "testimonials.xml",
            dataType: "xml",
            success: function(xml) {
                quoteXML = xml;
                rotateTestimonials();
                setInterval ( rotateTestimonials, quoteRotationTime );
            }
    });

    rotateBackground();

    setInterval ( rotateBackground, imageRotationTime );

});

function rotateBackground() {

    $('#bkfocus').animate({opacity: 0}, 1000, function() {
        loadNewImage();
    });

}

function rotateTestimonials() {

    var testimonials = $(quoteXML).find('Testimonial');

    $('#testimonalsContent').animate({opacity: 0}, 1000, function() {

        var testimonial = testimonials[currentQuote - 1];

        quote = $(testimonial).children("Quote").text();
        signature = $(testimonial).children("Signature").text();

        $('p#quote').html(quote);
        $('p#signature').html(signature);

        $('#testimonalsContent').animate({opacity: 1}, 1000, function() {
            // Animation complete.
        });
    });

    currentQuote++;

    if (currentQuote > testimonials.length) {
        currentQuote = 1;
    }

}

function loadNewImage() {
    var targetDiv = document.getElementById("bkfocus");

    if (currentImage == 1) {
        targetDiv.src = "images/bassballbk.png";
    }

    if (currentImage == 2) {
        targetDiv.src = "images/violinbk.png";
    }

    if (currentImage == 3) {
        targetDiv.src = "images/cheerbk.png";
    }

    currentImage++;

    if (currentImage > 3) {
        currentImage = 1;
    }

    $('#bkfocus').animate({opacity: 1}, 2000, function() {
        // Animation complete.
    });
}
