/**************************************
 *	Fadeshow
 **************************************/

var TestimonialFader = new Class({
	object:				null,	
	effect: 			null,
	periodicalTimer: 	null,
	
	setOptions: function(options){
		this.options = {
			interval: 5000,
			duration: 2000
		}
		Object.extend(this.options, options || {});
	},
	
	initialize: function(object, options){
		this.object		= object;
		
		this.object.addEvent('click', function(){
			location = '/testimonials/';
		})
		
		this.setOptions(options);
		if (this.options.duration > this.options.interval) { this.options.interval = this.options.duration + 2000 }
		
		this.showNew();
		this.periodicalTimer = this.transist.periodical(this.options.interval, this);
		
	},
	
	transist: function(){
		// Fade out
		this.effect = new Fx.Style(this.object, 'opacity', {duration: this.options.duration / 4, onComplete: this.showNew.bind(this)});
		this.effect.start(1,0);
	},
	
	showNew: function(){
		
		// Load new random record
		new Json.Remote("/testimonials/getrandom", { onComplete: function(testimonial){
			
			// Set new data
			this.setCurrentData(testimonial);
			
			//Fade in
			this.effect = new Fx.Style(this.object, 'opacity', {duration: this.options.duration});
			this.effect.start(0,1);
			
		}.bind(this)}).send();
	},
	
	setCurrentData: function(data){
		this.object.getElement('p').innerHTML = '<span>"</span>' + data.message + '<span>"</span>';
		
		var name		= data.name || 'Een tevreden klant';
		var location	= data.location? '<br />' + data.location : '';
		var country		= data.country? ', ' + data.country : '';
		this.object.getElement('em').innerHTML = name + location + country;
	}
})

fireOn = Window.ie ? 'load' : 'domready';
window.addEvent(fireOn, function(){
	
	// Load fadeshow
	var testimonial	= $E('#testimonial');
	if(testimonial){
		new TestimonialFader(testimonial, {duration: 2000, interval: 10000} );
	}
});