
var Carousel = new Class({
	Implements: [ Options, Chain, Events ],

	defaultOptions: function(){
		return {
			imgWidth: 120,
			radiusX: 180, 
			radiusY: 40,
			centerX: 150, 
			centerY: 120,
			speed: 20,
			baseSpeed: 0.0005,
			delay: 10
		};
	},

	initialize: function(el, options){
		this.carousel = $(el);

		if ($defined(this.carousel))
		{
			this.count = 0;
			this.imageDivs = this.carousel.getElementsByTagName('img');
			this.numberOfElements = this.imageDivs.length;
			this.setOptions(this.defaultOptions(), options);
			
			this.position();

			this.timer = this.play.periodical(this.options.delay, this);
			this.carousel.style.background = "none";
		}
	},
	
	position: function(){
		var imageStyle;
		var imageDivWidth;
		var imageDivZIndex;
		var imageOpacite;
		var posX;
		var posY;
		var angle;
		
		for(i=0; i < this.numberOfElements; i++){
		
			angle = (i*( Math.PI*2 )/this.numberOfElements) + (this.count*(this.options.baseSpeed*this.options.speed));
		
			imageStyle = this.imageDivs[ i ].style; 
			imageStyle.position='absolute'; 
			
			posX = ( Math.sin( angle )* this.options.radiusX + this.options.centerX );
			posY = ( Math.cos( angle )* this.options.radiusY + this.options.centerY );
			
			imageStyle.left = posX+"px"; 
			imageStyle.top = posY+"px"
			
			imageDivWidth = Math.round(Math.cos( angle ) * (this.options.imgWidth/3) ) + Math.round(this.options.imgWidth*2/3);
			imageDivZIndex = Math.round(imageDivWidth)+100;
			
			imageStyle.width = imageDivWidth+'px';
			imageStyle.zIndex = imageDivZIndex;

			imageOpacite = ((Math.cos( angle ))*0.6)+0.9;
			imageStyle.opacity = imageOpacite;
			imageStyle.filter = "alpha(opacity=" + 100*imageOpacite + ")";
			
			imageStyle.visibility = 'visible';
		}
	},
	
	play: function(){
		this.count++;
		this.position();
	}

});
