var cookie = {
    get: function(name){
        var pairs = document.cookie ? document.cookie.split("; ") : []
        for(var i = 0; i < pairs.length; i++){
            var idx = pairs[i].indexOf("=")
            var key = idx > -1 ? pairs[i].slice(0, idx) : pairs[i]
            if(key === name){
                var val = idx > -1 ? pairs[i].slice(idx + 1) : ""
                try{
                    return decodeURIComponent(val)
                }catch(e){
                    return val
                }
            }
        }
        return null
    },
    set: function(name, value, maxAge){
        var v = typeof value === "string" ? value : String(value)
        document.cookie = name + "=" + encodeURIComponent(v) + ";path=/" + (maxAge ? ";max-age=" + maxAge : "")
    },
    getJSON: function(name){
        var v = this.get(name)
        if(!v){
            return {}
        }
        try{
            return JSON.parse(v)
        }catch(e){
            return {}
        }
    },
    updateJSON: function(name, updater, maxAge){
        var obj = this.getJSON(name)
        var next = updater && updater(obj) || obj
        try{
            this.set(name, JSON.stringify(next), maxAge)
        }catch(e){}
        return next
    }
}
