var Rotator = Class.create({
	initialize: function(element) {
		
		this.element = $(element);
		this.element.setStyle({ position: "relative" });
		
		this.options = Object.extend({
			duration: 5,
			path: "/",
			images: $A()
		}, arguments[1] || {});
		
		this.current = 0;
		
		if (this.options.images.length > 1)
		{			
			this.leftImage = new Element("img", { 
				src: this.options.path + this.options.images[this.current] 
			}).setStyle({ position: "absolute", top: "0px", left: "0px" });
			
			this.rightImage = new Element("img", {
				src: this.options.path + this.options.images[++this.current] 
			}).setStyle({ position: "absolute", top: "0px", left: "0px", display: "none"});
			
			this.element.insert(this.leftImage);
			this.element.insert(this.rightImage);
			
			var next = this.next.curry(this, this.leftImage, this.rightImage);
									   
			new Effect.Appear(this.leftImage, {
				afterFinish: function() { setTimeout(next, this.options.duration); }.bind(this)
			});
		}
	},
	
	next: function(rotator, faded, shown) {
		new Effect.Parallel($A(new Effect.Fade(faded), new Effect.Appear(shown)), {
			afterFinish: function() {
				setTimeout(function() {
														
					rotator.current = rotator.current == rotator.options.images.length - 1 ? 0 : rotator.current + 1;
					faded.src = rotator.options.path + rotator.options.images[rotator.current];
					
					new Effect.Parallel($A(new Effect.Fade(faded), new Effect.Appear(shown)), {
						afterFinish: function() { setTimeout(rotator.next.curry(rotator, shown, faded), rotator.options.duration); }
					}); 
				}, rotator.options.duration / 10);
			}
		});
	}
});