var testimonials_ajax;
var testimonialImages;
var currentTestimonial;
var currentTestimonialImage;
var currentTestimonialOpacity = 0; // the opacity of the fading IN testimonial. Fading OUT is worked out by 100-this
var topTestimonialImage = 1;
var testimonialFadeStepAmount;
var testimonialFadeStepTiming;

var timeBetweenTestimonials = 5; // in seconds
var testimonialFadeTime = 1; // in seconds
var testimonialFadeNumberOfSteps = 20; // how 'smooth' the fade is.


function initTestimonials()
{	
	currentTestimonial = 0;
	
	timeBetweenTestimonials *= 1000;
	testimonialFadeTime *= 1000;
	
	testimonialFadeStepTiming = Math.round(testimonialFadeTime / testimonialFadeNumberOfSteps);
	testimonialFadeStepAmount = Math.round(100 / testimonialFadeNumberOfSteps);
	
	testimonials_ajax = startAjax(); // separate ones for each required XML
	testimonials_ajax.open("GET","php/read_testimonials.php",true); // this requests the document from the server
	testimonials_ajax.onreadystatechange=testimonialsLoaded; // this is a function that's called when the state changes
	testimonials_ajax.send(null); // sends the request off to the server	
}

function testimonialsLoaded()
{
	if(testimonials_ajax.readyState==4) // the request is complete
	{
		var data=testimonials_ajax.responseText; // do whatever you like with it!
		eval(data);// this should give us an array called testimonialImages();
		loadNextTestimonial();
	}
}

function loadNextTestimonial()
{
	currentTestimonialImage = new Image();
	addEvent(currentTestimonialImage,'load',nextTestimonialReady,false);
	currentTestimonialImage.src = testimonialImages[currentTestimonial];
	
	currentTestimonial++;
	if(currentTestimonial >= testimonialImages.length)
		currentTestimonial = 0;
}

function nextTestimonialReady()
{
	setTimeout("displayTestimonial()",timeBetweenTestimonials);
	
}

function displayTestimonial()
{

	// OK, place the image into the current 'bottom' image
	i = topTestimonialImage == 1 ? 2 : 1;
	
	document.getElementById("testimonialImage"+i).src = currentTestimonialImage.src;
	
	// set the z index
	document.getElementById("testimonialImage"+i).style.zIndex = 2;
	document.getElementById("testimonialImage"+topTestimonialImage).style.zIndex = 1;
	
	topTestimonialImage = i;
	
	doTestimonialFade();
	//loadNextTestimonial();
}

function doTestimonialFade()
{
	currentTestimonialOpacity += testimonialFadeStepAmount;
	
	if(currentTestimonialOpacity > 100)
		currentTestimonialOpacity = 100;
		
	setOpacity("testimonialImage"+topTestimonialImage,currentTestimonialOpacity);
	i = topTestimonialImage == 1 ? 2 : 1;
	setOpacity("testimonialImage"+i,100-currentTestimonialOpacity);
	
	if(currentTestimonialOpacity < 100)
		setTimeout("doTestimonialFade()",testimonialFadeStepTiming);
	else
	{
		// we're done
		currentTestimonialOpacity = 0;
		loadNextTestimonial();
	}
}

function setOpacity(layer,op)
{
	// op is from 0 to 100
	ops = Math.round(op/10) / 10;
	document.getElementById(layer).style.opacity = ops;

	document.getElementById(layer).style.filter = "alpha(opacity="+op+")";

}
