var StaticCarousel = Class.create({
  
  // Constructor
  initialize: function(carouselElement, options) {
    if (carouselElement == null) return;
    this.options = Object.extend({
      elementSize: 142,
      visible: 4,
      increment: 4
		}, options || {});
		

		// Gallery carousel event bindings
		Event.observe("prev_carousel_arrow", "click", this._prevScroll.bindAsEventListener(this));
		Event.observe("next_carousel_arrow", "click", this._nextScroll.bindAsEventListener(this));
		
		// Carousel elements
		this.carousel       = carouselElement
		this.window			    = this.carousel.select('.window').first();
		this.slider         = this.carousel.select('.slider').first();
		this.carousel_table = this.carousel.select('.carousel_table').first();
		this.carousel_row   = this.carousel.select('.carousel_table_row').first();
		this.size           = this.carousel_row.select('td').length;

    // initalise the this.elementSize value
    // this._getElementSize();
    this.elementSize = 142;

    // Tracking variables
    this.current_id = 0;     // id of the picture on the left
    // this.increment  = 4;     // number of pictures to slide at once
    // this.visible    = 4;     // number of pictures visible
    this.moving     = false; // indicates if the slider is moving
  },
  
	// ===================
	// = Previous Button =
	// ===================
  _prevScroll: function(event) {
    if (this.current_id != 0) {
  		this.scrollDirection = "prev";
      this._scroll(this.options.increment);      
    }
  },

	// ===============
	// = Next Button =
	// ===============
  _nextScroll: function(event) {
    if (this.current_id < (this.size - this.options.visible)) {
  		this.scrollDirection = "next";
      this._scroll(this.options.increment);
    }
  },

  // =================
  // = Scroll Action =
  // =================
  _scroll: function(delta) {
    if (this.moving) return;
		if(this.scrollDirection == "next") {
      // calculate the number of pics that overlap the right hand side
      var offset = this.size - this.current_id - this.options.visible

      if(offset < delta) delta = offset;
  		// if we are moving to the next images the offset needs to be negative
			delta = -delta;		  
		} else {
      // calculate the number of pics that overlap the left hand side		  
      var offset = this.current_id - delta;
      if(offset < 0) delta = this.current_id;
		}
    // increment or decrement the current_id (depending on the direction)
		this.current_id -= delta;		

    // move the slider based on the delta value and element width
		new Effect.MoveBy(this.slider, 0, delta * this.options.elementSize, {
		  duration: 0.5,
		  beforeStart: function() {
		    this.moving = true;
		  }.bind(this),
		  afterFinish: function() {
		    this.moving = false;
		  }.bind(this)
		});		
  },
  
  // ================
  // = Calculations =
  // ================
  _getElementSize: function() {
    var td = this.carousel_row.select('td').first();
		this.elementSize = td.getDimensions().width + parseFloat(td.getStyle("border-right-width")) + parseFloat(td.getStyle("border-left-width"));
  }
});