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

var Fadeshow = new Class({
	items:				null,
	index: 				0,
	effect: 			null,
	periodicalTimer: 	null,
	
	setOptions: function(options){
		this.options = {
			interval: 5000,
			duration: 2000
		}
		Object.extend(this.options, options || {});
	},
	
	initialize: function(items, options){
		this.items		= items;
		
		this.setOptions(options);
		if (this.options.duration > this.options.interval) { this.options.interval = this.options.duration + 2000 }
		
		this.showNew(true);
		
		if(this.items.length > 1){
			this.periodicalTimer = this.transist.periodical(this.options.interval, this);
		}
	},
	
	transist: function(){
		//Fade out
		if(window.webkit){
			this.items[this.index].addClass('hidden');
			this.showNew.delay(this.options.duration / 4, this);
		}else{
			this.effect = new Fx.Style(this.items[this.index], 'opacity', {duration: this.options.duration / 4, onComplete: this.showNew.bind(this)});
			this.effect.start(1,0);
		}
	},
	
	showNew: function(firstRun){
		
		// Show next item
		if(firstRun !== true){
			this.items[this.index].addClass('hidden');
			if(this.index == this.items.length -1) { 
				this.index = 0;
			} else { 
				this.index++;
			}
		}
	
		//Fade in
		this.items[this.index].removeClass('hidden');
		if(!window.webkit){
			this.items[this.index].setStyle('visibility', 'hidden');
			this.effect = new Fx.Style(this.items[this.index], 'opacity', {duration: this.options.duration});
			this.effect.start(0,1);
		}		
	}
})

fireOn = Window.ie ? 'load' : 'domready';
window.addEvent(fireOn, function(){
	
	// Load fadeshow
	var spotlightItems	= $$('#spotlight li');
	if(spotlightItems.length > 0){
		new Fadeshow(spotlightItems , {duration: 2000, interval: 8000} );
	}
});