/*
 * Copyright (c) 2008 Massimiliano Balestrieri
 * released under the GNU General Public License (GPL2)
 *
 */

if(!window.console)
    window.console = {log : function(){}};

ResizeRecorder = (function(){

    //private static attributes
    var recorders = 0;

    return function(elem){
        
        //private attrs
        var _style;
        var _position = 0;
        var _stack = new Array();
        
        //public attrs
        this.ref = elem.get(0);
        
        function _init_style(style){
             style = style || "";
             _style = style;
             _stack.push(_style);
             _position = _stack.length;
        };

        //privileged methods
        this.log = function(){
            console.log("Stile attuale: " + _style);
            console.log("Posizione history: " + _position);
            console.log("Lunghezza history: " + _stack.length);
        };
        this.update = function(){
            if(_position < _stack.length)
                _stack = _stack.slice(0,_position);
            
            _style = jQuery(this.ref).attr("style");
            _stack.push(_style);
            _position = _stack.length;
        };
        this.prev = function(){
            if(_position > 1){
                _position--;
                _style = _stack[(_position - 1)];
                jQuery(this.ref).attr("style", _style);
            }
        };
        this.next = function(){
            if(_position < _stack.length){
                _position++;
                _style = _stack[(_position - 1)];
                jQuery(this.ref).attr("style", _style);
            }
        };
        //constructor
        recorders++;
        console.log("Incremento il contatore statico di istanze : " + recorders);
        _init_style(jQuery(elem).attr("style"));
        this.log();
    }

})();


ResizeRecorder.prototype  = {
    on_resize: function(){
        //invoco update()
        this.update();
        //loggo sulla consolle
        this.log();
    }
};

ResizeRecorder.go_next = function(ref){
    ref.recorder.next();
    ref.recorder.log();
};
ResizeRecorder.go_prev = function(ref){
    ref.recorder.prev();
    ref.recorder.log();
};

$(document).ready(function(){
    var elem = jQuery("#div1");
    var ref = elem.get(0);
    //istanza del recorder
    var recorder = new ResizeRecorder(elem);
    //aggancio all'elemento DOM il recorder 
    ref.recorder = recorder;
    //inizializzo resizable e allo stop del resize invoco on_resize() dell'oggetto appena "agganciato"
    elem.resizable({ transparent: true, stop : function(){this.recorder.on_resize()}});

    //pulsantiera per navigare lo stack del recorder
    jQuery("#manager1").append('<a href="#" id="prev1">prev</a>');
    jQuery("#manager1").append(' &nbsp; ');
    jQuery("#manager1").append('<a href="#" id="next1">next</a>');

    jQuery("#prev1").click(function(){ResizeRecorder.go_prev(ref);return false;});
    jQuery("#next1").click(function(){ResizeRecorder.go_next(ref);return false;});

});

