
var GalleryScroller = Class.create();
GalleryScroller.prototype = {

  initialize: function(container, previous_button, next_button, total_count, count_per_page, center_on, pagination) {
    this.page_count = Math.ceil(total_count / count_per_page);
    this.previous_button = $(previous_button);
    this.next_button = $(next_button);
    this.container = $(container);
    this.pagination = $(pagination);
    this.current_page = 1;
    if (this.page_count > 1) {
      this.total_count = total_count;
      this.count_per_page = count_per_page;
      this.disable_buttons = false;
      this.width = Element.getWidth(this.container.parentNode);
      this.element_width = Math.ceil(this.width / this.count_per_page);
      this.total_width = this.element_width * this.total_count;
      this.paginate();
      this.center(center_on || 1);
      Event.observe(this.next_button, 'click', this.next.bindAsEventListener(this)); 
      Event.observe(this.previous_button, 'click', this.previous.bindAsEventListener(this)); 
    } else this.disable_buttons = true;
  },
  
  paginate: function() {
    if (null == this.pagination) return false;
    for (i = 1; i <= this.page_count; i++) {
      var a = document.createElement('A');
      a.href = 'javascript:void(0);';
      a.title = i;
      a.appendChild(document.createTextNode(i));
      Event.observe(a, 'click', function(evt) {this.page(parseInt(evt.element().title) || 1);}.bind(this));
      this.pagination.insert(a);
    }
  },
  
  page: function(page_to) {
    this.move_to(((page_to-1)*this.count_per_page)+1);
    this.current_page = page_to;
  },

  next: function() {
    if (this.disable_buttons || this.onLastPage()) return false;
    this.disable_buttons = true;
    this.current_page++;
    this.move_to(((this.current_page-1)*this.count_per_page)+1);
    return false;
  },

  previous: function() {
    if (this.disable_buttons || this.onFirstPage()) return false;
    this.disable_buttons = true;
    this.current_page--;
    this.move_to(((this.current_page-1)*this.count_per_page)+1);
    return false;
  },
  
  center: function(center_on) {
    this.move_to(center_on, true);
    this.current_page = Math.ceil(center_on/this.count_per_page);
  },
  
  move_to: function(start_on, immediate) {
    var position = this.itemPosition(start_on, immediate && start_on > 1);
    if (immediate) {
      Element.setStyle(this.container, {width: this.total_width + 'px',left: -(position || 0) + 'px'});
      this.finishMove();
    } else {
      var left = parseFloat(this.container.getStyle('left') || '0');
      target = (position - Math.abs(left))*-1;
      new Effect.Move(this.container, {x: target, mode: 'relative', afterFinish: this.finishMove.bind(this)});
    }
  },
  
  itemPosition: function(index, center) {
    var position = (index-(Math.floor(center ? this.count_per_page/2 : 1)))*this.element_width;
    var right_bound = this.total_width - this.width;// Left Boundary
    return((position > right_bound) ? right_bound : position);
  },

  finishMove: function() {
    this.updateButtons();
    this.disable_buttons = false;
  },

  updateButtons: function() {
    this.onFirstPage() ? Element.removeClassName(this.previous_button, 'active') : Element.addClassName(this.previous_button, 'active');
    this.onLastPage() ? Element.removeClassName(this.next_button, 'active') : Element.addClassName(this.next_button, 'active');
    var links = this.pagination.select('a');
    links.each(function(a) {a.removeClassName('selected')});
    links[this.current_page-1].addClassName('selected');
  },

  onFirstPage: function() {
    return parseFloat(this.container.getStyle('left') || '0') >= 0;
  },
  
  onLastPage: function() {
    return (this.width - parseFloat(this.container.getStyle('left') || '0')) >= this.total_width;
  }
};

