var Cookie = {
    data: {},
    options: {expires: 1, domain: "", path: "", secure: false},
//PUBLIC
    initialize: function(options, data) {
        this.options = Object.extend(this.options, options || {});
        var payload = this.retrieve();
        if(payload) {
            this.data = payload.evalJSON();
        }
        else {
            this.data = data || {};
        }
        this.store();

    },
    getData: function(options, key) {
	if(options) { this.initialize(options); }
        return this.data[key];
    },
    setData: function(options, key, value) {
	this.initialize(options);
        this.data[key] = value;
        this.store();
    },
    removeData: function(options, key) {
	this.initialize(options);
        delete this.data[key];
        this.store();
    },
    erase: function(options) {
	this.initialize(options);
        var t = this.options.name + '=' + this.getOptions() + ';expires=Fri, 27 Jul 2001 02:47:11 UTC;';	
	document.cookie = t;
    },

// PRIVATE 
    retrieve: function() {
        var start = document.cookie.indexOf(this.options.name + "=");
        if(start == -1) {
            return null;
        }
        if(this.options.name != document.cookie.substr(start, this.options.name.length)) {
            return null;
        }

        var len = start + this.options.name.length + 1;
        var end = document.cookie.indexOf(';', len);

        if(end == -1) {
            end = document.cookie.length;
        }
        return unescape(document.cookie.substring(len, end));
    },
    store: function() {
        var expires = '';
        if (this.options.expires) {
            var today = new Date();
            expires = this.options.expires * 86400000;
            expires = ';expires=' + new Date(today.getTime() + expires);
        }

        document.cookie = this.options.name + '=' + escape(Object.toJSON(this.data)) + this.getOptions() + expires;
    },
    getOptions: function() {
        return (this.options.path ? ';path=' + this.options.path : '') + (this.options.domain ? ';domain=' + this.options.domain : '') + (this.options.secure ? ';secure' : '');
    }
};

var SC_COOKIE_NAME = "";
var GLOBAL_COOKIE_NAME = "g";


var migrate = function() {
    var s = document.cookie;
    var arr = s.split(';');
    var cids = [];
    for(i=0;i<arr.length;i++) {
	var nv = arr[i].split('=');
	if(nv[0].match(/searchcloud\_\d/)) {
	    Cookie.erase({name:nv[0],domain:"ezra.com"});
	    Cookie.erase({name:nv[0],domain:"searchcloud.com"});
	}
    }

}
migrate();
