var testimonials = {
	
	counters : false,
	
	init : function(){
		testimonials.hideAll();
		testimonials.initCounters();
		testimonials.initRotate();
	},
	
	hideAll : function(){
		$('.testimonial-entry').hide();
	},
	
	initCounters : function(){
		
		testimonials.counters = new Array;
		
		$('.testimonial').each(function(){
			
			var iniqueId = $(this).attr('id').split('-')[1];
			var itemLength = $(this).find('.testimonial-entry').length;
			
			var countObj = {
				'uid' : iniqueId,
				'count' : 0,
				'items' : itemLength
			};
			
			testimonials.counters.push(countObj);
		});
		
	},
	
	initRotate: function(){
		for( var i = 0; i < testimonials.counters.length; i++ ){
			var details = testimonials.counters[i];
			var randomNumber = Math.floor( Math.random() * ( details.items + 1 ) );
			testimonials.show( details.uid, randomNumber, details.items);
		}
	},
	
	show : function( id, count, items ){
		
		// Show		
		var showElem = $('#testimonial-' + id + '-' + count);
				
		if (showElem.length == 0) {
			count = 0;
			showElem = $('#testimonial-' + id + '-' + count);
		}
		
		// Hide
		var hideElem = $('#testimonial-' + id + '-' + (count - 1) );
		
		if (hideElem.length == 0) {
			hideElem = $('#testimonial-' + id + '-' + (items - 1) );
		}
		
		// Run actions
		hideElem.fadeOut(function(){
			showElem.fadeIn();
		});
		
		setTimeout('testimonials.show(' + id + ',' + ( count + 1 ) + ',' + items + ')',7000);
	}
	
}


$(document).ready(function(){
	testimonials.init();
});
