var testimonialOld = 0;
var testimonialNow = 0;
var testimonialFadeDir = -1;
var testimonialOpacity = 100;
var testimonialTimer = 0;
var testimonialFadeTimer = 0;

var testimonialDelayTime = 15000; //milliseconds between transitions. 1000 = 1 second


/* Instant switch to next testimonial */
function testimonialNextNow(dir) {
	if( typeof(dir) != "number" ) dir = 1;

	var tests = getElementsByClassName('testimonial');
	var oldid = testimonialNow;
	var newid = oldid + dir;
	if( newid > tests.length -1 ) newid = 0;
	if( newid < 0 ) newid = tests.length -1;
	
	clearTimeout(); //Stop the fade timer if it exists
	
	
	tests[testimonialOld].style.visibility = 'hidden';
	tests[testimonialOld].style.display = 'none';
	tests[testimonialOld].style.opacity = 0;
	tests[testimonialOld].style.filter = 'alpha(opacity=0)';
	
	tests[testimonialNow].style.visibility = 'hidden';
	tests[testimonialNow].style.display = 'none';
	tests[testimonialNow].style.opacity = 0;
	tests[testimonialNow].style.filter = 'alpha(opacity=0)';

	testimonialNow = newid;
	testimonialOld = oldid;
	
	tests[testimonialNow].style.visibility = 'visible';
	tests[testimonialNow].style.display = 'block';
	tests[testimonialNow].style.opacity = 1;
	tests[testimonialNow].style.filter = 'alpha(opacity=100)';

}

/* Begin fading  */
function testimonialNext() {
	var tests = getElementsByClassName('testimonial');
	var oldid = testimonialNow;
	var newid = oldid + 1;
	if( newid > tests.length -1 ) newid = 0;
	testimonialNow = newid;
	testimonialOld = oldid;


	testimonialFade();
}

/* Perform fading */
function testimonialFade() {
	var tests = getElementsByClassName('testimonial');
	var id = 0;
	var stop = 0;
	
	if( testimonialFadeDir < 0 ) { /* Fading out */
		id = testimonialOld;
	} else { /* Fading in */
		id = testimonialNow;
	}

	testimonialOpacity = testimonialOpacity + testimonialFadeDir * 10;


	if( testimonialOpacity <= 0 ) { /* Fadeout Complete */
		testimonialOpacity = 0;
		testimonialFadeDir = 1;
		
		tests[testimonialOld].style.visibility = 'hidden';
		tests[testimonialOld].style.display = 'none';
		
		tests[testimonialNow].style.opacity = 0;
		tests[testimonialNow].style['filter']="alpha(opacity=0)";
		tests[testimonialNow].style.visibility = 'visible';
		tests[testimonialNow].style.display = 'block';
		
	}else if( testimonialOpacity >= 100 ) { /* Fadein Complete */
		testimonialOpacity = 100;
		testimonialFadeDir = -1;
		
		stop = 1;
	}
	
	tests[id].style.opacity = testimonialOpacity/100;
	tests[id].style['filter']="alpha(opacity="+testimonialOpacity+")";
	
	if( stop ) testimonialDelay(); else testimonialFadeTimer = setTimeout( testimonialFade, 50 );
	
}

/* Start waiting for next fade */
function testimonialDelay() {
	testimonialTimer = setTimeout(testimonialNext, testimonialDelayTime );
}

/* Once page loads, start waiting for first fade */
setTimeout( testimonialDelay, 1000 );