function Carousel( container )
{
	this.container = $(container);
	this.current_slide = 0;
	this.slides = new Array();
	this.objects = new Array();
	this.carousel_can_slide = true;
	
	// Pre-load arrows
	this.arrows = [new Image(),new Image(),new Image()];
	this.arrows[0].src='/base/img/carousel/arrow1.jpg';
	this.arrows[1].src='/base/img/carousel/arrow2.jpg';
	this.arrows[2].src='/base/img/carousel/arrow3.jpg';
	
	this.add = function(url,img1,img2,img3)
	{
		var images = [new Image(),new Image(),new Image()];
		images[0].src = img1;
		images[1].src = img2;
		images[2].src = img3;
		this.objects.push({url:url,images:images});
	}
	
	this.advance = function()
	{
		// Skip if currently sliding
		if ( !this.carousel_can_slide )
			return;
		
		this.carousel_can_slide = false;
		
		// Add next slide
		this._generate_slide(this.current_slide);
		this.current_slide++;
		if (this.current_slide>=this.objects.length)
			this.current_slide = 0;
		
		// Animate slides
		var slides = this.container.getElementsByClassName('slide');
		var effects = new Array();
	
		switch( slides.length )
		{
			case 1:
				effects.push(new Effect.BiFirstStage(slides[0]));
				break;
			case 2:
				effects.push(new Effect.BiFirstStage(slides[1]));
				effects.push(new Effect.BiSecondStage(slides[0]));
				break;
			case 3:
				effects.push(new Effect.BiFirstStage(slides[2]));
				effects.push(new Effect.BiSecondStage(slides[1]));
				effects.push(new Effect.BiThirdStage(slides[0]));
				break;
			case 4:
				effects.push(new Effect.BiFirstStage(slides[3]));
				effects.push(new Effect.BiSecondStage(slides[2]));
				effects.push(new Effect.BiThirdStage(slides[1]));
				effects.push(new Effect.BiLastStage(slides[0]));
		}
	
		new Effect.Parallel
		(
			effects,
			{
				carousel: this,
				afterFinish:function(obj) {
					var slides = obj.options.carousel.container.getElementsByClassName('placeholder');
					if ( slides.length>3 )
						Element.remove(slides[0]);
				
					obj.options.carousel.carousel_can_slide = true;
					
					// First 3 will be initially shown
					// if (slides.length<3)
					//	obj.options.carousel.advance();	// Slide-in first 3
				}
			}
		);
		
		this.activate_arrows();
	}
	
	this.activate_arrows = function()
	{
		var arrows = this.container.getElementsByClassName('arrow');
		for(var i=0;i<arrows.length;i++)
		{
			arrows[i].controller = this;
			arrows[i].onclick = function(){ this.controller.advance() }
		}
	}
	
	this._generate_slide = function(offset)
	{
		var object = this.objects[offset];
		new Insertion.Bottom
		(
			this.container,
			'<div class="placeholder"><div class="slide">'
			+this._generate_frame_html(object,2)
			+this._generate_frame_html(object,1)
			+this._generate_frame_html(object,0)
			+'</div></div>'
		);
	}
	
	this._generate_frame_html = function(object,frame)
	{
		return '<div class="f'+frame+'" style="display: none;"><a href="'+object.url+'"><img src="'+object.images[frame].src+'" alt=""/></a><img src="'+this.arrows[frame].src+'" alt="" class="arrow"/></div>'
	}
}