//ԹϣʽCookie
//ΪCookieʽַʽļֵ,ԿԸϣ໥ת

Hash.Cookie = new Class({


    //̳Cookie

    Extends: Cookie,



    options: {
        //ǷԶ,Ϊtrueʱÿ޸ĹϣݶԶCookie

        autoSave: true

    },


    //캯

    initialize: function(name, options){
        //øĹ캯,ͬĲ

        this.parent(name, options);
        //ȡCookieΪϣ

        this.load();

    },


    //Cookie

    save: function(){
        //ȽϣתΪַ

        var value = JSON.encode(this.hash);
        //ΪջݴС4K(Cookie),ȡ

        if (!value || value.length > 4096) return false; //cookie would be truncated!
        //ϣû,ɾCookie

        if (value == '{}') this.dispose();
        //򱣴Cookie

        else this.write(value);

        return true;

    },


    //Cookie

    load: function(){
        //ʹøCookiereadȡָCookie,ʹJSON.decodeΪϣ

        this.hash = new Hash(JSON.decode(this.read(), true));

        return this;

    }



});


//ΪHash.Cookieչʵ(ҪautoSaveʵ)
//ԭǽӹHashԭͷ,ϰװ,ָautoSaveΪtrue
//ÿεõʱԶsave
Hash.Cookie.implement((function(){

    

    var methods = {};

    //Hashԭ
еķ
    Hash.each(Hash.prototype, function(method, name){

        methods[name] = function(){
            //Ϊԭû,ܱ֤methodǷ,apply
            //ʱеthisָHash.Cookieʵhash

            var value = method.apply(this.hash, arguments);
            //ָԶ,ÿHashйصķʱԶ

            if (this.options.autoSave) this.save();

            //չʵֵĶ
           return methods;

        };

    });

    

    return methods;

    

})()); 


