/*===========================================================================
  COOKIE object
  (c) 2006, Inventive Labs

  To init the cookie object:
    var pref = new Cookie('pref', Cookie.days(30));

  Set the cookie to some value:
    pref.set(value);

  Find out the cookie value:
    pref.get();
---------------------------------------------------------------------------*/
function Cookie(key, expiry, domain, path, bSecure) {
  var _me = this;
  this.key = key;
  this.expiry = expiry;
  this.domain = domain;
  this.path = path ? path : "/";
  this.bSecure = bSecure;

  /* sets the value of the cookie identified by key */
  this.set = function (val) {
    var dough = escape(_me.key) + "=" + escape(val);
    if (_me.path) { dough += ";path="+_me.path; }
    if (_me.expiry) { dough += ";expires="+ _me.expiry.toGMTString(); }
    if (_me.domain) { dough += ";domain="+_me.domain; }
    if (_me.bSecure) { dough += ";secure"; }

    document.cookie = dough;
    return val;
  }

  /* returns the value of the cookie identified by key */
  this.get = function () {
    var pairs = document.cookie.split(';');
    for (var i=0;i<pairs.length;i++) {
      var p = pairs[i].replace(/^\s+/g, '');
      if (p && p.indexOf(_me.key) == 0) {
        return p.substring(key.length+1);
      }
    }
  }

  /* deletes the cookie identified by key */
  this.nullify = function () {
    _me.expiry = Cookie.days(-1);
    _me.set("");
  }
}

/* returns the date in so many days from now */ 
Cookie.days = function (days) {
  if (days) {
    var d = new Date();
    d.setTime(d.getTime() + (parseFloat(days) * 86400000));
    return d;
  }
}

