﻿jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') {
        options = options || {};
        if (value === null) {
            value = '';
            options = $.extend({}, options);
            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, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
var cookieKey = "yinxw_com_lastbrower";
function addToCookie(id, name, image) {
    var v = $.cookie(cookieKey);
    if (v == null || v == undefined) v = ""; 
    var exists = false;
    if (v != "") {
        var values = v.split("#");
        for (var i = 0; i < values.length; i++) {
            var s = values[i];
            var ss = s.split("$");
            if (id == ss[0]) {
                values[i] = "";
                exists = true;
                break;
            }
        }
        values.push();
        var len = values.length;
        //最近浏览最多只能8条记录
        if (len > 9 && exists) len = 9;
        else if (len > 8) len = 8;
        v = "";
        for (var i = 0; i < len; i++) {
            var s = values[i];
            if (s != "") {
                v = v + "#" + s ;
            }
        }
        v = id + "$" + name + "$" + image + "#" + v;
    } else {
        v = id + "$" + name + "$" + image;
    }
    $.cookie(cookieKey, v, { expires: 1, path:"/"});
}

function clearLastBrowerCookie() {
    $.cookie(cookieKey, "", {path: "/" }); 
}

function getFromCookie() {
    var cookie = $.cookie(cookieKey); 
    var result = [];
    if (cookie != undefined && cookie != "" && cookie!=null) {
        var values = cookie.split("#");
        for (var i = 0; i < values.length; i++) {
            var s = values[i];
            var ss = s.split("$");
            if (ss.length == 3) {
                var o = new Object();
                o.id = ss[0];
                o.name = ss[1];
                o.image = ss[2];
                result.push(o);
            }        
        }
    }
    return result;
}