$(function(){
	// Only load testimonials on pages that already display a testimonial
	if ($('#rightSidebar .testimonial').length > 0) {
		// Load testimonial data
		$.get('testimonials.html', function(data){
			var quotes = [];
			var duration = 10000;
			var speed = 0;		
			// Remove existing static testimonial from sidebar
			$('#rightSidebar .testimonial').remove();
			// Append wrapper div to hold testimonials
			$('#rightSidebar').append('<div class="testimonials"></div>');
			// Put testimonials into an array
			$(data).find('#testimonialSummaries .testimonial').each(function(){
				quotes.push($(this));
			});
			// Shuffle quotes
			quotes = shuffle(quotes);
			// Loop through randomized quotes and add to sidebar
			$(quotes).each(function(){
				$('#rightSidebar .testimonials').append('<div class="testimonial">' + $(this).css('display', 'none').html() + '<p class="more"><a href="testimonials.html">More >></a></div>');
			});
			// Fade in first quote
			$('#rightSidebar .testimonial').eq(0).fadeIn(speed);
			// Start slideshow
			var i = 0;
			var n = quotes.length;
			setInterval(function() {
				nextIndex = i + 1;
				if (i == (n - 1)) nextIndex = 0;
				$('#rightSidebar .testimonial').eq(i).fadeOut(speed);
				$('#rightSidebar .testimonial').eq(nextIndex).fadeIn(speed);
				i++;
				if (i == n) i = 0;
			}, duration);			
		});
	}
});

var shuffle = function(o){
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};