/*
CookieĶдʵҪַĲ
*/
var Cookie = new Class({

 //setOptionsӿʵ
 Implements: Options,

 options: {
  //Cookieȡʱ·
  path: false,
  //Cookieȡʱ
  domain: false,
  //ָCookieʱ,λĬΪ,ָʱ̰
  duration: false,
  //ǷҪȫ
  secure: false,
  //ָCookiedocument
  document: document
 },

 //캯,keyΪCookieеʶ,ͬʱǾ
 initialize: function(key, options){
  this.key = key;
  this.setOptions(options);
 },

 //дCookie
 write: function(value){
  //URI,
  value = encodeURIComponent(value);
  //ָCookieĹ
  if (this.options.domain) value += '; domain=' + this.options.domain;
  //ָCookieĹ·
  if (this.options.path) value += '; path=' + this.options.path;
  //ָCookieıʱ
  if (this.options.duration){
   var date = new Date();
   //תΪ쵥λ
   date.setTime(date.getTime() + this.options.duration * 24 * 60 * 60 * 1000);
   value += '; expires=' + date.toGMTString();
  }
  //Ҫȫ
  if (this.options.secure) value += '; secure';
  //дCookie
  this.options.document.cookie = this.key + '=' + value;
  return this;
 },

 //Cookie
 read: function(){
  //ǰCookieһԷֺ";"ָĵʽɵַ,Ҫַ
  var value = this.options.document.cookie.match('(?:^|;)\\s*' + this.key.escapeRegExp() + '=([^;]*)');
  //ӦдCookieʱencodeURIComponent
  return (value) ? decodeURIComponent(value[1]) : null;
 },

 //ɾCookie
 dispose: function(){
  //ͨдյַ,ùʱΪֵɾָCookie
  new Cookie(this.key, $merge(this.options, {duration: -1})).write('');
  return this;
 }

});

//̬,дCookie
Cookie.write = function(key, value, options){
 return new Cookie(key, options).write(value);
};

//̬,Cookie
Cookie.read = function(key){
 return new Cookie(key).read();
};

//̬,ɾCookie
Cookie.dispose = function(key, options){
 return new Cookie(key, options).dispose();
};

