var Rype = Rype || {};

Rype.testimonials = function(container) {
	container = $(container);
	
	//add buttons
	container.append('<div class="buttons"><a href="#" class="prevButton">Previous</a><a href="#" class="nextButton">Next</a></div>');
	
	var prev = container.find('.buttons .prevButton'),
		next = container.find('.buttons .nextButton'),
		testimonials = container.find('.testimonial');
		
	var current = testimonials.index(container.find('.testimonial.active'));
	
	var swap = function(n) {
		testimonials.eq(current).fadeOut(function() {
			testimonials.eq(n).fadeIn(function() {				
				testimonials.eq(current).removeClass('active');
				$(this).addClass('active');
				current = n;
			});
		});
	};

	prev.click(function(e) {
		e.preventDefault();
		var p = (current-1 >= 0 ) ? current-1 : testimonials.length - 1;
		swap(p);
	});
	
	next.click(function(e) {
		e.preventDefault();
		var n = (current+1 >= testimonials.length ) ? 0 : current+1;
		swap(n);
	});
};

$(document).ready(function(){
	var tests = $('#testimonials');
	if(tests.length)
		Rype.testimonials(tests);
});