/* CREDITS
 * ----------------------------------------------------------
 * Title:		Inventis Changeshow
 * Author:		Tom Claus <tom@inventis.be>
 * Co-Author:	David Candreva <david@inventis.be>
 * Date:		01/09/2009
 * Last Edit:	01/09/2009 [Author]
 * ----------------------------------------------------------
 * Inventis - Web Architects - We design the Web!
 * www.inventis.be
*/



var changeShow = new Class({
	
	Implements: [Options],
	
	options: {
		slides: {},
		interval: 5000,
		duration: 2000
	},
	
	index: 0,
	
	initialize: function(element, options){
		this.setOptions(options);
		this.element = element;
		
		this.totalItems = this.options.slides.length;
		if (this.options.duration > this.options.interval) { this.options.interval = this.options.duration + 2000 }

		this.next();
		this.periodicalTimer = this.transist.periodical(this.options.interval, this);
	},
	
	transist: function(){
		this.myFx = new Fx.Morph('changeshow-image', {duration: this.options.duration / 4, onComplete: this.next.bind(this)});
		this.myFx.start({'opacity': 0});
	},

	
	next: function(){
		
		if(this.cur){
			this.cur.removeClass('active');
		}
		
		this.cur = this.options.slides[this.index];
		this.cur.addClass('active');
		
		$('changeshow-image').getElement('img').src = this.cur.getElement('img').get('src');
		
		var effect = new Fx.Morph('changeshow-image', {duration: this.options.duration});
		effect.start({'opacity': 1});
		
		//Next index
		if(this.index == this.totalItems -1) { 
			this.index = 0;
		} else { 
			this.index++;
		}

	}
	
});

window.addEvent('domready', function(){
	
	new changeShow($('changeshow'), {
		slides: $('changeshow').getElements('li')
	});
});