var NFcarousel=new Class(
	{
		initialize:function(container,options)
		{
			this.container=$(container);
			if(!this.container.hasClass('hasCarousel'))
			{
				this.container.addClass('hasCarousel');
				this.slides=[];
				this.buttons=[];
				this.buttonNext=null;
				this.buttonPrev=null;
				this.setOptions(
					{
						onRotate:Class.empty,
						onStop:Class.empty,
						onAutoPlay:Class.empty,
						onShowSlide:Class.empty,
						panelSelector:".panel",
						slidesSelector:".slide",
						buttonsSelector:".button",
						buttonNextSelector:".button-next",
						buttonPrevSelector:".button-prev",
						slideInterval:4000,
						transitionDuration:700,
						transitionEffect:"scroll",
						startIndex:0,
						buttonOnClass:"selected",
						buttonOffClass:"off",
						rotateAction:"none",
						rotateActionDuration:100,
						rotateActionEffect:"scroll",
						autoplay:"on"
					},
				options);
				this.panel=$(container).getElement(this.options.panelSelector);
				this.slides=$(container).getElements(this.options.slidesSelector);
				this.buttons=$(container).getElements(this.options.buttonsSelector);
				this.buttonNext=$(container).getElement(this.options.buttonNextSelector);
				this.buttonPrev=$(container).getElement(this.options.buttonPrevSelector);
				this.showSlide(this.options.startIndex,1);
				if(this.options.autoplay=="on"||this.options.autoplay=="once")this.autoplay();
				this.setupAction(this.options.rotateAction);
				return this
			}
			else 
		return false
		},
		setupAction:function(action){
			if(this.options.rotateAction!='none'){
				this.buttons.each(function(el,idx){
					$(el).addEvent(action,function(){
						if(this.currentSlide!=idx)this.showSlide(idx,this.options.rotateActionDuration,this.options.rotateActionEffect);
						this.stop()
					}
					.bind(this))
				},
			this)}
			if(this.buttonNext&&this.buttonPrev){
				this.buttonNext.addEvent('click',function(){
					if(this.currentSlide+1>=this.slides.length){
						next=0
					}
					else{
						next=this.currentSlide+1
					};
				this.showSlide(next,this.options.rotateActionDuration,this.options.rotateActionEffect);
				this.stop()}.bind(this));
				this.buttonPrev.addEvent('click',function(){
					if(this.currentSlide-1<0){
						next=this.slides.length-1
					}
					else{
						next=this.currentSlide-1
					};
				this.showSlide(next,this.options.rotateActionDuration,this.options.rotateActionEffect);
				this.stop()}.bind(this))
			}
		},
		showSlide:function(slideIndex,fxDuration,fxTransition){
			this.slides.each(function(slide,index){
				var button=$(this.buttons[index]);
				if(index==slideIndex&&index!=this.currentSlide){
					if(button)button.removeClass(this.options.buttonOffClass).addClass(this.options.buttonOnClass)
				}
				else{
					if(button)button.removeClass(this.options.buttonOnClass).addClass(this.options.buttonOffClass)
				}
			},
			this);
			this.fireEvent('onShowSlide',slideIndex);
			this.currentSlide=slideIndex;
			if(fxTransition=='fade'){
				this.fadeSlide(fxDuration)
			}
			else{
				this.scrollSlide(fxDuration)
			}
		},
		scrollSlide:function(fxDuration){
			var scroll=new Fx.Scroll(this.panel,{
				duration:fxDuration
			}).toElement(this.slides[this.currentSlide])
		},
		fadeSlide:function(fxDuration){
			var fade=new Fx.Style(this.panel,'opacity',{
				duration:fxDuration,onComplete:function(){
					new Fx.Scroll(this.panel,{
						duration:1,onComplete:function(){
							new Fx.Style(this.panel,'opacity',{
								duration:fxDuration
							}).start(0.01,1)
						}.bind(this)
					}).toElement(this.slides[this.currentSlide])
				}.bind(this)
			});
			fade.start(1,0.01)
		},
		autoplay:function(){
			this.slideshowInt=this.rotate.periodical(this.options.slideInterval,this);
			this.fireEvent('onAutoPlay')
		},
		stop:function(){
			clearInterval(this.slideshowInt);
			this.fireEvent('onStop')
		},
		rotate:function(){
			if(this.currentSlide+1>=this.slides.length){
				next=0
			}
			else{
				next=this.currentSlide+1
			};
			if(this.options.autoplay=="once"&&next==0){
				this.stop();return
			};
			this.showSlide(next,this.options.transitionDuration,this.options.transitionEffect);
			this.fireEvent('onRotate')
		}
	});
	NFcarousel.implement(new Options);
	NFcarousel.implement(new Events);
