var Carousel = new Class({
						 
		Implements: [Options, Events],
		
		options: {
			transition:'fade',
			autoPlay:true,
			startPosition:0,
			interval:5000,
			fxFadeOptions: Fx.prototype.options,
			fxSlideOptions: {duration: 'long',transition: 'quad:in:out'},
			onPlay: $empty,
			onStop: $empty,
			onChange: $empty
		},
		
		itemsContainer:null, 
		buttons:null, 
		fxFade:null, 
		fxSlide:null, 
		itemWidth:null, 
		length:null, 
		timer:null,
		moveTo:$empty,
		
		initialize: function(itemsMaskID,thumbsMaskID,options) {
			this.setOptions(options);
			
			this.mask = document.id(itemsMaskID);
			this.itemsContainer = this.mask.getFirst('ul');
			this.buttons = document.id(thumbsMaskID);
			this.fxFade = new Fx.Morph(this.itemsContainer, this.options.fxFadeOptions);
			this.fxSlide = new Fx.Morph(this.itemsContainer, this.options.fxSlideOptions);
			this.itemWidth = this.itemsContainer.getSize().x;
			this.length = this.itemsContainer.getElements('li').length;
			
			switch(this.options.transition) {
				case 'slide':
					this.moveTo = this.slideTo;
					break;
				default:
					this.moveTo = this.fadeTo;
			} 
			
			if($defined(this.buttons)) {
				this.buttons.getElements('li a').each(function(item, index, array) {
					item.addEvent('click',function(event){
						event.stop();
						array[index].blur();
						this.stop();
						this.moveTo(index);
					}.bind(this));
				},this);
			}
			this.reset();
		},
		
		reset: function() {
			this.mask.setStyles({
				position:'relative'
			});
			this.itemsContainer.setStyles({
				position:'relative',
				left:'0px'
			});
			this.itemsContainer.setStyle('width', this.itemWidth * this.length);
			this.jumpTo(this.options.startPosition);
			if(this.options.autoPlay) {
				this.play();
			}
		},
		
		play: function() {
			this.fireEvent('onPlay',this.getCurrentPosition());
			this.timer = this.moveTo.periodical(this.options.interval,this);
		},
		
		stop: function() {
			myTimer = $clear(this.timer);
			this.fireEvent('onStop',this.getCurrentPosition());
		},
		
		getCurrentPosition: function() {
			return (-1 * this.itemsContainer.getStyle('left').toInt() / this.itemWidth).toInt();
		},
		
		setIndex: function(index) {
			var currentPosition = this.getCurrentPosition();
			if(!$defined(index)) {
				index = currentPosition + 1;
			}
			var nextPosition = index + 1;
			if(index < 0 || index > this.length-1) {
				index = 0;
				nextPosition = 1;
			}
			this.fireEvent('onChange',[currentPosition,index,nextPosition]);
			return index;
		},
		
		jumpTo: function(index) {
			index = this.setIndex(index);
			this.itemsContainer.setStyle('left', -1 * index * this.itemWidth);
		},
		
		fadeTo: function(index) {
			index = this.setIndex(index);
			var newLeft = -1 * index * this.itemWidth;
			this.fxFade.start({
				opacity: 0
			}).chain(function() {
				this.set({left: newLeft});
				this.start({opacity: 1});
			});
		},
		
		slideTo: function(index) {
			index = this.setIndex(index);
			var newLeft = -1 * index * this.itemWidth;
			this.fxSlide.start({
				left: newLeft
			});
		}
		
});
