var ly_xinZblog_page = 1;
$(document).on("click", ".comments .msg a", function (e) {
    if ($(this).attr('href') == '#comment') {
        $('.cancel-reply').css({
            'display': 'inline-block'
        });
        $('#AjaxComment' + $('#inpRevID').val()).after($('#divCommentPost'));
    }
});
$(document).on("click", ".cancel-reply", function (e) {
    $('#inpRevID').val(0);
    $('.cancel-reply').css({
        'display': 'none'
    });
    $('#AjaxCommentEnd').after($('#divCommentPost'));
    return false;
});
$(document).on("click", ".comments .pagebar a", function (e) {
    ly_xinZblog_page = 0;
    if ($('.cancel-reply').css('display') != 'none') {
        $('#inpRevID').val(0);
        $('.cancel-reply').css({
            'display': 'none'
        });
        $('#AjaxCommentEnd').after($('#divCommentPost'));
    }
    $('body,html').animate({
        scrollTop: $('#AjaxCommentBegin').offset().top - 118
    }, 100);
});
$(document).on("click", "a", function (e) {
    let href = $(this).attr('href');
    if (href && href.indexOf('#') === 0) {
        setTimeout("cmtidscrollTop();", 500);
    }
});
$(document).on("click", "#foot_top .i-m-speak", function (e) {
    $('.wx_pngs').toggle();
});

function ly_xinZblog_bbs(page) {
    ly_xinZblog_page = 0;
    $.ajax({
        url: bloghost + 'zb_users/theme/ly_xinZblog/bbs.php',
        data: 'page=' + page,
        dataType: "html",
        success: function (data) {
            if (data) {
                let Obj = $('<code></code>').append(data);
                $('div.detail').html($(".detail", Obj).html());
                $('.pagebar').html($(".pages", Obj));
                $('body,html').animate({
                    scrollTop: 0
                }, 100);
            }
        }
    });
}

