var Carousel = new Class({
	Implements:Options,
	options:{
		arrows:'',
		indicator:'',
		indicatorEls:'',
		indicatorElsActiveClass:'',
		right:'',
		left:'',
		arrows:'',
		position:'top',
		positionOffset: 0,
		perPage: 1,
		speed:400,
		autoRotate:false,
		rotateSpeed:7000
	},
	initialize: function(el,options){
		this.setOptions(options);
		this.instance = el;
		this.theImages = el.getElements(options.slideElements);
		this.slideWidth = options.slideWidth;
		//set carousel slider width
		this.sliderWidth = this.theImages.length * this.slideWidth;
		el.setStyle('width', this.sliderWidth);
		
		this.arrows =  $$(options.arrows);
		this.leftClass = options.left;
		this.rightClass = options.right;
		if($(options.indicator)){
			this.indicator = $(options.indicator);
			this.parent = this.indicator.getParent();
			this.parentSize = this.parent.getSize();
			this.position = options.position;
			this.positionOffset = options.positionOffset;
			this.positionIndicator();
			this.arrows.addEvent('click',this.slide.bindWithEvent(this));
			
			for(var i = 0; i < this.theImages.length;i++){
				$('indicator-wrapper').innerHTML +='<span class=""></span>';
			}
			
			$$('#indicator-wrapper span').setStyle('cursor','pointer');
			
			if(!options.multiSlide){
				this.parentSize.x = options.slideWidth;
			} else {
				$('indicator-wrapper').innerHTML = "";
				for(var i = 0; i < (this.theImages.length / options.divider);i++){
					$('indicator-wrapper').innerHTML +='<span class=""></span>';
				}
			}
		}
		
				
		
		this.perPage = options.perPage;
		this.indicatorEls = $$(options.indicatorEls);
		$$(options.indicatorEls).addEvent('click', this.slideTo.bind(this));
		if(!this.indicatorEls[0].hasClass('on')){
			this.indicatorEls[0].addClass('on');
		}
		this.indicatorElsActiveClass = options.indicatorElsActiveClass;
		this.inMotion = false;
		if(options.autoRotate){
			this.startDelay();	
		}
		//$clear(this.delayFunction);
		
		
		
		
	},
	startDelay: function(){
		this.delayFunction = this.autoRotate.periodical(this.options.rotateSpeed,this);	
	},
	
	positionIndicator: function(){
		var indicatorWidth = this.indicator.getSize();
		var arrowsWidth = this.arrows[0].getSize();
		var arrowsTotal = arrowsWidth.x;
		var totalWidth = arrowsTotal + indicatorWidth.x;
		this.indicator.setStyle('left', (this.parentSize.x / 2) - (totalWidth / 2));
		this.indicator.setStyle(this.position, this.positionOffset);
	},
	
	slide: function(e){
		if(this.options.autoRotate){
			$clear(this.delayFunction);
		}
		var num = 0;
		
		var sliderPos = this.instance.getStyle('left').toInt();
		if(e.target.hasClass(this.rightClass)){
			if(sliderPos == (-this.sliderWidth + this.parentSize.x)){
				var num = 0;
			} else {
				var num =(sliderPos) - this.parentSize.x;	
			}
		} else {
			if(sliderPos == 0){
				var num = (-this.sliderWidth) + this.parentSize.x;
			}	else {
				var num = sliderPos + this.parentSize.x;	
			}
		}
		if(this.inMotion){
			return false;
		} else {
			this.inMotion = true;
			
			new Fx.Morph(this.instance,{duration:this.options.speed,onComplete:function(){
				this.updateIndicator(-1 * (num / this.parentSize.x));
				this.inMotion = false;
				if(this.options.autoRotate){
					this.startDelay();
				}
				
			}.bind(this)}).start({
				'left':num
			});
		}	
	},
	
	updateIndicator: function(num){
		for(var i = 0; i < this.indicatorEls.length;i++){
			if(this.indicatorEls[i].hasClass(this.indicatorElsActiveClass)){
				this.indicatorEls[i].removeClass(this.indicatorElsActiveClass);
			}	
		}
		this.indicatorEls[num].addClass(this.indicatorElsActiveClass);
			
	},
	slideTo: function(e){
		e.stop();
		if(this.options.autoRotate){
			$clear(this.delayFunction);
		}
		 
		if(this.inMotion){
			return false;
		} else {
			this.inMotion = true;
			for(var i = 0; i < this.indicatorEls.length;i++){
				if(this.indicatorEls[i] == e.target){
					var updateNum = i;
					var num = -i * this.parentSize.x; 
				}
			}
			
			new Fx.Morph(this.instance,{duration:this.options.speed,onComplete: function(){
					this.inMotion = false;
					if(this.options.autoRotate){
						this.startDelay();
					}
				}.bind(this)}).start({
					'left':num
			});
			this.updateIndicator(updateNum);
		}
	},
	autoRotate: function(){
		if(this.inMotion){
			return false;
		} else {
			this.inMotion = true;
			var sliderPos = this.instance.getStyle('left').toInt();
			if(sliderPos == (-this.sliderWidth + this.parentSize.x)){
				var moveTo = 0;
			} else {
				var moveTo = sliderPos - this.parentSize.x;
			}
			
			new Fx.Morph(this.instance,{duration:this.options.speed,onComplete:function(){
					this.updateIndicator(-1 * (moveTo / this.parentSize.x));
					this.inMotion = false;
				}.bind(this)}).start({
					'left':moveTo
				});
			}
		
		
	}
	
});