/*
**      ++ Methods to use with Cookiejar:
**      
**      var kekse = new Cookiejar(cookieJarName, CookiePath)
**      This is the constructor.cookieJarName is the name of the collection of
**      cookies you would like to associate with this object. This is the name
**  of the cookie that holds your name:value pairs
**      
**      kekse.setCookie(name, value)
**      Sets name to value
**      
**      kekse.getCookie(name)
**      Returns the value of name
**
**      kekse.cookieExists(name)
**      Returns false if name does not exist in the jar.
**  Returns the position of the element in the jar if it does exist
**      
**      kekse.delCookie(name)
**      Removes name from the jar
**      
**      kekse.setExpiration(years, days, hours, minutes, seconds, milliseconds)
**      Set the jar to expire in years + days + hours + minutes + seconds +
milliseconds
**
**      kekse.reset()
**      Removes all cookies from the jar
**
**
**
*/

var Cookiejar = Class.create();
Cookiejar.prototype = {
        jar: {},
        jarname: "jar",
        expiration: 1 * 365 * 24 * 60 * 60 * 1000,
        dividerElement: "#",
        dividerIdValue: ":",
        path: "",
        domain: null,
        secure: false,

  initialize: function(jarname, path) {
        this.jarname = jarname;
        this.path = path;
        this._read();
  },

  cookieExists: function(c_id) {        
    if (c_id == "") return false;
       return !(typeof (this.jar[c_id]) == 'undefined');
  },

  delCookie: function(c_id) {
    delete this.jar[c_id];
        this._write();
  },
                        
  setCookie: function(c_id, c_value) {
        this.jar[c_id] = c_value;
        this._write();
  },

  getCookie: function(c_id) {   
    return this.jar[c_id] || null;
  },

  reset: function() {
        this.jar = {};
        Cookie.remove(this.jarname, this.path, this.domain);
  },

  setExpiration: function(years, days, hours, mins, secs, mill) {
    this.expiration =
                     (((years) ? (years) : 1)   * 365 * 24 * 60 * 60 * 1000) +
                                           (((days)  ? (days)  : 365) * 24 * 60 
* 60 * 1000) +
                                           (((hours)  ? (hours) : 24) * 60 * 60 
* 1000) +
                                             (((mins)  ? (mins)  : 60)  * 60 * 
1000 ) +
                                             (((secs)  ? (secs)  : 60)  * 1000) 
+
                                             ((mill)  ? (mill)   : 1000);
        this._write();                                  
  },

  _read: function() {
    var cookiestring = Cookie.get(this.jarname) || "";
    cookiestring.split(this.dividerElement).each(
      function(pair){
        pair = pair.split(this.dividerIdValue);
        this.jar[pair[0]] = pair[1];
      }.bind(this)
    );
  },

  _write: function() {
        var dateObj = new Date();
        dateObj.setTime(dateObj.getTime() + this.expiration);

        var base = new Date(0);
      var skew = base.getTime();
      if (skew > 0)
         dateObj.setTime(dateObj.getTime() - skew);

    var cookiestring = "";
    for (var i in this.jar){
      cookiestring += this.dividerElement + encodeURIComponent(i) +
this.dividerIdValue + encodeURIComponent(this.jar[i]);
    }
    Cookie.set(this.jarname, cookiestring, dateObj, this.path,
this.domain, this.secure);

  }
};

var Cookie = {
  get: function(name){
    if(typeof (document.cookie) == 'string'){
      var start = document.cookie.indexOf(name+"=");
      var len = start+name.length+1;
      if ((!start) && (name != document.cookie.substring(0,name.length))){
        return null;
      }
      if (start == -1) return null;
      var end = document.cookie.indexOf(";",len);
      if (end == -1) end = document.cookie.length;
      return decodeURIComponent(document.cookie.substring(len,end));
    } else {
      /* document.cookie is not a string so return an
      empty string. When tested this will type-convert to
      boolean false (accurately) giving the impression that
      client-side cookies are not available on this system:-
      */
      return "";
    }
  },

  set: function(name, value, expires, path, domain, secure) {
    if(typeof (document.cookie) == 'string'){
      document.cookie = name + "=" + encodeURIComponent(value) +
            ( (expires) ? ";expires=" + expires.toGMTString() : "") +
            ( (path) ? ";path=" + path : "") +
            ( (domain) ? ";domain=" + domain : "") +
            ( (secure) ? ";secure" : "");
    }//else document.cookie is not a string so do not write to it.
  },

  remove: function(name, path, domain) {
    if (this.get(name)) {
      document.cookie = name + "=" +
       ( (path) ? ";path=" + path : "") +
       ( (domain) ? ";domain=" + domain : "") +
       ";expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
  }
};