var History = function History(div)
{
    this.redo_stack = new Array();
    this.undo_stack = new Array();
    this.div = div;
};

History.prototype =
{
    push: function(action)
    {
        //Wrapper.free();
        this.undo_stack.push({html:this.div.innerHTML,desc:action});
        this.redo_stack = new Array();
    },

    canUndo: function()
    {
        if(this.undo_stack.length)
            return this.undo_stack.last().desc;
        else
            return 0;
    },

    canRedo: function()
    {
        if(this.redo_stack.length)
            return this.redo_stack.last().desc;
        else
            return 0;
    },

    undo: function()
    {
        if(this.undo_stack.length)
        {
            var state = this.undo_stack.pop();
            this.redo_stack.push({html:this.div.innerHTML,desc:state.desc});
            this.div.innerHTML = state.html;
            //Format.checkDirty(this.div);
            Format.ensureIntegrity(this.div);
        }
    },

    redo: function()
    {
        if(this.redo_stack.length)
        {
            var state = this.redo_stack.pop();
            this.undo_stack.push({html:this.div.innerHTML,desc:state.desc});
            this.div.innerHTML = state.html;
            //Format.checkDirty(this.div);
            Format.ensureIntegrity(this.div);
        }
    },

    deleteFuture: function()
    {
        this.redo_stack = new Array();
    },

    collapse: function(action)
    {
        this.undo_stack.pop();
        this.undo_stack.last().desc = action;
    }
};
