var testimonialsAry = new Array();
var currentTestimonial=0;
var testimonialTimer;
var count=0;

function autotestimonials(){
	var rand = "&rand="+Math.random() +1 * count++; //cache busting
	var url = "http://"+location.host+"/scripts/getContent.php?element=testimonials";
	
	try{request = new XMLHttpRequest();}catch(e){
	try{request = new ActiveXObject("Msxml2.XMLHTTP");}catch(e){
	try{request = new ActiveXObject("Microsoft.XMLHTTP");}catch(e){document.write("System error, please reboot or contact administrator", "warning"); request = false;}}}
		
	if(request!=false){
		request.onreadystatechange=xmlHTTPGetResponse;
		request.open("GET",url+rand,true);
		request.send(null);
	}
}

function xmlHTTPGetResponse(){
	if(request.readyState==4 && request.status==200){

		var response = request.responseXML; //get the xml
		
		for (var i=0; i<response.getElementsByTagName('testimonial').length; i++){ //get all testimonials into an array of objects
			var author = response.getElementsByTagName('author')[i].childNodes[0].nodeValue;
			var content = response.getElementsByTagName('content')[i].childNodes[0].nodeValue;

			var testimonial = new Object();
			testimonial.content = content;
			testimonial.author = author;
			testimonialsAry.push(testimonial);
		}
		fillTestimonials();
	}
}

function fillTestimonials(){ //to run on timer tick (or similar)
	document.getElementById("play-pausebtn").innerHTML = '<img src="images/pause.png" onmouseover="this.src=\'images/pause2.png\';" onmouseout="this.src=\'images/pause.png\';" onclick="pauseTestimonials();"/>';
	var quoute = document.getElementById("testimonialQuoute"); //define the quoute area
	var author = document.getElementById("testimonialAuthor"); //define the author area
	quoute.innerHTML = testimonialsAry[currentTestimonial].content;
	author.innerHTML = testimonialsAry[currentTestimonial].author;
	currentTestimonial++;
	if(currentTestimonial == testimonialsAry.length){currentTestimonial = 0;}
	testimonialTimer = setTimeout("fillTestimonials()", 5000);
}

function pauseTestimonials(){
	clearTimeout ( testimonialTimer );
	document.getElementById("play-pausebtn").innerHTML = '<img src="images/play.png" onmouseover="this.src=\'images/play2.png\';" onmouseout="this.src=\'images/play.png\';" onclick="fillTestimonials();"/>';
}

function nextTestimonial(){
	pauseTestimonials();
	if(currentTestimonial == testimonialsAry.length){currentTestimonial = 0;}
	currentTestimonial++;
	var quoute = document.getElementById("testimonialQuoute"); //define the quoute area
	var author = document.getElementById("testimonialAuthor"); //define the author area
	quoute.innerHTML = testimonialsAry[currentTestimonial].content;
	author.innerHTML = testimonialsAry[currentTestimonial].author;
}

function prevTestimonial(){
	pauseTestimonials();
	if(currentTestimonial == 0){currentTestimonial = testimonialsAry.length;}
	currentTestimonial--;
	var quoute = document.getElementById("testimonialQuoute"); //define the quoute area
	var author = document.getElementById("testimonialAuthor"); //define the author area
	quoute.innerHTML = testimonialsAry[currentTestimonial].content;
	author.innerHTML = testimonialsAry[currentTestimonial].author;
}

