
/* classes/scroll.js */

 
/* classes/scroll.js */

 Scroll = Class.create();
Object.extend(Scroll.prototype, {
  initialize: function(previous_state, first_current_item, last_current_item, total_items, config, dom, callbacks) {
    this.previous_state = previous_state;
    this.first_current_item = first_current_item;
    this.last_current_item = last_current_item;
    this.total_items = total_items;
    this.width = PixelMath.getCompleteElementWidth(dom.li_items[0]);
    var base_config = Object.extend({}, {
        total_per_items : total_items,
        per_item       : 1
    });
    this.config = Object.extend(base_config, config || {});
    this.dom = dom;
    this.callbacks = callbacks;
  },

  next: function() {
    if (this.last_current_item < this.total_items) {
      this.calculatePositionInfo(true);
      this.setPositionInfo();
      this.run(this.toOffset());
    }
    return [this.first_current_item, this.last_current_item];
  },

  previous: function() {
    if (this.first_current_item > 1) {
      this.calculatePositionInfo(false);
      this.setPositionInfo();
      this.run(this.toOffset());
    }
    return [this.first_current_item, this.last_current_item];
  },

  toOffset: function() {
    var end = this.first_current_item;
    var offset = 0;
    for( i = 0; i< end - 1; i++){
        var element = this.dom.li_items[i];
        offset += PixelMath.getCompleteElementWidth(element);
    }
    return -(offset);
    // TODO: loop effect
  },

  move: function() {
    this.setPositionInfo();
    this.run(this.toOffset());
  },

  slide: function(previous_state){
  	var start = this.previous_state;
  	var end = this.first_current_item;
    this.setPositionInfo();
    this.run(this.toOffset());

  },
  calculatePositionInfo: function(is_forward) {
    if (is_forward) {
      if (this.total_items - this.last_current_item < this.config.slide_by) {
        this.first_current_item += this.total_items - this.last_current_item;
      } else {
        this.first_current_item += this.config.slide_by;
      }
    } else {
      if (this.first_current_item - this.config.slide_by < 1) {
        this.first_current_item = 1;
      } else {
        this.first_current_item -= this.config.slide_by;
      }
    }
    if (this.config.window_size == 1) {
      this.last_current_item = this.first_current_item;
    } else {
      this.last_current_item = this.first_current_item + (this.config.window_size - 1);
    }
  },

  setPositionInfo: function() {
    if (this.dom.first_current_item) {
      this.dom.first_current_item.innerHTML = 1 + (this.config.per_item * (this.first_current_item-1));
    }
    if (this.dom.last_current_item) {
      last = this.config.per_item + (this.config.per_item * (this.last_current_item-1));
      this.dom.last_current_item.innerHTML = last > this.config.total_per_items ? this.config.total_per_items : last;
    }
    if (this.dom.total_items) {
      this.dom.total_items.innerHTML = this.config.total_per_items ? this.config.total_per_items : this.total_items;
    }
  },

  run: function(to_offset) {
    new Effect.Move(this.dom.ul_item, {
      x: to_offset, y: 0, mode: 'absolute', duration: this.config.duration,
      beforeStart: (function() {
        if(this.callbacks.beforeSlide) {
          this.callbacks.beforeSlide();
        }
      }.bind(this)),
      afterFinish: (function() {
        this.callbacks.afterSlide();
      }.bind(this))
    });
    return true;
  }
});
