var delay = 15e3;

function tloop() {
	// get the next testimonial
	var next = $('div.testimonial.current ~ div.testimonial').eq(0);
	if (next.length == 0)
	{
		next = $('div.testimonial:first');
	}
	
	// don't animate if we only have one testimonial
	if (next.hasClass('current'))
	{
		next.removeClass('current');
		return;
	}
	
	var speed = 0.5e3;
	
	$('div.testimonial.current')
		.removeClass('current')
		.animate({top: $('#testimonials').height() / 4, opacity: 0},speed,function() {
			// remove explicit opacity for png compatibility
			$('div.testimonial.current').css({opacity: ''});
			$('div.testimonial').not('.current').hide();
		
			setTimeout(tloop, delay);
		})
		.animate({top: 0},0);
	next
		.addClass('current').show()
		.animate({opacity: 1},speed / 2);
}

$(document).ready(function() {
	// set CSS styles for testimonial rotation
	var testimonials = $('div.testimonial')
		.css({position: 'absolute', left: 0, top: 0, opacity: 0}).hide().removeClass('alt');
	testimonials.eq(Math.floor(Math.random() * testimonials.length))
		.addClass('current').css({opacity: ''}).show();
		
	// determine container height
	$('div.testimonial').each(function() {
		var theight = $(this).height() + 30 + 50;
		
		//theight = theight * 1.2;
		if (theight > $('#testimonials').height())
		{
			$('#testimonials').height(theight);
		}
	});
	
	// call async recursion for testimonial rotation
	setTimeout(tloop, delay);
});
