var Carousel = new Class({  
  
  initialize: function(elem, tabs, options) {
    var elem = $(elem);
    var tabs = $(tabs).getElements('.feature');

    // Initialize all tabs by binding them to the slideTo method with their current
    // position as param.
    tabs.each(function(item, index){
      item.addEvent('click', function(e) { this.slideTo(index+1); }.bind(this) );
    }.bind(this));

    // calculate the metrics of the visible div so that we know how and where to scroll  
    this.fx = new Fx.Styles(elem, {duration: 900, transition: Fx.Transitions.Sine.easeInOut, wait: false});
    this.fx2 = new Fx.Styles(elem, {duration: 900, transition: Fx.Transitions.Circ.easeIn, wait: false});

    // Initialize the effect
    
    // size of each slide
    var size = elem.getElement('li.slide').getSize().size;
    
    // number of slides
    var num_elements = $('slides').getElementsByClassName('slide').length;

    // set UL slides container to proper width
    $('slides').setStyle('width', size.x * num_elements+"px");
    
    this.currentPos = 0;
    this.elem = elem;
    this.num_elements = num_elements;
    this.tabs = tabs;
    this.itemWidth = size.x;            

    this.slideTo(0);
  },
    
  previous: function() {
    if (this.currentPos <= 0) {
      this.flingTo(this.num_elements-1);
    } else {
      this.slideTo(this.currentPos-1);
    }        
  },

  next: function() {
    if (this.currentPos >= this.num_elements-1) {
      //this.previous();
      this.flingTo(0);
    } else {
      this.slideTo(this.currentPos+1);
    }
  },

  slideTo: function(pos) {   
    var desiredOffset = pos * this.itemWidth;
  
    this.tabs.removeClass('active');
		this.fx.start({"margin-left" : -desiredOffset + 'px'});			
    this.currentPos = pos;
    if (pos >= 1) {
      this.tabs[pos-1].addClass('active'); 
    }
  },
  
  flingTo: function(pos) {
    var desiredOffset = pos * this.itemWidth;    
    this.tabs.removeClass('active');
    
    this.fx2.start({"margin-left" : -desiredOffset + 'px'});			
    this.currentPos = pos;      
  }
  
});