/*!
 * jQuery Cookie Plugin v1.4.1
 * https://github.com/carhartl/jquery-cookie
 *
 * Copyright 2006, 2014 Klaus Hartl
 * Released under the MIT license
 */
(function (factory) {
	if (typeof define === 'function' && define.amd) {
		// AMD (Register as an anonymous module) 如RequireJS
		define(['jquery'], factory);
	} else if (typeof exports === 'object') {
		// Node/CommonJS 如SeaJS,node.js的模块
		module.exports = factory(require('jquery'));
	} else {
		// Browser globals
		factory(jQuery);
	}
}(function ($) {

	var pluses = /\+/g;

	function encode(s) {
		return config.raw ? s : encodeURIComponent(s); //根据配置是否对url字符进行编码
	}

	function decode(s) {
		return config.raw ? s : decodeURIComponent(s); //根据配置是否对url字符进行解码
	}

	function stringifyCookieValue(value) {
		return encode(config.json ? JSON.stringify(value) : String(value));  //将Json数据字符串化
	}

	function parseCookieValue(s) {
		if (s.indexOf('"') === 0) {
			// This is a quoted cookie as according to RFC2068, unescape...
      //slice(1,-1)去掉前后双引号
      //replace(/\\"/g,'"')将转义的双引号全部替换为常用双引号
      //replace(/\\\\/g,'\\')将转义的反斜杠全部替换为常用反斜线
			s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
		}

		try {
			// Replace server-side written pluses with spaces.
			// If we can't decode the cookie, ignore it, it's unusable.
			// If we can't parse the cookie, ignore it, it's unusable.
			s = decodeURIComponent(s.replace(pluses, ' '));
			return config.json ? JSON.parse(s) : s;
		} catch(e) {}
	}

	function read(s, converter) {
		var value = config.raw ? s : parseCookieValue(s);
		return $.isFunction(converter) ? converter(value) : value;
	}

	var config = $.cookie = function (key, value, options) {

		// Write

		if (arguments.length > 1 && !$.isFunction(value)) {
			options = $.extend({}, config.defaults, options);
      //如果设置过期时间(为天数),则转换覆写options.expires时间为当前时间+设置的时间
      //注意后面的t是采用传址格式的对象变量,后面的赋值会对前面的变量有影响
			if (typeof options.expires === 'number') {
				var days = options.expires, t = options.expires = new Date();
				t.setMilliseconds(t.getMilliseconds() + days * 864e+5);
			}

			return (document.cookie = [
				encode(key), '=', stringifyCookieValue(value), //转换值类型为字符型
				options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
				options.path    ? '; path=' + options.path : '', //如果有路径则设置路径,默认是当前文件的文件夹下
				options.domain  ? '; domain=' + options.domain : '', //如果有域名限制,则设置域名.默认是当前域名
				options.secure  ? '; secure' : ''  //是否使用https传输,只有开启https链接的情况下有用.
			].join(''));  //数组join函数,将其链接为字符串
		}

		// Read

		var result = key ? undefined : {},
			// To prevent the for loop in the first place assign an empty array
			// in case there are no cookies at all. Also prevents odd result when
			// calling $.cookie().
			cookies = document.cookie ? document.cookie.split('; ') : [],  //注意;后面还有一个空格,兼容性值得学习
			i = 0,
			l = cookies.length;   //缓存数组长度,能提高访问性能

		for (; i < l; i++) {
			var parts = cookies[i].split('='),  //对上面分解的cookie名值对进行进一步的分解
				name = decode(parts.shift()),  //提取分解后的第一个数组元素,即cookie名并解码
				cookie = parts.join('=');  //提取cookie值,这里有一个技巧,数组只有一个元素时,后面的=号是不链接进去的.
                                   //同时join函数,可将数据类型转换为字符类型

			if (key === name) {
				// If second argument (value) is a function it's a converter...
				result = read(cookie, value);
				break;
			}

			// Prevent storing a cookie that we couldn't decode.
			if (!key && (cookie = read(cookie)) !== undefined) {
				result[name] = cookie;
			}
		}

		return result;
	};

	config.defaults = {};  //可扩展配置,见前面的函数
  //设置cookie过期时,直接调用设置函数
	$.removeCookie = function (key, options) {
		// Must not alter options, thus extending a fresh object...
		$.cookie(key, '', $.extend({}, options, { expires: -1 }));
		return !$.cookie(key);
	};

}));
