$(document).ready(function() {
		preBufferImages();
		pickRandomQuote();
		setInterval("pickNextQuote()", 5000) // start loop
    
})

var currentQuoteId = null;
var quotes = new Array() 
quotes[0] = '/images/quotes/TMC-Site_Testimonials_Karen.gif'
quotes[1] = '/images/quotes/TMC-Site_Testimonials_Max.gif'
quotes[2] = '/images/quotes/TMC-Site_Testimonials_Judy4.gif'
quotes[3] = '/images/quotes/TMC-Site_Testimonials_Ajit.gif'



function preBufferImages() {
		var p = quotes.length;
		var preBuffer = new Array();
		
    for (i = 0; i < p; i++){
       preBuffer[i] = new Image()
       preBuffer[i].src = quotes[i]
    }
}

function pickRandomQuote() {
	  var p = quotes.length;
		var whichImage = Math.round(Math.random()*(p-1));

		setQuote(whichImage, false);		
}

function pickNextQuote() {
	  var p = quotes.length;
	  var nextImageId = (currentQuoteId+1 >= p) ? 0 : currentQuoteId+1;

		setQuote(nextImageId, true);		
}

function setQuote(whichImage, animate) {

		currentQuoteId = whichImage;

		var switchImage = function() {
		 		//var img = '<img src="'+quotes[whichImage]+'">';
		    //document.getElementById('quote').innerHTML = img;
			$('#quote').attr('src',quotes[whichImage]);
		}
		
		if (animate) {

			$("#quote").fadeOut("slow", function(){
				switchImage();
				$("#quote").fadeIn("slow");
			});
		
		} else {
			switchImage();
		}
	
}
