//
//
// file created in 2009/05/26 17:37:44.
// LastUpdated :2009/05/26 18:32:57.
// author iNo
//

var Manju = function(){
    this.elem;
    this.hidden = false;
    this.setup();
}
Manju.prototype = {
    setup: function(){
        this.elem = $('<div class="manju">MANJU!</div>');
        $(document.body).append(this.elem);
        var self = this;
        this.elem.click(function(){
            if(self.hidden){
                self.show();
            }else{
                self.hide();
            }
        });
    },
    hide: function(){
        this.elem.css("background","#cccccc");
        this.hidden = true;
    },
    show: function(){
        this.elem.css("background","#000000");
        this.hidden = false;
    }
}


var manju = function() {
    var that = {};
    var elem;
    var visibility = true;
    var setVisibility = function(flag) {
        visibility = flag;
    };
    var setBgColor = function(color) {
        elem.css('background', color);
    };
    that.setup = function() {
        elem = $('<div class="manju">MANJU!</div>');
        $(document.body).append(elem);
        elem.click(function() {
            if (visibility) {
                setBgColor('#cccccc');
                setVisibility(false);
            } else {
                setBgColor('#000000');
                setVisibility(true);
            }
        });
    };
    return that;
};

$(function() {
    var NUMBERS = 200;

    // use closure
    var m = [];
    var closureStartTime = new Date();
    for (var i=0; i<NUMBERS; i++) {
        m[i] = manju();
        m[i].setup();
    }
    var closureEndTime = new Date();
    var closureTime = closureEndTime.getTime() - closureStartTime.getTime();

    // use prototype
    var n = [];
    var prototypeStartTime = new Date();
    for (var j=0; j<NUMBERS; j++) {
        n[j] = new Manju();
    }
    var prototypeEndTime = new Date();
    var prototypeTime = prototypeEndTime.getTime() - prototypeStartTime.getTime();

    // result on Firefox 3.0.10, closure: 1638, prototype: 1608
    // result on Safari 3.2.3, closure: 489, prototype: 465
    $('#debug').text('closure: ' + closureTime + ', prototype: ' + prototypeTime);
});


/*
vim:fdl=0 fdm=marker:ts=4 sw=4 sts=0:
*/