function cmtidscrollTop() {
    if (/(#\w+)$/.test(window.location.href)) {
        let id = $(RegExp.$1);
        if (id.length) {
            let top = $(this).scrollTop();
            let topx = id.offset().top;
            if (top > 100 && Math.abs(topx - top) < 10) {
                $('body,html').animate({
                    scrollTop: topx - 100
                }, 500);
            }
        }
    }
}

function ly_xinZblog_pages() {
    if (ly_xinZblog_page < 0) {
        ly_xinZblog_page = Math.abs(ly_xinZblog_page);
    }
}

function scroll_load() {
    if ($('.pagebar .nextbutton').length == 0) return;
    let max = 4;
    $(window).scroll(function () {
        if (ly_xinZblog_page > max || ly_xinZblog_page < -max) {
            return;
        } else if (ly_xinZblog_page == 0) {
            ly_xinZblog_page = -1;
            setTimeout(ly_xinZblog_pages, 500);
            return;
        }
        if (ly_xinZblog_page > 0) {
            let bar = $('.pagebar').prev();
            let eTop = bar.offset().top + bar.height() - 500;
            let wTop = $(window).height();
            let dTop = $(document).scrollTop();
            if (dTop + wTop > eTop) {
                ly_xinZblog_page++;
                ly_xinZblog_page = -ly_xinZblog_page;
                let btn = $('.pagebar .pages');
                let url = btn.find('.nextbutton').attr('href');
                if (url === undefined) {
                    ly_xinZblog_page = max + 1;
                    return;
                }
                let now = parseInt(btn.attr("data-now"));
                let all = parseInt(btn.attr("data-all"));
                if (now >= all) {
                    return;
                }
                let $html;
                let m = [];
                let cmt = 0;
                let bbs = 0;
                if (m = /zbp\.comment\.get\('(\d+)','(\d+)'\)/g.exec(url)) {
                    cmt = m[1];
                    url = bloghost + 'zb_system/cmd.php?act=getcmt&postid=' + m[1] + '&page=' + m[2];
                } else if (m = /ly\_xinZblog\_bbs\((\d+)\)/g.exec(url)) {
                    bbs = m[1];
                    url = bloghost + 'zb_users/theme/ly_xinZblog/bbs.php?page=' + m[1];
                }
                console.log(url);
                $.ajax({
                    type: 'get',
                    url: url,
                    dataType: "html",
                    success: function (data) {
                        if (data) {
                            let Obj = $('<code></code>').append(data);
                            if (cmt) {
                                $('.Commentlist').append($(".Commentlist", Obj).html());
                                $('.pagebar').html($(".pagebar", Obj).find('.pages'));
                            } else if (bbs) {
                                $('div.detail').append($(".detail", Obj).html());
                                $('.pagebar').html($(".pages", Obj));
                            } else {
                                $html = $(".word,.word1", Obj);
                                bar.append($html.find('.pagebar').prev().html());
                                $('.pagebar').html($html.find('.pagebar .pages'));
                            }
                            if (typeof LazyLoad === 'function') {
                                $("img").lazyload();
                            }
                        }
                        setTimeout(ly_xinZblog_pages, 500);
                    },
                    timeout: 5000,
                    error: function (XHR, textStatus, errorThrown) {
                        setTimeout(ly_xinZblog_pages, 500);
                    }
                });
            }
        }
    });
}

function ly_xinZblog_load() {
    let menutop = 0,
        menutops = -1;
    $(document).on("click", ".banner .pic span", function (e) {
        let c = $(this).css('background-color');
        let cc = zbp.cookie.get("body_color");
        let x = Number(zbp.cookie.get("body_colorx"));
        if (x <= 0 && cc && c.indexOf(cc.replace(/(\.\d+)?\)/g, '')) > -1) {
            $('body').css('background-color', window.body_color);
            zbp.cookie.set("body_color", '', -1);
            zbp.cookie.set("body_colorx", '', -1);
        } else {
            if (!x || x <= 0 || !cc || c.indexOf(cc.replace(/0\.\d+\)/g, '')) === -1) {
                x = 0.9;
            } else {
                x = Math.round((x - 0.1) * 100) / 100;
            }
            c = c.replace(/0\.\d+/g, x);
            $('body').css('background-color', c);
            zbp.cookie.set("body_color", c, 30);
            zbp.cookie.set("body_colorx", x, 30);
        }
    });
    $(window).scroll(function () {
        menutop = $(this).scrollTop();
        if (menutop > 300) {
            $(".foot_top").fadeIn();
        } else {
            $(".foot_top").fadeOut();
        }
        if (menutops > -1 && menutops != menutop) {
            hide_menu(cssmenu, 1);
        }
    });
    $(document).on("click", ".foot_top", function () {
        $('body,html').animate({
            scrollTop: 0
        }, 500);
        return false;
    });
    $('#menu-button .menu-button').after($('#cssmenu ul li h1,#cssmenu ul li b').html());

    function hide_menu(cssmenu, $is) {
        let mainmenu = cssmenu.find('.menu');
        if (mainmenu.css('display') == 'block') {
            menutops = -1;
            mainmenu.hide();
            mainmenu.find('ul').hide();
            if ($('#menu-button .search').css('display') == 'block') {
                if ($('#cssmenu li.search').length) {
                    $('#menu-button .search').hide();
                    $("#menu-button .searchs").show();
                    $("#menu-button a").show();
                }
            }
        } else if (!$is) {
            menutops = menutop;
            mainmenu.show();
            if ($('#menu-button .search').css('display') == 'none') {
                if ($('#cssmenu li.search').length) {
                    $('#menu-button .search').show();
                    $("#menu-button .searchs").hide();
                    $("#menu-button a").hide();
                }
            }
        }
    }
    let cssmenu = $("#cssmenu");
    $(document).on("click", ".menu-button", function () {
        if (!$('#menu-button .search').html()) {
            $('#menu-button .search').html($('#cssmenu li.search').html());
        }
        hide_menu(cssmenu, 0);
    });
    $(document).on("click", ".submenu-button", function () {
        if ($("#menu-button").css('display') != 'none') {
            if ($(this).siblings('ul').css('display') == 'block') {
                $(this).siblings('ul').hide()
            } else {
                $(this).siblings('ul').show()
            }
        }
    });
    $(document).on('click', function (e) {
        if ($("#menu-button").css('display') != 'none') {
            if (!$(e.target).closest('#cssmenu').length) {
                hide_menu(cssmenu, 1);
            }
        }
    });
    $(document).on("click", ".menu .i-search", function () {
        if (!$('.search-input').val()) {
            return false;
        }
    });
    $(document).on("click", "#menu-button .i-search", function () {
        if ($('#menu-button .search').css('display') == 'block') {
            if (!$('.search-input').val()) {
                $('.searchs').show();
                $("#menu-button a").show();
                $('#menu-button .search').hide();
                return false;
            }
        } else {
            $(this).hide();
            if (!$('#menu-button .search').html()) {
                $('#menu-button .search').html($('#cssmenu li.search').html());
            }
            $("#menu-button a").hide();
            $('#menu-button .search').show();
        }
    });
}
ly_xinZblog_load();

function ly_xinZblog_loads() {
    setTimeout("cmtidscrollTop();scroll_load();", 500);
}
$(function () {
    ly_xinZblog_loads();
});