var mssCarousel = new Class({
   options: {
      animation:{
        duration:2000,
        amount:1,
        direction:'horizontal' // 'horizontal' or 'vertical'
      },
      item: {
         classname: "mss-horizontal-scroller-item-",
         size:100
      },
      carouselId:1,
      container:"mss-horizontal-scroller-content-",
      idPrev:"mss-horizontal-scroller-previous-",
      idNext:"mss-horizontal-scroller-next-"
   },
   initialize: function(options){
      this.setOptions(options);
      this.container = this.options.container + this.options.carouselId;
      this.con = $(this.container);
      this.items = $$('.'+this.options.item.classname+this.options.carouselId);
      this.numItems = this.items.length;
      this.totalIniLength = this.options.item.size * this.numItems;
      this.addAmount = this.options.animation.amount * 3; // Number of elements to add to either side of div.
      if(this.addAmount==1){this.addAmount++;}
      this.firstItemLocation = (this.addAmount * this.options.item.size) * -1;
      this.lastItemLocation = ((this.totalIniLength - this.options.item.size) * -1 + this.firstItemLocation);
      
      // initialize location
      this.startLoc = 1 * this.firstItemLocation; // x < 0
      if(this.options.animation.direction=='horizontal'){this.con.setStyle('left',this.startLoc); }
      else if(this.options.animation.direction=='vertical'){this.con.setStyle('top',this.startLoc);}
      
      this.prev = this.options.idPrev + this.options.carouselId;
      this.next = this.options.idNext + this.options.carouselId;
      
      // Add Events
      if($(this.prev)){
         $(this.prev).addEvent('click',function(event){
            event.stop();
            this.scrollPrev();
         }.bind(this));
      }
      if($(this.next)){
         $(this.next).addEvent('click',function(event){
            event.stop();
            this.scrollNext();
         }.bind(this));
      }

      //Add Additional Elements
      // End
      for(var i=0, j=0;i<this.addAmount;i++){
         if(j<this.numItems){
            this.items[j].clone(true, true).inject(this.container, 'bottom');
            j++;
         } else {
            i--;
            j = 0;
         }
      }
      // front
      for(var i=0, k=(this.numItems - 1);i<this.addAmount;i++){
         if(k>=0){
            this.items[k].clone(true, true).cloneEvents(this.items[k]).inject(this.container,'top');
            k--;
         } else {
            i--;
            k = this.numItems - 1;
         }
      }
   },
   
   scrollNext: function(){
      this.newLoc = this.startLoc - (this.options.item.size * this.options.animation.amount);
      var f = new Fx.Morph(this.container, {duration:this.options.animation.duration, transition:Fx.Transitions.Sine.easeInOut});
      
      if(this.options.animation.direction=='horizontal'){
         f.start({'left':[this.startLoc,this.newLoc]}).chain(function(){
            if(this.newLoc<this.lastItemLocation){            
               this.startLoc = this.newLoc + this.totalIniLength;
               this.con.setStyle('left',this.startLoc);
            } else {
               this.startLoc = this.newLoc;
            }
         }.bind(this));
      } else if (this.options.animation.direction=='vertical'){
         f.start({'top':[this.startLoc,this.newLoc]}).chain(function(){
            if(this.newLoc<this.lastItemLocation){            
               this.startLoc = this.newLoc + this.totalIniLength;
               this.con.setStyle('top',this.startLoc);
            } else {
               this.startLoc = this.newLoc;
            }
         }.bind(this));
      }
   
   },
   scrollPrev: function(){
      this.newLoc = this.startLoc + (this.options.item.size * this.options.animation.amount);
      var f = new Fx.Morph(this.container, {duration:this.options.animation.duration, transition:Fx.Transitions.Sine.easeInOut});
      
      if(this.options.animation.direction=='horizontal'){
         f.start({'left':[this.startLoc, this.newLoc]}).chain(function(){
            if(this.newLoc>this.firstItemLocation){
               this.startLoc = this.newLoc - this.totalIniLength;
               this.con.setStyle('left',this.startLoc);
            } else {
               this.startLoc = this.newLoc;
            }
         }.bind(this));
      } else if (this.options.animation.direction=='vertical'){
         f.start({'top':[this.startLoc, this.newLoc]}).chain(function(){
            if(this.newLoc>this.firstItemLocation){
               this.startLoc = this.newLoc - this.totalIniLength;
               this.con.setStyle('top',this.startLoc);
            } else {
               this.startLoc = this.newLoc;
            }
         }.bind(this));
      }

   }
});
mssCarousel.implement(Events);
mssCarousel.implement(Options);
mssCarousel.implement(Chain);
