﻿function viewHistory(obj) {
    var p = $(obj).position();
    var css = {
        left: p.left + "px",
        top: p.top + 20 + "px"
    };
    var tpl = "<a href='/p/{#Path}'><img src='" + photoDomain + "/{#Photo}' style='width:40px;height:40px;margin-right:3px;' align='left' /></a>";
    tpl += "<a href='/p/{#Path}' class='redText' style='text-decoration:underline'>{#Title}</a><br/>{#Price}";
    var html = "";

    var history = $.cookie("viewhistory");
    if (history != null) {
        if (history.indexOf("|") == 0) history = history.substr(1, history.length);
        var ds = history.split("|");

        for (var i = 0; i < ds.length; i++) {
            var d = ds[i].split(",");
            if (d.length < 4) continue;

            var tmp = tpl.replace(/\{#Photo\}/gi, d[3]);
            tmp = tmp.replace(/\{#Title\}/gi, d[1]);
            tmp = tmp.replace(/\{#Price\}/gi, d[2]);
            tmp = tmp.replace(/\{#Path\}/gi, d[0]);
            html += "<p>" + tmp + "</p>";
        }
        html += "<p style='text-align:right;cursor:pointer;'><span onclick='clearHistory()' id='clearHistoryBtn'>清空</span></p>";
    } else {
        html = "暂没有历史浏览记录";
    }
    $("#historyPanel").html(html).css(css);
    $("#historyPanel").fadeIn("fast");
}

function hideHistory() {
    $("#historyPanel").fadeOut("fast");
}

function clearHistory() {
    delCookie("viewhistory");
    $("#historyPanel").html("暂没有历史浏览记录");
}

function pushToHistory(data) {
    var gh = $.cookie("viewhistory");
    var b = false;
    if (gh == null) {
        gh = "|" + data;
    }
    else {
        if (gh.indexOf("|") == 0) gh = gh.substr(1, gh.length);
        var a = gh.split("|");

        if ($.inArray(data, a) == -1) {
            if (a.length == 10) {
                for (var i = 0; i < a.length; i++) {
                    if (i > 1) {
                        a[i - 1] = a[i];
                    }
                }
                a[9] = data;
                b = true;
            }
            else {
                gh += "|" + data;
            }
        }
        if (b) gh = "|" + a.join("|");
    }
    var exp = { expires: 1 };
    setCookie("viewhistory", gh, 60 * 60 * 24, "/");
}

$(function() {
    $("body").click(function(e) {
        e = e || event;
        if (e.target.id != 'clearHistoryBtn' && e.target.tagName.toLowerCase() != "a") {
            hideHistory();
        };
    });

    return;
    if (m_Index == 1) {
        $("#m" + m_Index).addClass("f_nav_selected");
    } else if (m_Index > 1) {
        $("#m" + m_Index).addClass("f_nav_selected2");
    }
});

function getCookie(key) {
    var arg = key + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg) return getCookieVal(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return null;
}

function setCookie(key, value) {
    var expdate = new Date();
    var argv = setCookie.arguments;
    var argc = setCookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    if (expires != null) expdate.setTime(expdate.getTime() + (expires * 1000));
    document.cookie = key + "=" + encodeURIComponent(value) + ((expires == null) ? "" : ("; expires=" + expdate.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : "");
}

function getCookieVal(offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1) endstr = document.cookie.length;
    return decodeURIComponent(document.cookie.substring(offset, endstr));
}

function delCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}


