/* ImgRotator
* @author: matthias bendel // bendel@echonet.at
           sindre wimberger // wimberger@echonet.at
* @version: 1.0 2009-12-01
* @changelog:
* @depencies: prototype, scriptaculous
* @usage: 
		if ($('ImgRotator')) {
			var r = new ImgRotator('ImgRotator');
		}
* @info: needs a list with images 	
*/
var ImgRotator = {
	options: {
		pause: 3, // seconds
		duration: 1.5, //seconds
		loop: true
	},
	initialize: function(container,options) {
		this.container = $(container);
		this.imgs = $$('#' + container + ' li');
		this.imgs.each(function(img, index) { 
			img.setStyle({
				'position':'absolute',
				'top':0,
				'left':0,
				'opacity':0,
				'display':'none'
			});
			if(index == 0 ) {
				img.setStyle({
					'opacity':1,
					'display':'block'
				});		
			}
		}.bind(this));			
		if (this.imgs.length > 1) {
			this.next = 0;
			this.start();
		}
	},
	start: function() {
		this.periodical = new PeriodicalExecuter(this.show.bind(this),this.options.pause);
	},
	stop: function() {
		this.periodical.stop();
	},
	show: function() {
		if (!this.options.loop && this.next==this.imgs.length-1) {
			this.stop();
		}
		this.next = (this.next==this.imgs.length-1)?0:this.next+1;
		var prev = (this.next==0)?this.imgs.length-1:this.next-1;
		
		new Effect.Morph(this.imgs[this.next],{style:"opacity:1",duration: this.options.duration, 
			beforeStart: function(){
				this.imgs[this.next].show();
			}.bind(this)
		});
		new Effect.Morph(this.imgs[prev],{style:"opacity:0",duration: this.options.duration, afterFinish: function(){this.imgs[prev].hide()}.bind(this)});
	}
}; 

