var ImageCrossfader = Class.create(
	{
		initialize: function(container, images, timeout)
		{
			this.container = $(container);
			
			this.images = images;
			this.pe = null;
			this.timeout = (typeof timeout !== "undefined" ? timeout : 5);
			this.index = 0;
			/*
			this.container.insert(
				new Element(
					"img",
					{
						src: this.images[0].image,
						alt: ""
					}
				)
			);
*/
			var srcs = [];
			for (var i = 0; i < images.length; i++)
			{
				var opacity = "opacity: 1";
				
				if(i > 0)
					opacity = "opacity: 0";
			
				srcs.push(images[i].image);
				this.container.insert(
						new Element(
							"img",
							{
								id: container.readAttribute('id')+"-img"+i,
								src: this.images[i].image,
								alt: "",
								style: opacity
							}
						)
					);
			}

			new ImagePreloader(srcs, this.imagesPreloaded.bind(this));
		},

		imagesPreloaded: function()
		{
			this.pe = new PeriodicalExecuter(this.nextImage.bind(this), this.timeout);
		},
	
		nextImage: function()
		{
			if(this.index == 'undefined')
				this.index = 0;
			
			var nextIndex = this.index + 1;
			var id = this.container.readAttribute('id')+'-img';
			if (nextIndex >= this.images.length)
			{
				nextIndex = 0;							
			}
			
			$(id+this.index).setStyle({				
				zIndex:5
			});		
			$(id+nextIndex).setStyle({				
				zIndex:50
			});		
			new Effect.Opacity(
				$(id+nextIndex),
				{
					from: 0,
					to: 1,
					afterFinish: function()
					{
						$(id+this.index).setStyle({
							opacity:0							
						});		
						
						this.index = nextIndex;
												
					}.bind(this)
				}
			);
			
	
		}

	}
);

