var Quotator = new Class({
	
	Implements: [Options, Events],
	
	options:{
		json : '/wp-content/themes/boettner/js/quotes.js',
		speed : 12000,
		start : 0
	},

	initialize: function(element, options){
		this.setOptions(options);
		this.element = $(element);
		
		
		var quoteLoader = new Request.JSON({
			url: this.options.json,
			onSuccess: function(response){
				this.quotes = response.quotes;
				this.element.setStyle('background', this.quotes[this.options.start].background);
				/*this.element.set(
					'html' , 
					"<div id='quote'>" + this.quotes[this.options.start].quote + "</div><div id='author'>" + this.quotes[this.options.start].author + "</div>"
				);*/
				setInterval(this.changeQuote.bind(this), this.options.speed);
			}.bind(this)
		});
		quoteLoader.send();
	},
	
	changeQuote : function(){
		if(this.options.start == this.quotes.length - 1){
        	this.options.start = 0;
		} else{
        	this.options.start++;
      	}
      	
      	this.fadeIn(this.options.start);
      	
	},
	
	fadeIn : function(index){
		var fader = new Fx.Tween(this.element,{property : 'opacity', duration : '2000'});
		
	
		fader.start(0).chain(
			function() {
				//alert('moo');
				this.element.setStyle('background', this.quotes[index].background);
				/*this.element.set(
					'html', 
					"<div id='quote'>" + this.quotes[index].quote + "</div><div id='author'>" + this.quotes[index].author + "</div>"
				);*/
				fader.start(1); 
			}.bind(this)
		);
	}
});