var QuoteRotator = Class.create();

QuoteRotator.prototype = {
	initialize: function(element, options) {
		this.element = $(element);
		this.options = Object.extend({className: 'quote', firstQuote: 0}, options);
		this.quotes = this.element.select('.'+this.options.className);
		this.currentQuote = this.quotes[this.options.firstQuote];
		this.prepareQuotes();
		new Effect.Appear(this.currentQuote, {duration: 1.5});
		this.registerCallback();
	},
	
	prepareQuotes: function() {
		this.element.style.position = 'relative';
		this.element.style.height = (this.quotes.max(function(quote) {
			Element.setStyle(quote, {display: 'none', position: 'absolute', width: '100%', left: '0px'});
			return Element.getHeight(quote);
		}) + 30).toString() + 'px';
	},
	
	nextQuote: function() {
		return this.quotes[(this.quotes.indexOf(this.currentQuote) + 1) % this.quotes.length];
	},
	
	registerCallback: function() {
		window.setTimeout(this.tick.bind(this), this.currentQuote.innerHTML.stripTags().strip().split(/\b[\s,\.-:;]*/).length * 250 + 4);
	},
	
	tick: function() {
		var currentQuote = this.currentQuote, nextQuote = this.nextQuote();
	
		new Effect.Parallel([
			new Effect.Fade(currentQuote, {sync: true}),
			new Effect.Appear(nextQuote, {sync: true})
			], {
				duration: 2,
				afterFinish: (function(effect) {
				this.currentQuote = nextQuote;
				this.registerCallback();
			}).bind(this)
		})
	}
};