/*
 * 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();
};


JResizeRecorder = {
    build : function(options)
    {
        return this.each(
            function(nr)
            {
                this.recorder = new ResizeRecorder(jQuery(this));
                JResizeRecorder.toolbar(this); 
            }
        );
    },
    toolbar: function(elem)
    {
        var id = 'manager_' + jQuery(elem).attr("id");
        
        jQuery(elem).after("<div id=\""+id+"\"></div>");
        var prev = jQuery('<a href="#">prev</a>').click(function(){
            ResizeRecorder.go_prev(elem);
            return false;
        });
        var next = jQuery('<a href="#">next</a>').click(function(){
            ResizeRecorder.go_next(elem);
            return false;
        });

        jQuery("#"+id).append(prev);
        jQuery("#"+id).append(' &nbsp; ');
        jQuery("#"+id).append(next);
    }
};   
jQuery.fn.JResizeRecorder = JResizeRecorder.build;

$(document).ready(function(){
    

    var add = jQuery('<a href="#">ADD BOX</a>').click(function(){
        var count = jQuery(".box").length + 1;
        var id_div = "div" + count;
        jQuery("#playground").append("<h1>"+ id_div +"</h1><div id=\""+id_div+"\" class=\"box\"></div>");
        var elem = jQuery("#" + id_div);
        elem.resizable({ transparent: true, stop : function(){this.recorder.on_resize()} }).JResizeRecorder();
    });
    var add5 = jQuery('<a href="#">ADD 5 BOX</a>').click(function(){
        var count = jQuery(".box").length + 1;
        for(var x = 0;x < 5;x++){
            var id_div = "div" + (count + x); 
            jQuery("#playground").append("<h1>"+ id_div +"</h1><div id=\""+ id_div +"\" class=\"box\"></div>");
            var elem = jQuery("#" + id_div);
            elem.resizable({ transparent: true, stop : function(){this.recorder.on_resize()} }).JResizeRecorder();
        }
    });
    jQuery("#top").append(add , "&nbsp; | ");
    jQuery("#top").append(add5 , "&nbsp; | ");
});
