﻿var maxhistroycount = 18;

function getCookie(name) {
    var cookieValue = "";
    var search = name + "=";
    if (document.cookie.length > 0) {
        offset = document.cookie.indexOf(search);
        if (offset != -1) {
            offset += search.length;
            end = document.cookie.indexOf(";", offset);
            if (end == -1) end = document.cookie.length;
            cookieValue = unescape(document.cookie.substring(offset, end))
        }
    }
    return cookieValue;
}
//设置cookie
function setCookie(cookieName, cookieValue, DayValue) {
    var expire = "";
    var day_value = 1;
    if (DayValue != null) {
        day_value = DayValue;
    }
    expire = new Date((new Date()).getTime() + day_value * 86400000);
    expire = "; expires=" + expire.toGMTString();
    document.cookie = cookieName + "=" + escape(cookieValue) + ";path=/" + expire;
}
//删除cookie
function delCookie(cookieName) {
    var expire = "";
    expire = new Date((new Date()).getTime() - 1);
    expire = "; expires=" + expire.toGMTString();
    document.cookie = cookieName + "=" + escape("") + ";path=/" + expire;
    //path=/
}

function play(index) {
    $("#boxindex").removeClass("my now");
    for (var i = 1; i <= 6; i++) {
        $("#jlist-" + i).hide();
    }
    $("#jlist-" + index).show();
    if (index == 6) {
        $("#jlist-6").html(showhistory());
    }
}

function showhistory() {
    var title = getCookie("title").split("@");
    var url = getCookie("url").split("@");
    var html = "<ul id='jlold'>";
    html += "<li><button onclick='deletehistory()'>清空浏览记录</button></li>";
    for (var i = 0; i < title.length && i < maxhistroycount; i++) {
        html += ("<li><a href='" + url[i] + "' target='_blank'>" + title[i] + "</a></li>");
    }
    html += "</ul>";
    return html;
}

function showhistorypage() {
    var title = getCookie("title").split("@");
    var url = getCookie("url").split("@");
    var html = "<ul id='jold'>";
    html += "<li><button onclick='deletehistory()'>清空浏览记录</button></li>";
    html += "<table>";
    for (var i = 0; i < title.length && i < maxhistroycount; i += 4) {
        html += "<tr>";
        for (var j = i; j < i + 4 && j < maxhistroycount; j++) {
            html += ("<td width='25%'><a href='" + url[j] + "' target='_blank'>" + title[j] + "</a></li></td>");
        }
        if (j < i + 4) {
            html += ("<td colspan='" + (i + 4 - j) + "'></td>");
        }
        html += "</tr>";
    }
    html += "</table>";
    html += "</ul>";
    return html;
}

$(document).ready(function() {
    $("a").click(function() {
        if ($(this).html() != "") {
            var title = getCookie("title");
            var url = getCookie("url");
            title = ($(this).html().replace(/<\/[^>]+>/ig, "").replace(/<[^>]+>/ig, "") + "@" + title);
            url = ($(this).attr("href").replace(/<\/[^>]+>/ig, "").replace(/<[^>]+>/ig, "") + "@" + url);
            setCookie("title", title, 10);
            setCookie("url", url, 10);
        }
    });
});

function deletehistory() {
    delCookie("title");
    delCookie("url");
    $("#jlold").html("<li><button onclick='deletehistory()'>清空浏览记录</button></li>");
}