/* Inventis Changeshow
 * ----------------------------------------------------------
 * Author:		David Candreva <david@inventis.be>
 * Version:		v1.0
 * Date:		28/05/09
 * Last Edit:	28/05/09 David Candreva <david@inventis.be>
 * ----------------------------------------------------------
 * Inventis - Web Architects - We design the Web!
 * www.inventis.be
 */

var changeShow = new Class({
	
	Implements: [Options],
	
	options: {
		slides: {},
		interval: 5000,
		duration: 2000
	},
	
	index: 0,
	first: false,
	
	initialize: function(element, options){
		this.setOptions(options);
		this.element = element;
		
		this.totalItems = this.options.slides.length;
		this.orginalUrl = this.options.slides[0].get('src');
		
		if (this.options.duration > this.options.interval) { this.options.interval = this.options.duration + 2000 }
		
		
		if(this.totalItems > 1){
		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(){
		
		this.cur = this.options.slides[this.index];
		
		if(this.index == 0){
			$('changeshow-image').src = this.orginalUrl;
		} else {
			$('changeshow-image').src = this.cur.get('src');
		}
		
		var effect = new Fx.Morph('changeshow-image', {duration: this.options.duration});
		effect.start({'opacity': 1});
		
		this.first = true;
		
		//Next index
		if(this.index == this.totalItems -1) { 
			this.index = 0;
		} else { 
			this.index++;
		}

	}
	
});

window.addEvent('domready', function(){
	
	new changeShow($('changeshow'), {
		slides: $('changeshow').getElements('img')
	});
});
