
var slidingCarousel = new Class({
	Implements:[Events,Options],
	options:{
		leftNav:'navleft',
		rightNav:'navright',
		slideClass:'.article',
		buttonClass:'',
		distanceToMove:'240px',
		current:0,
		navLeftId:'',
		navRightId:'',
		interval:4000
	},
	initialize:function(options){
		
		this.current = this.options.current;
		this.setOptions(options);
		this.max = $$(this.options.slideClass).length - 1;
		this.interrupt = false; //registers a click override
		this.nextSlide = this.current+1;
		this.moving = false;
		
		$$(this.options.slideClass).setStyles({
			'display':'block',
			'position':'absolute',
			'top':0,
			'left':this.options.distanceToMove,
			'float':'none'
		});
		
		$$(this.options.slideClass).each(function(el,i){					
			if(i == this.current){
				$(el).setStyle('left','0px');
			}
		},this);
		
		var autoRotate = this.doSlide.periodical(this.options.interval,this);		
		
		//setup direct slide access buttons
		$$(this.options.buttonClass).each(function(el, index){
			
			if(index == this.current){el.addClass('button-on');};
		
			el.addEvent('click',function(){
				if(this.moving==false && index!=this.current){					
					this.current = index - 1;
					autoRotate = $clear(autoRotate);
					this.doSlide();
				}
			}.bind(this));	
		},this);
		
		//set up navleft - untested!
		if(this.options.navLeftId!=''){		
			if($(this.options.navLeftId)){
				$(this.options.navLeftId).addEvent('click',function(){
					if(this.moving==false && index!=this.current){					
						this.current==0 ? this.current=this.max : this.current=this.current-1;
						autoRotate = $clear(autoRotate);
						this.doSlide();
					}
				}.bind(this));	
			}
		}
		
		//set up navright - untested!
		if(this.options.navRightId!=''){		
			if($(this.options.navRightId)){
				$(this.options.navRightId).addEvent('click',function(){
					if(this.moving==false && index!=this.current){					
						this.current==this.max ? this.current=0 : this.current=this.current+1;
						autoRotate = $clear(autoRotate);
						this.doSlide();
					}
				}.bind(this));	
			}
		}
	},
	
	doSlide:function(){		
       
            $$(this.options.slideClass).each(function(el, i){
                if(i != this.current){
                    el.setStyle('left',this.options.distanceToMove);
                   // el.setStyle('opacity',1);    
                    //var myInner = el.get('html');
					//el.empty();
					//el.set('html',myInner);		
					//el.setStyle('opacity',1);                   
                }       
            },this);
            
            this.nextSlide = this.current+1;
            if(this.nextSlide>this.max){
                this.nextSlide = 0;
            }
            
            $$(this.options.slideClass).each(function(el, index){
                                    
                if(index == this.current){
					
					var moveLeft = parseInt(this.options.distanceToMove) * -1;
					//$(el).tween('left',moveLeft);
					
					var myTween = new Fx.Tween(el);
					//el.set('tween',{duration:'long',transition:Fx.Transitions.Quint.easeOut});
					myTween.start('left',0,moveLeft);//.chain(
						//function(){el.setStyle('left',730);}
					//);
					
					
					
					//myChain.callChain(el,moveLeft);
					//myChain.callChain(el);
					//myChain.callChain(el,this.options.distanceToMove);
					//myChain.callChain(el);
				};
                
                if(index==this.nextSlide){
                    var mFunc = function(){
						var moving;
						var that = this;
                        var myFx = new Fx.Morph(el,{
                            duration:'long',
                            transition:Fx.Transitions.Quint.easeOut,
                            onComplete:function(){
                                this.moving=false;
                            }.bind(this),
                            onStart:function(){
                                this.moving=true;
                            }.bind(this)    
                        });
                        myFx.start({'left':0});
                    }.bind(this);
                    mFunc.delay(1);
                    
                    if(this.options.buttonClass!=''){$$(this.options.buttonClass)[index].addClass('button-on');};
                }else{
					if(this.options.buttonClass!=''){$$(this.options.buttonClass)[index].removeClass('button-on');};
                };
            },this);
            this.current++;
            if(this.current>this.max){
                this.current = 0;
            }            
    }
	
});

