var Cookie = {
  lookup_hash: null,
  
  get: function(key) {
    value = Cookie.get_raw(key)
    try {
      return value.evalJSON();
    } catch (e) {
      return null;
    }
  },

  get_raw: function(key) {
    return unescape(Cookie.lookup().get(key));
  },

  set: function(key, value, expiry, path) {
    Cookie.set_raw(key, Object.toJSON(value), expiry, path);
  },
  
  set_raw: function(key, value, expiry, path) {
    if (expiry == null) {
      expiry = new Date;
      expiry.setYear(expiry.getFullYear() + 20);
    }
    if (path == null) path = '/';
    
    Cookie.lookup().set(key, value);
    //Cookie.lookup_hash = null;
    document.cookie = key + '=' + escape(value) + '; expires=' + expiry.toGMTString() + '; path=' + path;
  },

  clear: function(key) {
    Cookie.set_raw(key, '', new Date);
  },
  
  keys: function(key) {
    return Cookie.lookup().keys();
  },
  
  lookup: function() {
    if (Cookie.lookup_hash == null) {
      Cookie.lookup_hash = new Hash();
      document.cookie.split(';').each(function(cookie) {
        kv = cookie.strip().split('=');
        if (kv.length == 2) {
          Cookie.lookup_hash.set(kv[0], kv[1].replace(/\+/g,' '));
        }
      });
    }
    return Cookie.lookup_hash;
  }
};

var Remember = {
  profile_id: null,
  prefix: '',
  
  key: function() {
    if (Remember.profile_id == null) {
      return 'r_hsh';
    } else {
      return 'r_hsh_' + Remember.profile_id;
    }
  },
  
  value: function(key, value) {
    cookie = Remember.cookie();
    if (value == null) {
      return cookie[Remember.prefix + key];
    } else {
      cookie[Remember.prefix + key] = value;
      Cookie.set(Remember.key(), cookie);
    }
  },
  
  cookie: function() {
    return Cookie.get(Remember.key()) || {};
  }
};
