/*
   Copyright 2010 Hanov Solutions Inc. All Rights Reserved

   steve.hanov@gmail.com
 */

 /** @constructor */
function UndoStack()
{
    this.items = [];
    this.next = 0;
    this.dirty = false;
}

UndoStack.prototype = {
    execute: function( action, alreadyDone )
    {
        if ( this.next < this.length ) {
            this.items.slice( this.next, this.length - this.next );
        }
        
        this.items.push( action );
        if ( !alreadyDone ) {
            action.execute();
        }
        this.dirty = true;
        this.next = this.items.length;
    },

    undo: function()
    {
        if ( this.canUndo() ) {
            this.items[--this.next].undo();
            this.dirty = true;
        }
    },

    redo: function() 
    {
        if ( this.canRedo() ) {
            this.items[this.next++].execute();
            this.dirty = true;
        }
    },

    canUndo: function()
    {
        return this.next > 0;
    },

    canRedo: function()
    {
        return this.next < this.items.length;
    },

    getUndoDescription: function()
    {
        return this.items[this.next-1].description;
    },

    getRedoDescription: function()
    {
        return this.items[this.next].description;
    },

    clear: function()
    {
        this.items.length = 0;
        this.next = 0;
        this.dirty = false;
    }
};
