// Uses globals, so currently only supports one per page

function FeaturedAnimator(_length, _main_window_id, _play_button_id, _pause_button_id) {
  this.length = _length;
  this.main_window_id = _main_window_id;
  this.play_button_id = _play_button_id;
  this.pause_button_id = _pause_button_id;
  this.current = 0
  this.playing = false;
  this.currentTimer = null;
  this.advanceTime = 10;
    
  this.next = function () {
    old = this.current;
    this.current++;                          

    new Animator().addSubject(new NumericalStyleSubject($(this.main_window_id), 'left', (old*-100), (this.current*-100), '%')).play();

    this.current %= this.length;

    this.setAdvanceTimer();
  }
  
  this.prev = function () {
    old = this.current;

    if(old == 0) {
      old = this.length;
    }

    this.current = old - 1;

    new Animator().addSubject(new NumericalStyleSubject($(this.main_window_id), 'left', (old*-100), (this.current*-100), '%')).play();

    this.setAdvanceTimer();
  }
  
  this.setAdvanceTimer = function () {
    if(this.currentTimer) {
      this.currentTimer.stop();
      this.currentTimer = null;
    }
    animator_instance = this;

    if(this.playing) {
      this.currentTimer = new PeriodicalExecuter(function(pe) {
        animator_instance.next();
        pe.stop();
      }, this.advanceTime);
    } else {
      this.currentTimer = null;
    }
  }
  
  this.pause = function() {
    this.playing = false;
    this.setAdvanceTimer();

    $(this.pause_button_id).style.display = "none";
    $(this.play_button_id).style.display = "";
  }
  
  this.play = function() {
    this.playing = true;
    this.setAdvanceTimer();

    $(this.pause_button_id).style.display = "";
    $(this.play_button_id).style.display = "none";
  }
   
}


