/* {{{ GPL

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

}}} */

var History = Class.extend({

  // Saves a set of Undo objects, to provide undo/redo feature

  // {{{ init  
  init: function( animation ) {
    this.animation = animation;
    this.limit = 20;

    this.undos = [];
    this.currentPosition = 0;
  },
  // }}}
  
  // {{{ add
  add: function( undo ) {
  
    undo.animation = this.animation;
    undo.frameNumber = this.animation.getCurrentFrameNumber();
    
    var ahead = this.undos.length - this.currentPosition;
    if ( ahead > 0 ) {
      this.undos.splice( this.currentPosition, ahead );
    }
    
    if ( this.undos.length > this.limit ) {
      this.undos.splice( 0, 1 );
    }
    
    this.undos[ this.undos.length ] = undo;
    this.currentPosition = this.undos.length;
    this.animation.saved = false;
  },
  // }}}
  
  // {{{ canUndo
  canUndo: function() {
    return this.currentPosition > 0;
  },
  // }}}
  
  // {{{ undo
  undo: function() {
    this.animation.saved = false;
    this.currentPosition -= 1;
    var item = this.undos[ this.currentPosition ];
    return item.undo();
  },
  // }}}

  // {{{ canRedo
  canRedo: function() {
    return this.currentPosition < this.undos.length;
  },
  // }}}
        
  // {{{ redo
  redo: function() {
    this.animation.saved = false;
    var item = this.undos[ this.currentPosition ];
    this.currentPosition += 1;
    return item.redo();
  },
  // }}}
});

History.prototype.log = new Log( "History" );

