var ProductSlider = Class.create();
ProductSlider.prototype = {
  // To move is the div that is getting moved.  
  initialize: function(toMove, width, numberOfSlides) {
    this.toMove = toMove;
    this.width = width;
    this.numberOfSlides = numberOfSlides;
    this.current = 0;
    this.threadLock = 0;
  },

  next: function() {
    if(this.canLock()) {
      this.lock();
      if(this.current < this.numberOfSlides - 1) {
        new Effect.Move($(this.toMove), { x: -this.width, y: 0, transition: Effect.Transitions.sinoidal, queue: { position: 'end', scope: 'global', limit: 1 }});
        this.current += 1;
        if(this.onMove != null)
          this.onMove(this.current);
      }
      this.unlock();
    }
  },

  previous: function() {
    if(this.canLock()) {
      this.lock();
      if(this.current > 0) {
        new Effect.Move($(this.toMove), { x: this.width, y: 0, transition: Effect.Transitions.sinoidal, queue: { position: 'end', scope: 'global', limit: 1 }});
        this.current -= 1;
        if(this.onMove != null)
          this.onMove(this.current);
      }
      this.unlock();
    }
  },
  
  moveTo: function(current) {
    var move = 0;
    var pages = Math.abs(this.current - current);
    if(this.current < current) 
      move = -(this.width * pages); 
    else if(this.current > current)
      move = this.width * pages;
    new Effect.Move($(this.toMove), { x: move, y: 0, transition: Effect.Transitions.sinoidal, queue: { position: 'end', scope: 'global', limit: 1 }});
    this.current = current;
    if(this.onMove != null)
      this.onMove(this.current);
  },
  
  lock: function() {
    this.threadLock = 1;
  },
  
  unlock: function() {
    // Unlock the lock in 500 milliseconds, this should give enough time for
    // the effects to complete.
    setTimeout("slider.doUnlock();", 1000);
  },
  
  doUnlock: function() {
    this.threadLock = 0;
  },
  
  canLock: function() {
    return this.threadLock == 0;
  }
}
