var carousel;
$(document).ready(function(){
  $('#carousel a').click(function (event) {
    event.preventDefault();
    event.stopPropagation();
	  var id = $(this).attr('id');
    $('#' + carouselTargetFieldID).val(id);
  });
  carousel = new Carousel();
  $('#scrollright').click(function (event) {
    event.preventDefault();
    event.stopPropagation();
	  carousel.scrollRight();
  });
  $('#scrollleft').click(function (event) {
    event.preventDefault();
    event.stopPropagation();
	  carousel.scrollLeft();
  });
});

function Carousel() {
  this.x = 0;
  this.index = 0;
  this.length = $('#destinations td').size();
  this.domobj = $('#destinations');
}
Carousel.ITEM_WIDTH = 124;

Carousel.prototype = {
  scrollLeft: function() {
    if(this.index > 0) {
      if(this.index - 4 < 0) this.index = 0;
      else this.index -= 4;
      this.scroll();
    }
  },
  scrollRight: function() {
    if(this.index < this.length - 4) {
      if(this.index + 4 > this.length - 4) this.index = this.length - 4;
      else this.index += 4;
      this.scroll();
    }
  },
  scroll: function() {
    var newpos = -(this.index * Carousel.ITEM_WIDTH);
    this.domobj.animate({
      left: newpos + 'px'
    }, {
      duration: 'slow',
      queue: true
    });
  }
}
