var Cookie = function(){return {

    data: {},
    options: {expires: 90, path: "/", secure: false, name: 'DP'  },

    init: function(options, data) {

            this.options = Object.extend(this.options, options || {});

            var payload = this.retrieve();

            if(payload) {
                    try {this.data = payload.evalJSON();} catch(e) {}
            }
            else {
                    this.data = data || {};
            }
            this.store();
    },

    getData: function(key) {
        return this.data[key];
    },

    setData: function(key, value) {
        this.data[key] = value;
        this.store();
    },

    removeData: function(key) {
        delete this.data[key];
        this.store();
    },

    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 && (typeof this.options.expires == 'number' || this.options.expires.toUTCString)) {
            var date;
            if (typeof this.options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (this.options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = this.options.expires;
            }
            expires = '; expires=' + date.toUTCString();
        }		
		
        cookieName = (typeof this.options.name === 'undefined' ? '' : this.options.name + '');
        document.cookie = this.options.name + '=' + escape(Object.toJSON(this.data)) + this.getOptions() + expires;
    },

    erase: function() {
        document.cookie = this.options.name + '=' + this.getOptions() + ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
      
    },
    
    getOptions: function() {
        return (this.options.path ? ';path=' + this.options.path : '') + (this.options.secure ? ';secure' : '');
    }
};};
