var quotes, quote, quote_count, quote_interval, current_quote = 0, old_quote = 0, top;

$(function() {

	$('#gallery a').lightBox();
	$('a[rel=lightbox]').lightBox();

	if($("#testimonial").size())
	{
		$.ajaxSetup({ scriptCharset: "utf-8" , contentType: "application/json; charset=utf-8"});
		$.getJSON('/testimonials',function(json){
			quotes = json['quotes'];
			quote_count = quotes.length;
			rotate_quote();
			quote_interval = setInterval(rotate_quote, 10000);
		});
	}
});

function rotate_quote() {
	$("#testimonial").animate({opacity: 0}, 500, 'linear', function(){
		$("#testimonial > p").css('visibility', 'visible');
		$("#testimonial > p").html("“"+quotes[current_quote]['quote']+"”");
		$("#testimonial > p").append("<cite>&nbsp;— "+quotes[current_quote]['cite']+"</cite>");
		$("#testimonial > p").css('top', 25 - ($("#testimonial > p").height()/2) + 'px');
		$("#testimonial").animate({opacity: 1}, 500, 'linear');
	});
	current_quote = (old_quote + 1) % quote_count;
	old_quote = current_quote;
}

window.onunload = function(){ clearInterval(quote_interval); }
