﻿/**
*上海乌龙网络技术发展有限公司
*2011-02-14
*LY
************************************
*set the value of a cookie
*@example $.cookie('the_cookie', 'the_value');
*Create a cookie with all available options
*$.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
*Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain used when the cookie was set.
*$.cookie('the_cookie', null);
*
**/

jQuery.cookie = function (name, value, options) {
    if (typeof value != "undefined") {
        options = options || {};
        if (value === null) {
            value = "";
            options.expires = -1;
        }
        var expires = "";
        if (options.expires && (typeof options.expires == "number" || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == "number") {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = "; expires=" + date.toUTCString();
        }
        var path = options.path ? "; path=" + (options.path) : "";
        var domain = options.domain ? "; domain=" + (options.domain) : "";
        var secure = options.secure ? "; secure" : "";
        document.cookie = [name, "=", escape(value), expires, path, domain, secure].join("");
    } else { 
        var _value = null;
        try {
            if (document.cookie && document.cookie != '') {
                var cookies = unescape(document.cookie).split(";");
                for (var i = 0; i < cookies.length; i++) {
                    var _cookie = $.trim(cookies[i]);
                    if (_cookie.substring(0, name.split("$")[0].length + 1) == (name.split("$")[0] + "=")) {
                        var _v = _cookie.substring(name.split("$")[0].length + 1).split("&");
                        for (var k = 0; k < _v.length; k++) {
                            var _cv = $.trim(_v[k]);
                            if (_cv.substring(0, name.split("$")[1].length + 1) == (name.split("$")[1] + "=")) {
                                _value = _cv.substring(name.split("$")[1].length + 1);
                                break;
                            }
                        }
                    }
                }
            }
        } catch (e) { _value = null; }
        return _value;
    }
};
