/**
* @date    : 2011-4-12$
* @author  : Perlt <zhenpengday.blog.163.com>
* @Version : 1.0
*
* jQuery Cookie Plugin
*
* Usage:
* Simple: 
* <script type="text/javascript">
* $.Cookie.set(name,value,expires,path,domain,secure);
* $.Cookie.get(name);
* $.Cookie.unset(name);
* </script>
*
*/
(function($){
 $.extend({
  'Cookie' : {
   /**
   * 获取指定名称的cookie
   * @param name string
   * @return object {name,value,expires,path,domain,secure,cookieString}
   */
   get : function(name) {
    
    // 初始化变量
    var cookieName = encodeURIComponent(name) + "=",// 每个cookie的名称都会经过编码
    cookie = document.cookie,      // cookie的别名，避免多次使用document.cookie降低性能
    cookieStart = cookie.lastIndexOf(cookieName), // 获取相应Cookie的头位置,如果存在多个同名cookie，以最后一个为准
    cookieEnd = -1,         // 用来记录当前cookie的结束位置
    nextKeyEnd = -1,        // 下一个键的开始
    nextColon = -1,         // 暂存下一个逗号的位置
    nextKey   = -1,         // 暂存下一个关键字的起始位置
    nextValue = -1,         // 暂存下一个关键字值的起始位置
    cookieString = '',        // 整个cookie字符串形式
    value = '',          // cookie的值
    expires = '',         // 过期时间
    path = '',          //  路径
    domain = '',         // 域
    secure = '';         // 安全标志
     
    // 如果该cookie存在
    if (cookieStart >-1) {
     
     // 获取第一个分号,也就是值
     cookieEnd = cookie.indexOf(';', cookieStart);
     // 如果该cookie在尾部
     if (cookieEnd == -1) {
      cookieEnd = cookie.length;
     }
     
     // 获取整个cookie的字符串形式
     cookieString = decodeURIComponent( cookie.substring(cookieStart, cookieEnd) );
     
     // 第value开始的位置
     nextKey = cookieStart  + cookieName.length;
     
     // 获取第一个逗号的位置
     nextKeyEnd = cookie.indexOf(',', nextKey);
     
     // 如果存在逗号，也就是有其他字段的值
     if (nextKeyEnd == -1) {
      // 不存在逗号
      value = decodeURIComponent(cookie.substring(nextKey, cookieEnd ));
     } else {
      // 存在逗号
      value = decodeURIComponent(cookie.substring(nextKey, nextKeyEnd)); 
     }
     
     // 第一个逗号，位置一下一位
     nextKeyEnd++;
     
     // 如果有expires，则获取之,判断位置加上是否小于cookieEnd
     if ( nextKeyEnd != -1 && nextKeyEnd <= cookieEnd) {
      expires = searchByKey('expires');
     }
     
     
     // 如果有path，则获取之.判断位置加上是否小于cookieEnd
     if (nextKeyEnd != -1 && nextKeyEnd <= cookieEnd) {
      path = searchByKey('path');
     }
     
     // 如果有domain，则获取之,判断位置加上是否小于cookieEnd
     if (nextKeyEnd != -1 ) {
      domain = searchByKey('domain');
      
     }
     
     // 如果有secure，则获取之,判断位置加上是否小于cookieEnd
     if (nextKeyEnd != -1 ) {
      // 获取下一个分号和关键字的位置
      nextKey = cookie.indexOf('secure', nextKeyEnd);
      // 如果符合条件
      if (nextKey != -1 && cookieEnd > nextKey) {
       // 获取secure 
       secure = 'secure';
      }
     }
     
     // 返回cookie结果对象
     return {
      'name'  : name,
      'value'  : value,
      'expires' : expires,
      'path'  : path,
      'domain' : domain,
      'secure' : secure,
      'cookieString' : cookieString
     };
    }
    
    // 默认返回null
    return null;
    
    
    /**
    *
    * 搜索关键字段值的函数定义
    *
    * @param key   string 字段名
    * @global nextKeyEnd int
    * @global nextColon int
    * @global nextKey  int
    * @global cookie  document.cookie
    * @global cookieEnd int
    *
    * @return  value  string
    */
    function searchByKey(key) {
     
     // 暂时存放值的临时变量
     var value = '';
     // 获取下一个分号和关键字的位置
     nextColon = cookie.indexOf(',', nextKeyEnd);
     nextKey = cookie.indexOf(key + '=', nextKeyEnd);
      
     // 判断是否存在此关键字段
     if (nextKey != -1) {
      // 判断，逗号可能不存在，则测试是否是分号结尾
      if (nextColon != -1 ) {
       if ( nextColon > nextKey) {
        // 获取关键字段对应的值 
        value = cookie.substring(nextKey + (key + '=').length, nextColon);
        // 获取当前逗号的下一个位置
        nextKeyEnd =  nextColon + 1;
       }
      } else {
       // 逗号可能不存在，则测试是否是分号结尾
       if ( cookieEnd > nextColon) {
        // 获取关键字段对应的值 
        value = cookie.substring(nextKey + 'domain='.length, cookieEnd);
       }
      }
     }
     
     return value;
    }
    
   },
   
   
   /**
   * 设置指定名称的cookie
   * @param name string
   * @param value string
   * @param expires date/string
   * @param path string
   * @param domain string
   * @param secure boolean
   */
   set : function(name, value, expires, path, domain, secure) {
    
    // 编码cookie名称和值
    var cookieText = encodeURIComponent(name) + "=" + encodeURIComponent(value) ;
    
    // 设置expires
    if (expires) {
     // 如果expires 是 Date 类型
     if (expires instanceof Date) {
      cookieText += ",expires=" + expires.toString();
     } else {
      cookieText += ",expires=" + expires;
     }
    }
    
    // 设置path
    if (path) {
     cookieText += ",path=" + path ;
    }
    
    // 设置domain
    if (domain) {
     cookieText += ",domain=" + domain ;
    }
    
    // 设置secure
    if (secure == true) {
     cookieText += ",secure";
    }
    
    // 补上分号
    cookieText += ';';
    
    // 将cookieText加入到cookie中
    document.cookie = cookieText;
   },
   
   
   /**
   * 删除指定的cookie
   * @param name string
   */
   unset : function(name) {
    this.set(name, "", new Date(0), '', '', '');
   }
  }
 });
})(jQuery)
