var testimonials = {

	current_id: 0,
	total: 0,
	show_for: 8,
	duration: .5,
	already_seen: [],
	
	init: function() {

		//work out current ID
		$$('#testimonials .testimonial').each( function( elm ) {
			testimonials.total++;
			id = elm.id.replace( 'testimonial_', '' );
			if( elm.getStyle( 'display' ) != 'none' ) testimonials.current_id = id;
		} );
		
		this.already_seen.push( this.current_id );
	
		testimonials.rotate.bind(this).delay( this.show_for );
		
	},

	rotate: function() {
	
		//pick a new testimonial
		var r = this.get_random();
		while( this.already_seen.indexOf( r ) != -1 ) r = this.get_random();
		
		//rememebr we have seen this
		this.already_seen.push( r );
		
		//hide the old box
		new Effect.Fade( 'testimonial_' + this.current_id, { duration: this.duration } );
		
		//update current ID
		this.current_id = r;
	
		//show the new box
		new Effect.Appear( 'testimonial_' + r, { duration: this.duration, queue: 'end' } );
	
		//reset array if seen then all
		if( this.already_seen.length == this.total ) this.already_seen = [ r ];
		
		//recurse
		testimonials.rotate.bind(this).delay( this.show_for + ( this.duration * 2 ) );
	
	},
	
	get_random: function() {
		
		return Math.floor( Math.random() * ( this.total ) );
		
	}

}

document.observe( 'dom:loaded', testimonials.init.bind(testimonials) );