/**
 * Text rotator implemintation
 *
 *@author Sam McCallum <sam@palo-verde.us>
 */
var TextRotatorImp = new Class({
	start: function () {		
		// set a default wait time if not set
		if (!this.options) throw new Error('TextRotatorImp expects options');
		if (!this.options.wait) this.options.wait = 10;
		
		// Show the first text
		this.rotateIn(this.getText());
		
		// start the timer
		this.rotationHandle = this.rotate.bind(this);
		this.rotationHandle.periodical(this.options.wait*1000);
	},
	
	stop: function () {
		$clear(this.rotationHandle);
	},
	
	rotate: function () {
		this.rotateOut();
		this.rotateIn(this.getText());		
	},
	
	getText: function () {throw new Error('TextRotatorImp#getText is abstract');},
	rotateOut: function () {throw new Error('TextRotatorImp#rotateOut is abstract');},
	rotateIn: function (textIn) {throw new Error('TextRotatorImp#rotateIn is abstract');}	
});

/**
 * Testimony rotator
 */
var TestimonyRotator = new Class({
	Implements: TextRotatorImp,
	last: 0,
	counter: 0,

	initialize: function (options) {
		// default options
		this.options = {
			container_id: 'testimonials_bq',
			source_selector: '#testimonial_source li'
		};
		
		$extend(this.options, options || {});
		this.sources = $$(this.options.source_selector);
		
		this.slide = new Fx.Slide(this.options.container_id,{mode: 'horizontal'});
		this.start();
	},
	
	rotate: function () {
		this.slide.slideOut().chain(function () {
			TestimonyRotator.instance.rotateOut();
			TestimonyRotator.instance.rotateIn(TestimonyRotator.instance.getText());
			this.slideIn();
		});
	},
	
	getText: function () {
		/*
		var current = $random(0,(this.sources.length-1));
		while (current == this.last) {
		    current = $random(0,(this.sources.length-1));
		}
		this.last = current;*/
		if (this.counter == (this.sources.length)) this.counter = 0;
		return this.sources[this.counter++].getFirst().clone();
	},
	
	rotateOut: function () {
		$(this.options.container_id).getFirst().dispose();
	},
	
	rotateIn: function (textIn) {
		$(this.options.container_id).grab(textIn);
	}
});

TestimonyRotator.load = function () {TestimonyRotator.instance = new TestimonyRotator();};
window.addEvent('domready', TestimonyRotator.load);