var $body = (window.opera) ? (document.compatMode == "CSS1Compat" ? $('html') : $('body')) : $('html,body');
var storage = window.localStorage;

var lazyLoadInstance = new LazyLoad({});

// $(document).on('click', '.left_menu_box ul li a , .widget_nav_menu ul li a, .menu-top-container ul li a', function () {
//     var t = $(this);
//     t.siblings("ul").slideToggle(200);
//     t.parent().siblings().find("ul").hide(200);
//     if (t.parent().hasClass('has_children')) {
//         t.find('.drop_icon').toggleClass('up');
//     }
//     $('.left_menu_box ul li').removeClass('current-pjax-item current-menu-item current-menu-parent current-menu-ancestor');
//     t.parent().addClass('current-pjax-item');
// });

function highlightMenu() {
    $('.left_menu_box ul li').removeClass('current-pjax-item')
    // 路径映射表
    const pathMap = {
        '/moments': '/',
        '/tags': '/content',
        '/archives': '/archives',
        '/categories': '/content'
    }

    // 获取清洗后的 pathname（去掉 ? 和 #）
    function getPathname(url) {
        const cleanUrl = url.substring(0,
            url.indexOf('?') === -1
                ? (url.indexOf('#') === -1 ? url.length : url.indexOf('#'))
                : url.indexOf('?')
        )
        return new URL(cleanUrl).pathname
    }

    // 从长到短匹配路径映射（支持 /categories/shi 匹配 /categories）
    function getTargetPathname(pathname) {
        const segments = pathname.split('/').filter(Boolean)
        for (let i = segments.length; i >= 0; i--) {
            const currentPath = '/' + segments.slice(0, i).join('/')
            if (pathMap[currentPath]) return pathMap[currentPath]
        }
        return pathname // 无映射则返回原路径
    }

    const currentPathname = getPathname(location.href)
    const targetPathname = getTargetPathname(currentPathname)
    let hasMatched = false

    // 遍历一级菜单匹配
    $('.left_menu_box ul li a').each((i, e) => {
        const menuPathname = getPathname(e.href)
        if (targetPathname === menuPathname) {
            e.parentElement.classList.add('current-pjax-item')
            hasMatched = true
        }
    })

    // 兜底匹配 /navigator
    if (!hasMatched) {
        $('.left_menu_box ul li a').each((i, e) => {
            const menuPathname = getPathname(e.href)
            if (menuPathname === '/navigator') {
                e.parentElement.classList.add('current-pjax-item')
            }
        })
    }
}


//全局loading
function loading_template() {
    tag = '<div class="loader" uk-spinner></div>';
    tag += '</div>';

    return tag;
}

function loading_start(target) {
    target.append(loading_template());
}

function loading_done(target) {
    target.children('.loader').remove();
}


//话题表情添加
$(document).on('click', 'a.smile_btn', function () {
    var a = $(this).html();
    var textarea = $("textarea#topic_content");
    var content = textarea.val();
    textarea.val(content + a);
    textarea.focus();
});

$(document).on('click', '.smile_box i', function () {
    var t = $(".smile_show");
    t.slideToggle(100);
});
//选择话题
$(document).on('click', '.t_cat_box li', function () {
    var name = $(this).find('.c_name span').html();
    $('.t_cat_toogle span').html(name);

    UIkit.dropdown('.t_cat_box').hide(false);
});

$(document).on('click', '.up_cat_btn', function () {
    var name = $('input#add_cat').val();
    if (!name) {
        cocoMessage.error("请填写话题");
        return false;
    }
    $('.t_cat_toogle span').text(name)
    UIkit.dropdown('.t_cat_box').hide(false);
});

$(document).on('click', '.simi a', function () {
    var visible = $(this).attr('visible');
    if (visible == 'PUBLIC') {
        $(this).html('<i class="ri-lock-line"></i>').attr('visible', 'PRIVATE');
        $(this).children().css({ "background": "#ddd", "color": "#c6c6c6" });
    } else {
        $(this).html('<i class="ri-lock-unlock-line"></i>').attr('visible', 'PUBLIC');
        $(this).children().css({ "background": "#e3efe7", "color": "#66c187" });
    }
});

//消息通知
cocoMessage.config({
    duration: 2000,
});

//sortable事件 隐藏input
UIkit.util.on('.img_show', 'start', function (item) {
    $(".up_img_btn").hide();
});
UIkit.util.on('.img_show', 'moved', function (item) {
    var img_num = $('.add_img_box .t_media_item').length;
    if (img_num < 9) {
        $(".up_img_btn").show();
    }

});
UIkit.util.on('.img_show', 'stop', function (item) {
    var img_num = $('.add_img_box .t_media_item').length;
    if (img_num < 9) {
        $(".up_img_btn").show();
    }
});


//ajax上传媒体文件到媒体库（已兼容图片/视频）
$(document).on('change', '#topic_img_up', function (e) {
    e.preventDefault();
    if ($('#topic_img_up').val() == '') return;

    const policyName = Theme.moments.policy_name;
    const groupName = Theme.moments.group_name;

    var files = $('#topic_img_up')[0].files;
    var imgtot = 0;
    var num = $('.add_img_box .t_media_item').length;

    // 修改提示语为“文件”
    if (num + files.length > 9) {
        cocoMessage.error("最多上传9个媒体文件");
        return;
    }

    $.each(files, function (i, file) {
        imgtot += file['size'];
    });

    var tot = imgtot / 1024000;

    $(".up_img_btn i").hide();
    $(".up_img_btn").prepend("<div class='img_load_text'><div uk-spinner='ratio: .6'></div><span>"
        + tot.toFixed(2) + "MB</span></div>");

    $.each(files, function (i, file) {
        var data = new FormData();
        data.append('file', file);
        policyName && data.append('policyName', policyName);
        groupName && data.append('groupName', groupName);

        $.ajax({
            type: "POST",
            url: `${Theme.moments.attachments_upload_api}`,
            dataType: 'json',
            data: data,
            processData: false,
            contentType: false,
            beforeSend: function () {
                $('input#topic_img_up').attr('disabled', 'disabled');
            },
            success: function (data) {
                // 1. 获取上传后的地址
                var src = data.metadata.annotations["storage.halo.run/uri"]
                    || data.metadata.annotations["storage.halo.run/external-link"]
                    || data.metadata.annotations["lskypro.plugin.halo.chenhe.me/image-link"];

                // 2. 获取文件类型 (MIME Type)
                // Halo 的 API 通常在 data.spec.mediaType 中返回类型，或者根据后缀判断
                var mediaType = data.spec.mediaType || "";
                var isVideo = mediaType.startsWith('video/') || /\.(mp4|webm|mov)$/i.test(src);

                // 3. 动态生成预览 HTML
                var mediaInner = isVideo
                    ? `<video src="${src}" class="pix_video" preload="metadata" muted></video>`
                    : `<img src="${src}">`;

                var media = `<div class="t_media_item" data-src="${src}" data-type="${mediaType}" data-thum="${src}">
                                <a class="topic-img-de"><i class="ri-subtract-line"></i></a>
                                ${mediaInner}
                             </div>`;

                $(".img_show").prepend(media);

                num = $('.add_img_box .t_media_item').length;
                $(".img_load_text").remove();

                if (num < 10) $(".up_img_btn i").show();
                $('input#topic_img_up').removeAttr('disabled');
            },
            error: function () {
                cocoMessage.error("上传失败");
                $(".img_load_text").remove();
                $(".up_img_btn i").show();
                $('input#topic_img_up').removeAttr('disabled');
            }
        });
    });
});

//同步修改检查数量的提示语
function check_image_num() {
    var num = $('.add_img_box .t_media_item').length;
    if (num > 8) {
        cocoMessage.error("最多上传9个媒体文件");
        return true;
    }
}

//sortable事件 隐藏input
UIkit.util.on('.img_show', 'start', function (item) {
    $(".up_img_btn").hide();
});
UIkit.util.on('.img_show', 'moved', function (item) {
    var img_num = $('.add_img_box .t_media_item').length;
    if (img_num < 9) {
        $(".up_img_btn").show();
    }

});
UIkit.util.on('.img_show', 'stop', function (item) {
    var img_num = $('.add_img_box .t_media_item').length;
    if (img_num < 9) {
        $(".up_img_btn").show();
    }
});

//获取媒体库图片/视频（功能不变）
$(document).on('click', '.up_from_media a', function () {
    $(".attch_nav .pre").hide();
    $(".attch_nav .nex").hide();
    $(".attch_nav").attr('paged', '1');
    $(".wp_get_media_list").empty();
    $(".show_media_box").show();
    $.ajax({
        type: "get",
        url: `/apis/${Theme.moments.attachments_api}/v1alpha1/attachments?group=&page=1&size=10&ungrouped=false&accepts=image/*&accepts=video/*`,
        contentType: "application/json",
        beforeSend: function () { },
        success: function (data) {
            var list = data.items;
            $.each(list, function (i, value) {
                var thum = value.status.permalink;
                var src = value.status.permalink;
                var mediaType = value.spec.mediaType || '';
                var wf_media = '';

                if (mediaType.startsWith('video/')) {
                    wf_media = `<li data-src="${src}" data-type="${mediaType}">
                                    <video class="media-video-thumb" src="${src}" preload="metadata" muted></video>
                                </li>`;
                } else {
                    wf_media = `<li data-src="${src}" data-type="${mediaType}">
                                    <img src="${thum}">
                                </li>`;
                }
                $(".wp_get_media_list").append(wf_media);
            });
            var max = data.totalPages;
            $(".attch_nav .nex").toggle(max > 1);
        }
    });
});

//媒体库分页（功能不变）
$(document).on('click', '.attch_nav a', function () {
    $(".wp_get_media_list").empty();
    var type = $(this).attr('class');
    var paged = $(".attch_nav").attr('paged');

    if (type == 'nex') {
        paged = parseInt(paged) + 1;
    } else if (type == 'pre' && paged) {
        paged = parseInt(paged) - 1;
    }

    $(".attch_nav").attr('paged', paged);
    $.ajax({
        type: "get",
        url: `/apis/${Theme.moments.attachments_api}/v1alpha1/attachments?group=&page=${paged}&size=10&ungrouped=false&accepts=image/*&accepts=video/*`,
        contentType: "application/json",
        beforeSend: function () { },
        success: function (data) {
            $(".wp_get_media_list").empty();
            var list = data.items;
            $.each(list, function (i, value) {
                var thum = value.status.permalink;
                var src = value.status.permalink;
                var mediaType = value.spec.mediaType;
                var wf_media = '';

                if (mediaType.startsWith('video/')) {
                    wf_media = `<li data-src="${src}" data-type="${mediaType}">
                                    <video class="media-video-thumb" src="${src}" preload="metadata" muted></video>
                                </li>`;
                } else {
                    wf_media = `<li data-src="${src}" data-type="${mediaType}">
                                    <img src="${thum}">
                                </li>`;
                }
                $(".wp_get_media_list").append(wf_media);
            });

            var max = data.totalPages;
            $(".attch_nav .nex").toggle(max > paged);
            $(".attch_nav .pre").toggle(paged > 1);
        }
    });
});

//收起媒体库（无修改）
$(document).on('click', '.show_media_box .souqi', function () {
    $(".show_media_box").hide();
    $(".wp_get_media_list").empty();
});

//从媒体库插入图片/视频（删除按钮样式简化）
$(document).on('click', '.wp_get_media_list li', function () {
    var num = $('.add_img_box .t_media_item').length;
    if (num == 8) {
        $("a.up_img_btn").hide();
    }
    if (check_image_num()) {
        return false;
    }

    var src = $(this).attr('data-src');
    var type = $(this).attr('data-type');
    var mediaHtml = '';

    if (type.startsWith('video/')) {
        mediaHtml = `<div class="t_media_item" data-src="${src}" data-type="${type}">
                        <a class="topic-img-de"><i class="ri-subtract-line"></i></a>
                        <video src="${src}" class="pix_video" preload="metadata" muted></video>
                    </div>`;
    } else {
        var thum = $(this).children().attr('src') || $(this).children().attr('poster');
        mediaHtml = `<div class="t_media_item" data-src="${src}" data-thum="${thum}" data-type="${type}">
                        <a class="topic-img-de"><i class="ri-subtract-line"></i></a>
                        <img src="${thum}">
                    </div>`;
    }
    $(".img_show").prepend(mediaHtml);
});

//删除图片/视频（保留事件冒泡修复）
$(document).on('click', '.topic-img-de', function (e) {
    e.stopPropagation();
    $(this).parent().remove();
    var num = $('.add_img_box .t_media_item').length;
    if (num < 9) {
        $("a.up_img_btn").show();
    }
});

// 样式修复：3列布局 + 正方形显示（核心）
$(function () {
    var style = `<style>
        /* 媒体库视频封面：正方形显示，和图片一致 */
        .media-video-thumb {
            width: 100%;
            height: 100%;
            object-fit: cover; /* 裁剪为正方形，不拉伸 */
            border: none;
            pointer-events: none;
        }
        /* 媒体库li：适配3列，强制正方形 */
        .wp_get_media_list li {
            position: relative;
            display: inline-block;
            width: calc(33.333% - 10px); /* 3列基础宽度 */
            aspect-ratio: 1/1; /* 强制正方形（宽高比1:1） */
            margin: 5px;
            overflow: hidden; /* 裁剪超出部分 */
            vertical-align: top;
        }

        /* 核心：.t_media_item 强制正方形 + 3列布局 */
        .t_media_item {
            position: relative !important;
            display: inline-block !important;
            width: calc(33.333% - 10px) !important; /* 3列宽度，和原有布局一致 */
            aspect-ratio: 1/1 !important; /* 强制正方形，覆盖视频原比例 */
            margin: 5px !important;
            overflow: hidden !important; /* 裁剪超出的视频内容 */
            vertical-align: top !important;
            touch-action: none !important;
            user-select: none !important;
        }
        /* 删除按钮：固定在正方形右上角 */
        .t_media_item .topic-img-de {
            position: absolute !important;
            top: 5px !important;
            right: 5px !important;
            background: rgba(0,0,0,0.5) !important;
            color: #fff !important;
            width: 20px !important;
            height: 20px !important;
            border-radius: 50% !important;
            display: flex !important;
            align-items: center !important;
            justify-content: center !important;
            text-decoration: none !important;
            z-index: 99 !important;
            pointer-events: auto !important;
        }
        /* 视频/图片：裁剪为正方形，不拉伸 */
        .t_media_item video, .t_media_item img {
            width: 100% !important;
            height: 100% !important;
            object-fit: cover !important; /* 关键：裁剪多余部分，保持正方形 */
            display: block !important;
        }
    </style>`;
    $('head').append(style);
});

//插入外部图片链接
$(document).on('click', '.up_from_cdn a', function () {
    //$(".show_media_box .souqi").click();
    $(".show_cdn_media").show();

});

//取消
$(document).on('click', 'a.img_link_cancel', function () {
    $(".show_cdn_media").hide();
});

//插入图片或视频链接
$(document).on('click', 'a.img_link_btn', function () {
    var img_url = $("#img_link_up").val().trim();

    // 1. 扩展正则判断，支持视频后缀
    if (!/\.(mp4|webm|jpg|png|gif|webp|jpeg)$/i.test(img_url) || img_url == '') {
        cocoMessage.error("媒体格式不正确或链接为空！");
        return false;
    }

    if (check_image_num()) {
        return false;
    }

    var num = $('.add_img_box .t_media_item').length;
    if (num == 8) {
        $("a.up_img_btn").hide();
    }

    var src = img_url;
    // 获取后缀名并转为小写
    var ext = img_url.substring(img_url.lastIndexOf('.') + 1).toLowerCase();

    // 2. 判定是否为视频
    var isVideo = (ext === 'mp4' || ext === 'webm');
    var type = isVideo ? 'video/' + ext : 'image/' + ext;

    // 3. 根据类型生成预览 HTML
    var mediaPreview = '';
    if (isVideo) {
        mediaPreview = `<video src="${src}" class="pix_video" preload="metadata" muted></video>`;
    } else {
        mediaPreview = `<img src="${src}">`;
    }

    var media = `<div class="t_media_item" data-src="${src}" data-thum="${src}" data-type="${type}">
                    <a class="topic-img-de"><i class="ri-subtract-line"></i></a>
                    ${mediaPreview}
                </div>`;

    $(".img_show").prepend(media);
    $("#img_link_up").val('');
});

//ajax获取我的地理位置
$(document).on('click', '.loca .laqu', function () {
    var gaode_key = Theme.moments.gaode_key;
    var ip = '';
    $.ajax({
        type: "get",
        url: `/apis/uc.api.security.halo.run/v1alpha1/devices`,
        contentType: "application/json",
        success: function (data) {
            if (data.length > 0) {
                var ipAddress = data[0].device.spec.ipAddress;
                if (!/^10\.|^192\.|^168\.|^172\./.test(ipAddress)) {
                    ip = data[0].device.spec.ipAddress
                }
            }
            $.ajax({
                type: "get",
                url: `https://restapi.amap.com/v3/ip?ip=${ip}&key=${gaode_key}`,
                contentType: "application/json",
                beforeSend: function () {
                    $(".loca_text").text("数据拉取中..");
                },
                success: function (data) {
                    if (data.status == '1') {
                        var city = 'X市';
                        if (typeof data.city === 'string') {
                            city = data.city;
                        }
                        var province = 'X省';
                        if (typeof data.province === 'string') {
                            province = data.province;
                        }
                        var loca = "" + province + " · " + city + "";
                        $(".loca_text").html(loca).attr("state", '1'); //设置为开启
                    } else {
                        cocoMessage.error(data.info);
                    }
                }
            });
        }
    });
});

//插入自定义位置【点击关闭】
$(document).on('click', 'a.set_local_btn', function (e) {
    var loca = $("#set_local").val();
    var state = $(".loca_text").attr('state');
    if (loca == '') {
        cocoMessage.error("为空则不显示位置");
        $(".loca_text").text('').attr("state", '0');
        $("a.close_local").text('位置已关闭').css('color', '#ddd');
        $.removeCookie('mylocal', { path: '/' });
    } else {
        $(".loca_text").text(loca).attr("state", '1');
        $("a.close_local").text('位置已开启').css('color', '#8890cc');
        $.cookie('mylocal', loca, { expires: 30, path: '/' });
    }

    e.preventDefault();
    e.stopPropagation();
    UIkit.dropdown($('.local_box')).hide(0);
});

//插入自定义位置【input监控】
$(document).on('input', '#set_local', function () {
    var loca = $(this).val();
    var state = $(".loca_text").attr('state');

    if (loca == '') {
        cocoMessage.error("为空则不显示位置");
        $(".loca_text").text('').attr("state", '0');
        $("a.close_local").text('位置已关闭').css('color', '#ddd');
    } else {
        $(".loca_text").text(loca).attr("state", '1');
        $("a.close_local").text('位置已开启，点击关闭').css('color', '#8890cc');
    }
});

//关闭地理位置
$(document).on('click', 'a.close_local', function () {
    $(".loca_text").text('').attr("state", '0');
    $('#set_local, [name="set_local"]').val('');
    $("a.close_local").text('位置已关闭').css('color', '#ddd');
});

$(document).on('click', '.loca_text', function () {
    $("a.close_local").text('位置已开启，点击关闭').css('color', '#8890cc');
});

//发布更新瞬间
let isSubmittingMoment = false;
$(document).on('click', '.push_item', function () {
    if (isSubmittingMoment) return false;
    let closeLoading = null;
    var content = $("#topic_content").val();
    var catname = $(".t_cat_toogle span").text();
    var loca = $(".loca_text").text();
    var act = $('.push_item').attr('action');
    var pid = $('.push_item').attr('pid');
    var moment_type = $('.push_item').attr('type');
    var visible = $(".simi a").attr('visible');
    var min = Theme.moments.min_push_num;

    if (min > 0) {
        if (!content) {
            cocoMessage.error("请输入内容");
            return false;
        }
        if (content.length < min) {
            cocoMessage.error("内容不得少于" + min + "个字");
            return false;
        }
    }

    let tags = [];
    if (catname) {
        tags.push(catname);
        content += `<p><a class="tag" href="?tag=${catname}" data-pjax="">${catname}</a></p>`;
    }

    get_moment_data(moment_type);

    isSubmittingMoment = true;

    const $btn = $(this);
    $btn.attr('disabled', true).addClass('disabled');

    function release() {
        isSubmittingMoment = false;
        $btn.removeAttr('disabled').removeClass('disabled');
    }

    if (act === 'push') {

        let pushData = {
            apiVersion: 'moment.halo.run/v1alpha1',
            kind: 'Moment',
            metadata: { generateName: "moment-" },
            spec: {
                content: {
                    html: content,
                    medium: moment_data,
                    raw: content
                },
                owner: "",
                tags: tags,
                visible: visible
            }
        };

        if (loca && loca !== '数据拉取中..') {
            pushData.metadata.annotations = { mylocal: loca };
        }

        closeLoading = cocoMessage.loading('正在发布...');
        $('.push_close, .push_item').prop('disabled', true);

        $.ajax({
            type: "POST",
            url: `/apis/${Theme.moments.push_api}/v1alpha1/moments`,
            contentType: "application/json",
            data: JSON.stringify(pushData),
            success() {
                closeLoading?.()
                cocoMessage.success('发布成功！');
                const currentScrollTop = window.scrollY || document.documentElement.scrollTop;
                $.cookie('scrollParam', currentScrollTop, { path: '/', domain: 'anian.cc' });
                $.cookie('load_type', 'create', { path: '/', domain: 'anian.cc' });
                location.reload();
            },
            error() {
                $('.push_close, .push_item').prop('disabled', false);
                closeLoading?.()
                cocoMessage.error('发布失败！');
                release();
            }
        });

        return;
    }

    if (act === 'update') {

        let updateData = {
            apiVersion: 'moment.halo.run/v1alpha1',
            kind: 'Moment',
            metadata: {
                name: pid,
                generateName: "moment-",
                finalizers: ["tag-moment-protection", "moment-protection"],
                version: parseInt($('.push_item').attr('version'))
            },
            spec: {
                content: {
                    html: content,
                    medium: moment_data,
                    raw: content
                },
                owner: $('.push_item').attr('owner'),
                releaseTime: $('.push_item').attr('releaseTime'),
                tags: tags,
                visible: visible
            }
        };

        if (loca && loca !== '数据拉取中..') {
            updateData.metadata.annotations = { mylocal: loca };
        }

        closeLoading = cocoMessage.loading('正在更新...');
        $('.push_close, .push_item').prop('disabled', true);

        $.ajax({
            type: "PUT",
            url: `/apis/${Theme.moments.api}/v1alpha1/moments/${pid}`,
            contentType: "application/json",
            data: JSON.stringify(updateData),
            success() {
                closeLoading?.()
                cocoMessage.success('更新成功！');
                const currentScrollTop = window.scrollY || document.documentElement.scrollTop;
                $.cookie('scrollParam', currentScrollTop, { path: '/', domain: 'anian.cc' });
                $.cookie('load_type', 'update', { path: '/', domain: 'anian.cc' });
                location.reload();
            },
            error() {
                closeLoading?.()
                cocoMessage.error('更新失败！');
                $('.push_close, .push_item').prop('disabled', false);
                release();
            }
        });
    }
});

//获取片刻类型参数
function get_moment_data(type) {
    moment_data = [];
    switch (type) {
        case "image":
            $(".t_media_item").each(function () {
                var src = $(this).attr('data-src');
                var type = $(this).attr('data-type');
                var obj = {
                    url: src,
                    type: "PHOTO",
                    originType: type
                }
                moment_data.push(obj); //图片
            });
            break;
        case "card":
            $(".card_sortble .moment_card_item").each(function () {
                var pid = $(this).attr('pid');
                var type = $(this).attr('type');
                var obj = {
                    url: pid,
                    type: "POST",
                    originType: type
                }
                moment_data.push(obj); //卡片
            });
            break;
        case "audio":
            get_audio_data();
            break;
        case "video":
            get_video_data();
            break;
    }
}

//片刻音乐参数
function get_audio_data() {
    var type = $('.audio_choose li.uk-active a').attr('au_type');
    if (type == 'local') {
        var url = $('input#moment_audio_url').val();
        var obj = {
            url: url,
            type: "AUDIO",
            originType: 'audio/' + type
        }
    } else {
        var n_id = $('input#moment_audio_api').val();
        var obj = {
            url: n_id,
            type: "AUDIO",
            originType: 'audio/' + type
        }
    }
    moment_data.push(obj);
}

//片刻视频参数
function get_video_data() {
    var type = $('.video_choose li.uk-active a').attr('vi_type');
    if (type == 'local') {
        var url = $('input#moment_video_url').val();
        var obj = {
            url: url,
            type: "VIDEO",
            originType: 'video/mp4'
        }

    } else if (type == 'bili') {
        var bvid = $('input#moment_video_bili').val();
        var obj = {
            url: bvid,
            type: "VIDEO",
            originType: 'video/bili'
        }
    }
    moment_data.push(obj);

}

//片刻空值提示
function get_moment_error(type) {
    switch (type) {
        case "card":
            var v = $('.show_card .moment_card_item');
            if (v.length == 0) {
                return false;
            }
            break;
        case "audio":
            var au_type = $('.audio_choose li.uk-active a').attr('au_type');
            var url = $('input#moment_audio_url').val(); //歌曲链接
            var title = $('input#moment_audio_name').val(); //标题
            var cover = $('.loacl_audio .audio_left img').length; //封面
            if (au_type == 'local') {
                if (url == '' || title == '' || cover == 0) {
                    return false;
                }
            } else {
                var n_id = $('input#moment_audio_api').val();
                if (n_id == '') {
                    return false;
                }
            }
            break;
        case "video":
            var vi_type = $('.video_choose li.uk-active a').attr('vi_type');
            var url = $('input#moment_video_url').val();
            //var cover = $('.m_media_left img').length;
            if (vi_type == 'local') {
                if (url == '') {
                    return false;
                }
            } else {
                var bvid = $('input#moment_video_bili').val();
                if (bvid == '') {
                    return false;
                }
            }
            break;
    }
}

// ajax分类筛选 moment
$(document).on('click', '.moment_cat_nav ul li a', function () {
    var $this = $(this);
    if ($this.hasClass('disabled')) {
        return false;
    }
    $('.moment_cat_nav ul li a').addClass('disabled');

    var temp = $("#comment_form_reset");
    var formHtml = $("#t_commentform").prop('outerHTML');
    temp.html(formHtml);

    // --- 1. 保存旧内容---
    var $momentList = $(".moment_list");
    var oldContent = $momentList.html();
    var oldPagination = $('#t_pagination').prop('outerHTML');

    $(".moment_list").empty();
    $('#t_pagination a').hide();

    var cat = $this.attr('data');

    $.ajax({
        type: "GET",
        url: cat,
        beforeSend: function () {
            $('.moment_list').html('<div class="loading_box"><div uk-spinner></div></div>');
        },
        success: function (data) {
            $this.addClass('active').parent().siblings().children().removeClass('active');
            $('.moment_list .loading_box').remove();
            var result = $(data).find(".moment_list .p_item");
            $(".moment_list").append(result.fadeIn(300));

            var $newPagination = $(data).find("#t_pagination");
            var newHref = $newPagination.find("a").attr("data");

            // 检查当前页面是否有分页容器，如果没有则补齐
            if ($('#t_pagination').length === 0 && $newPagination.length > 0) {
                $(".moment_list").after($newPagination.prop('outerHTML'));
            }

            var $paginationBtn = $('#t_pagination a');

            if (newHref === undefined) {
                $paginationBtn.hide();
            } else {
                $paginationBtn.attr("data", newHref).text(Theme.site_page).show();
                $('#t_pagination').show();
            }
            getMomentAudio();
            initAgree();
            if (typeof lazyLoadInstance !== 'undefined') lazyLoadInstance.update();
            $('.moment_cat_nav ul li a').removeClass('disabled');
            initializeMomentFold();
            // if (typeof modify_moment_video_size === 'function') modify_moment_video_size();
            if (cat.indexOf('tag=') !== -1) {
                history.pushState({ url: cat }, '', cat);
            } else {
                history.pushState({ url: cat }, '', '/');
            }
            if (!$.cookie('scrollParam')) {
                $.removeCookie('page', { path: '/', domain: 'anian.cc' });
            }
            if (window._tagSwitchCallback) {
                window._tagSwitchCallback.resolve(); // 标记成功
                window._tagSwitchCallback = null; // 清理临时变量
            }
        },
        error: function () {
            $('.moment_cat_nav ul li a').removeClass('disabled');
            cocoMessage.error('内容获取失败');
            $('.moment_list .loading_box').remove();
            // 恢复列表内容
            $momentList.html(oldContent);
            if ($('#t_pagination').length > 0) {
                $('#t_pagination').replaceWith(oldPagination);
            } else {
                $momentList.after(oldPagination);
            }
            if (window._tagSwitchCallback) {
                window._tagSwitchCallback.reject(); // 标记失败
                window._tagSwitchCallback = null; // 清理临时变量
            }
        }
    });
});

// ajax分类筛选朋友圈
$(document).on('click', '.friends_cat_nav .cat-link', function () {
    var $this = $(this);
    if ($this.hasClass('disabled')) {
        return false;
    }
    $('.friends_cat_nav .cat-link').addClass('disabled');

    // 保留原有的表单重置逻辑（如果不需要可删除）
    var temp = $("#comment_form_reset");
    var formHtml = $("#t_commentform").prop('outerHTML');
    temp.html(formHtml);

    // --- 1. 保存旧内容---
    var $momentList = $(".moment_list");
    var oldContent = $momentList.html();
    var oldPagination = $('#t_pagination').prop('outerHTML');

    // 清空列表并隐藏分页
    $(".moment_list").empty();
    $('#t_pagination a').hide();

    // 获取目标 URL（注意这里用的是 data-href）
    var url = $this.attr('data-href');

    $.ajax({
        type: "GET",
        url: url,
        beforeSend: function () {
            // 显示加载动画
            $('.moment_list').html('<div class="loading_box"><div uk-spinner></div></div>');
        },
        success: function (data) {
            $this.addClass('active').parent().siblings().children().removeClass('active');
            // 移除加载动画
            $('.moment_list .loading_box').remove();

            // 提取并插入新的列表项
            var result = $(data).find(".moment_list .p_item");
            $(".moment_list").append(result.fadeIn(300));

            // 处理分页逻辑
            var $newPagination = $(data).find("#t_pagination");
            var newHref = $newPagination.find("a").attr("data");

            // 检查当前页面是否有分页容器，如果没有则补齐
            if ($('#t_pagination').length === 0 && $newPagination.length > 0) {
                $(".moment_list").after($newPagination.prop('outerHTML'));
            }

            var $paginationBtn = $('#t_pagination a');

            if (newHref === undefined) {
                $paginationBtn.hide();
            } else {
                $paginationBtn.attr("data", newHref).text(Theme.site_page).show();
                $('#t_pagination').show();
            }

            // --- 功能初始化区域（根据您的实际情况启用/禁用） ---
            if (typeof lazyLoadInstance !== 'undefined') lazyLoadInstance.update();
            $('.friends_cat_nav .cat-link').removeClass('disabled');
            initializeMomentFold();

            // 更新浏览器历史状态
            url = url.replace('author', 'tag');
            history.pushState({ url: url }, '', url);

            // 清除分页 cookie（保持原样）
            if (!$.cookie('scrollParam')) {
                $.removeCookie('page', { path: '/', domain: 'anian.cc' });
            }

            if (window._tagSwitchCallback) {
                window._tagSwitchCallback.resolve(); // 标记成功
                window._tagSwitchCallback = null; // 清理临时变量
            }
        },
        error: function () {
            $('.friends_cat_nav .cat-link').removeClass('disabled');
            if (window._tagSwitchCallback) {
                window._tagSwitchCallback.reject(); // 标记失败
                window._tagSwitchCallback = null; // 清理临时变量
            }
            cocoMessage.error('内容获取失败');
            $('.moment_list .loading_box').remove();
            // 恢复列表内容
            $momentList.html(oldContent);
            if ($('#t_pagination').length > 0) {
                $('#t_pagination').replaceWith(oldPagination);
            } else {
                $momentList.after(oldPagination);
            }
        }
    });
});

//ajax加载片刻
$(document).on('click', '#t_pagination a', function () {
    const postListElement = document.getElementById("post_item");
    var href = $(this).attr('data');
    const currentPage = parseInt(href.match(/\d+/)[0]);
    const $pagingArea = $('#t_pagination .post-paging');
    const originalPagingHtml = $pagingArea.html();

    $.ajax({
        type: "GET",
        url: href,
        beforeSend: function () {
            $('#t_pagination .post-paging').html('<div uk-spinner></div>');
        },
        success: function (posts) {
            if (posts) {
                var result = $(posts).find(".moment_list .p_item");
                $('#t_pagination .post-paging').html(`<a>${Theme.site_page}</a>`);
                $(".moment_list").append(result.fadeIn(300));
                window.pjax && window.pjax.refresh(postListElement)
                var newhref = $(posts).find("#t_pagination a").attr("data");

                if (newhref != undefined) {
                    $("#t_pagination a").attr("data", newhref);
                    $('#t_pagination a').show();
                } else {
                    $("#t_pagination a").hide();
                }
                if (!window.scroll_param || (!window.loadMoreTimeout && window.scroll_param > result.offset().top - 58)) {
                    $body.animate({ scrollTop: result.offset().top - 58 }, 500);
                } else window.forceStopLoading = true;
                getMomentAudio();
                initAgree();
                lazyLoadInstance.update();
                initializeMomentFold();
                $.cookie('page', currentPage, { path: '/', domain: 'anian.cc' });
            } else {
                $('#t_pagination a').hide();
            }
        },
        error: function () {
            $pagingArea.html(originalPagingHtml);
            $('#t_pagination a').show();
            cocoMessage.error('内容获取失败');
        }
    });
    return false;
});

// AJAX分类切换图库
$(document).ready(function () {
    $(document).on('click', '.photos_cat_nav ul li a', function (e) {
        var $this = $(this);

        if ($this.hasClass('disabled') || $this.hasClass('active')) {
            return false;
        }

        var url = $this.data('href');
        if (!url) {
            return false;
        }

        var tagText = $this.clone().find('span').remove().end().text().trim();

        var displayUrl;
        if (tagText === '全部') {
            displayUrl = '/photos';
        } else {
            displayUrl = '/photos?tag=' + encodeURIComponent(tagText);
        }

        $('.posts_cat_nav ul li a').addClass('disabled');

        // --- 1. 保存旧内容 (关键修改) ---
        var $photosItem = $("#photos_item");
        var oldPhotosContent = $photosItem.html(); // 保存图库列表内容
        var oldPagination = $('#p_pagination').length > 0 ? $('#p_pagination').prop('outerHTML') : ''; // 保存分页结构

        $.ajax({
            type: "GET",
            url: url,
            beforeSend: function () {
                $("#photos_item").html(
                    '<div class="loading_box" style="text-align:center;padding:50px;"><div uk-spinner></div></div>'
                );
                $('#p_pagination').hide();
            },
            success: function (data) {
                $this.addClass('active').parent().siblings().children().removeClass('active');
                var $data = $(data);

                var newContent = $data.find("#photos_item").html();
                $("#photos_item").html(newContent);

                var $newPagination = $data.find("#p_pagination");
                if ($newPagination.length > 0) {
                    var newHref = $newPagination.find("a").attr("data");
                    if (newHref) {
                        if ($('#p_pagination').length === 0) {
                            $("#photos_item").after($newPagination.prop('outerHTML'));
                        } else {
                            $('#p_pagination').replaceWith($newPagination.prop('outerHTML'));
                        }
                        $('#p_pagination').show();
                    } else {
                        $('#p_pagination').hide();
                    }
                } else {
                    $('#p_pagination').hide();
                }

                if (typeof pix !== 'undefined' && pix.initGalleryPhotos) {
                    pix.initGalleryPhotos();
                }
                if ($('.gallery-photos').length > 0 && typeof waterfall === 'function') {
                    waterfall('.gallery-photos');
                }

                $('.posts_cat_nav ul li a').removeClass('disabled');

                history.pushState({ path: displayUrl }, '', displayUrl);

                if (!$.cookie('scrollParam')) {
                    $.removeCookie('page', { path: '/', domain: 'anian.cc' });
                }
                if (window._tagSwitchCallback) {
                    window._tagSwitchCallback.resolve(); // 标记成功
                    window._tagSwitchCallback = null; // 清理临时变量
                }
            },
            error: function () {
                // --- 2. 还原旧内容 (关键修改) ---
                $photosItem.html(oldPhotosContent); // 恢复图库列表内容

                // 恢复分页结构
                if (oldPagination) {
                    if ($('#p_pagination').length > 0) {
                        $('#p_pagination').replaceWith(oldPagination);
                    } else {
                        $photosItem.after(oldPagination);
                    }
                }
                $('.posts_cat_nav ul li a').removeClass('disabled');
                cocoMessage.error('内容获取失败');
                if (window._tagSwitchCallback) {
                    window._tagSwitchCallback.reject(); // 标记失败
                    window._tagSwitchCallback = null; // 清理临时变量
                }
            }
        });

        return false;
    });
});

//ajax加载图库
$(document).on('click', '#p_pagination a', function () {
    var href = $(this).attr('data');
    const currentPage = parseInt(href.match(/\d+/)[0]);
    const $pagingArea = $('#p_pagination .post-paging');
    const originalPagingHtml = $pagingArea.html();
    const targetOffsetTop = $('#p_pagination a').offset().top;
    $.ajax({
        type: "GET",
        url: href,
        beforeSend: function () {
            $('#p_pagination .post-paging').html('<div uk-spinner></div>');
        },
        success: function (photos) {
            if (photos) {
                var result = $(photos).find(".norpost_list .gallery-photo");
                $('#p_pagination .post-paging').html(`<a>${Theme.site_page}</a>`);
                $(".gallery-photos").append(result.fadeIn(300));
                var newhref = $(photos).find("#p_pagination a").attr("data"); //找出新的下一页链接
                if (newhref != undefined) {
                    $("#p_pagination a").attr("data", newhref);
                    $('#p_pagination a').show();
                } else {
                    $("#p_pagination a").hide(); //如果没有下一页了，隐藏
                }
                if (!window.scroll_param || (!window.loadMoreTimeout && window.scroll_param > targetOffsetTop - 358)) {
                    $body.animate({ scrollTop: targetOffsetTop - 358 }, 500);
                } else window.forceStopLoading = true;
                pix.initGalleryPhotos();
                $.cookie('page', currentPage, { path: '/', domain: 'anian.cc' });
            } else {
                $('#p_pagination a').hide();
            }
        },
        error: function () {
            $pagingArea.html(originalPagingHtml);
            $('#p_pagination a').show();
            cocoMessage.error('内容获取失败');
        }
    });
    return false;
});

//阅读更多按钮
$(document).on('click', 'a.show-more-btn', function () {
    var t = $(this);
    t.prev().show();
    t.next().show();
    t.siblings('.dotd').hide();
    t.hide();
});

$(document).on('click', 'a.read-less-btn', function () {
    var t = $(this);
    t.siblings('.rm_hidden').hide();
    t.siblings('.dotd').show();
    t.prev().show();
    t.hide();
});


//ajax加载评论
$(document).on('click', '.show_comment', function () {
    $('.blog_list_inner').css('min-height', 'calc(100vh)');
    var pid = $(this).attr('pid');
    var other = $(this).parents('.moment_item').siblings('div');
    $.each(other, function (name, value) {

        //遍历每个 回复表单
        var otherid = $(this).find('.show_comment').attr('pid');
        $("#halo-comment-" + otherid).hide();

    });
    $(`#halo-comment-${pid}`).css('display', $(`#halo-comment-${pid}`).css('display') == 'block' ? 'none' : 'block');

});

//片刻点赞
$(document).on('click', '.up_like ', function () {

    if ($(this).hasClass('done')) {
        cocoMessage.info("地久天长");
        return false;
    } else {
        $(this).addClass('done');
        var pid = $(this).data("id");
        var key = $(this).data("key");
        let agreeArr = localStorage.getItem(`pix.upvoted.${key}.names`)
            ? JSON.parse(localStorage.getItem(`pix.upvoted.${key}.names`))
            : [];
        rateHolder = $(this).children('span');
        iconHolder = $(this).children('i');
        $.ajax({
            type: 'post',
            contentType: "application/json; charset=utf-8",
            url: '/apis/api.halo.run/v1alpha1/trackers/upvote',
            data: JSON.stringify({
                group: key == "moment" ? "moment.halo.run" : "content.halo.run",
                plural: key == "moment" ? "moments" : "posts",
                name: pid,
            }),
            success: function (data) {
                $(iconHolder).toggleClass('ri-heart-2-fill');
                var num = $(rateHolder).text();
                $(rateHolder).text(++num);
                agreeArr.push(pid);
                const val = JSON.stringify(agreeArr);
                localStorage.setItem(`pix.upvoted.${key}.names`, val);
                cocoMessage.success("卿卿与黄");
            }
        }); // end ajax

    }
});

function initAgree() {
    var agreeAnnius = $('.up_like')
    if (agreeAnnius.length > 0) {
        let agreeArr = JSON.parse(localStorage.getItem(`pix.upvoted.${$(agreeAnnius[0]).data("key")}.names`))
        for (var i = 0; i < agreeAnnius.length; i++) {
            let pid = $(agreeAnnius[i]).attr('data-id');
            if (agreeArr != null) {
                let flag = agreeArr.includes(pid);
                if (flag) {
                    $(agreeAnnius[i]).addClass('done');
                }
            }


        }
    }
}

function getMomentAudio() {
    var audioList = $('.pix_player.qt')
    if (audioList.length > 0) {
        for (var i = 0; i < audioList.length; i++) {
            let id = $(audioList[i]).attr('id');
            let type = $(audioList[i]).attr('type');
            var result = type.substring(type.indexOf("audio/") + 6);
            if (id != null && id != '') {
                var api = `${Theme.play.pix_mu_api || 'https://api.i-meto.com/meting/api'}?type=song&id=:id`
                var meta = {
                    type: result,
                    id: id,
                }
                let url = api.replace(":id", meta.id);
                $.ajax({
                    type: "get",
                    url: url,
                    success: function (res) {
                        var html = ''
                        if (res.length > 0) {
                            let author = res[0].author;
                            let pic = res[0].pic;
                            let title = res[0].title;
                            let url = res[0].url;
                            let lrc = res[0].lrc;
                            html = `<div class="player_thum">
                                <img  src="${pic}">
                                </div>
                                <div class="player_meta">
                                    <div class="title"><span class="name">${title}</span><span class="author">${author}</span></div>
                                    <div class="player_tool">
                                        <div class="player_time"><div class="current_time"></div><span></span><div class="total_time"></div></div>
                                    </div>
                                </div>
                                <a class="play_btn" data ="${url}" lrc="${lrc}"><i class="ri-play-line"></i></a>
                                <div class="play_bg"><img  src="${pic}"></div>`;
                        } else {
                            html = '音乐参数错误';
                        }
                        $('#' + id).html(html);

                    }
                });

            }

        }
    }
}

// ajax分类筛选 posts（首页片刻时改这个）
$(document).on('click', '.posts_cat_nav ul li a', function (e) {
    e.preventDefault();
    var $this = $(this);
    const postListElement = document.getElementById("post_item");
    var t = $('.posts_cat_nav ul li a');

    if (t.hasClass('disabled')) {
        return false;
    }
    $('.posts_cat_nav ul li a').addClass('disabled');

    // --- 1. 保存旧内容---
    var $momentList = $(".norpost_list");
    var oldContent = $momentList.html();
    var oldPagination = $('#pagination').prop('outerHTML');

    $(".norpost_list").empty();
    $('#pagination a').hide();

    var cat = $(this).attr('data');
    // 替换：获取a标签内的中文文本并去除空格
    var tag = $(this).text().trim();

    $.ajax({
        type: "GET",
        url: cat, // 完全保留原有请求URL，不修改
        headers: {
            Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"
        },
        beforeSend: function () {
            $('.norpost_list').html('<div class="loading_box"><div uk-spinner></div></div>');
        },
        success: function (data) {
            $this.addClass('active').parent().siblings().children().removeClass('active');
            $('#pagination a').text(Theme.site_page);
            var result = $(data).find(".norpost_list").children();
            $(".norpost_list").append(result.fadeIn(300));
            window.pjax && window.pjax.refresh(postListElement);

            let newhref = $(data).find("#pagination a").attr("data") || $(data).find(".arc_pagenav a").attr("data");
            if (newhref != undefined) {
                $("#pagination a").attr("data", newhref);
                $('#pagination a').show();
            } else {
                $('#pagination a').hide();
            }

            $('.loading_box').remove();
            initAgree();
            lazyLoadInstance.update();
            $('.posts_cat_nav ul li a').removeClass('disabled');

            // 仅新增：基于中文tag构造URL并更新（不影响请求逻辑）
            const urlParams = new URLSearchParams();
            let newUrl = '/archives';
            // 替换：判断是否为"全部"，非全部则追加中文tag参数
            if (tag !== '全部' && tag !== '') {
                urlParams.append('tag', tag);
                newUrl = `${newUrl}?${urlParams.toString()}`;
            }
            history.pushState({ url: newUrl }, '', newUrl);
            if (!$.cookie('scrollParam')) {
                $.removeCookie('page', { path: '/', domain: 'anian.cc' });
            }
            if (window._tagSwitchCallback) {
                window._tagSwitchCallback.resolve(); // 标记成功
                window._tagSwitchCallback = null; // 清理临时变量
            }
        },
        error: function () {
            $('.posts_cat_nav ul li a').removeClass('disabled');
            cocoMessage.error('内容获取失败');
            $('.norpost_list .loading_box').remove();
            // 恢复列表内容
            $momentList.html(oldContent);
            if ($('#pagination').length > 0) {
                $('#pagination').replaceWith(oldPagination);
            } else {
                $momentList.after(oldPagination);
            }
            if (window._tagSwitchCallback) {
                window._tagSwitchCallback.reject(); // 标记失败
                window._tagSwitchCallback = null; // 清理临时变量
            }
        }
    });
});

//ajax加载文章
$('body').on('click', '#pagination a', function () {
    const postListElement = document.getElementById("post_item");
    const $pagingArea = $('#pagination .post-paging');
    const originalPagingHtml = $pagingArea.html();
    var href = $(this).attr('data');
    const currentPage = parseInt(href.match(/\d+/)[0]);
    $.ajax({
        type: "GET",
        url: href,
        headers: {
            Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"
        },
        beforeSend: function () {
            $('#pagination .post-paging').html('<div uk-spinner></div>');
        },
        success: function (data) {
            if (data) {
                var result = $(data).find(".norpost_list ").children();
                $('#pagination .post-paging').html(`<a>${Theme.site_page}</a>`);
                $('.norpost_list').append($(result).fadeIn(400));

                window.pjax && window.pjax.refresh(postListElement)

                var newhref = $(data).find("#pagination a").attr("data") || $(data).find(".arc_pagenav a").attr("data"); //找出新的下一页链接

                if (newhref != undefined) {
                    $("#pagination a").attr("data", newhref);
                    $('#pagination a').show();
                } else {
                    $("#pagination a").hide(); //如果没有下一页了，隐藏
                }
                if (!window.scroll_param || (!window.loadMoreTimeout && window.scroll_param > result.offset().top - 58)) {
                    $body.animate({ scrollTop: result.offset().top - 58 }, 500);
                } else window.forceStopLoading = true;
                initAgree()
                lazyLoadInstance.update();
                $.cookie('page', currentPage, { path: '/', domain: 'anian.cc' });
            } else {
                $("#pagination a").hide();
            }
        },
        error: function () {
            $pagingArea.html(originalPagingHtml);
            $('#pagination a').show();
            cocoMessage.error('内容获取失败');
        }
    });
    return false;
});

// ajax加载分类页面文章
$('body').on('click', '.arc_pagenav a', function () {
    // 【新增】保存分页区域的原始HTML，用于失败时恢复
    const $pagingArea = $('.arc_pagenav');
    const originalPagingHtml = $pagingArea.html();
    const postListElement = document.getElementById("post_item");
    var href = $(this).attr('data');
    const currentPage = parseInt(href.match(/\d+/)[0]);

    $(this).hide();
    var content = $('.norpost_list');
    var href = $(this).attr('data');
    if (href != undefined) {
        $.ajax({
            type: "GET",
            url: href,
            headers: {
                Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"
            },
            beforeSend: function () {
                $('.arc_pagenav').append('<div uk-spinner></div>');
            },
            success: function (data) {
                var post = $(data).find(".norpost_list").children();
                content.append(post.fadeIn(300));

                window.pjax && window.pjax.refresh(postListElement)

                var newhref = $(data).find(".arc_pagenav a").attr("data"); //找出新的下一页链接
                if (newhref != undefined) {
                    $(".arc_pagenav a").attr("data", newhref);
                    $('.arc_pagenav a').show();
                } else {
                    $(".arc_pagenav a").hide(); //如果没有下一页了，隐藏
                }
                $('.arc_pagenav .uk-spinner').remove();
                if (!window.scroll_param || (!window.loadMoreTimeout && window.scroll_param > post.offset().top - 58)) {
                    $body.animate({ scrollTop: post.offset().top - 58 }, 500);
                } else window.forceStopLoading = true;
                initAgree()
                lazyLoadInstance.update();
                $.cookie('page', currentPage, { path: '/', domain: 'anian.cc' });
            },
            error: function () {
                $pagingArea.html(originalPagingHtml);
                cocoMessage.error('内容获取失败');
            }
        });
    }

    return false;

});

function loginUrl() {
    window.location.href = `/login?redirect_uri=${encodeURIComponent(window.location.pathname)}`;
}

$(document).on('mouseenter', '.lbc .left_menu_box ul li a , body.mod_third_s .left_menu_box ul li a', function (event) {
    var title = $(this).find('.nav_title').text();
    $(this).append("<span class='menu_tips'>" + title + "</span>");
});

$(document).on('mouseleave', '.lbc .left_menu_box ul li a , body.mod_third_s .left_menu_box ul li a', function (event) {
    $(this).find('.menu_tips').remove();

});

//滚动
$(document).ready(function (e) {
    $(window).scroll(function () {

        var b = $(window).scrollTop(); //监控窗口已滚动的距离;

        if (b > 190) {

            $('.top_bar').addClass('mobile_active');

        } else {
            $('.top_bar').removeClass('mobile_active');
        }

        if (b > 1500) {
            $('a.go_top').addClass('show');
            $('a.tool_top').addClass('show');
        } else {
            $('a.go_top').removeClass('show');
            $('a.tool_top').removeClass('show');
        }

    });
});

$.fn.autoTextarea = function (options) {
    var defaults = {
        maxHeight: null,//文本框是否自动撑高，默认：null，不自动撑高；如果自动撑高必须输入数值，该值作为文本框自动撑高的最大高度
        minHeight: 50
    };
    var opts = $.extend({}, defaults, options);
    return $(this).each(function () {
        $(this).bind("paste cut keydown keyup focus blur", function () {
            var height, style = this.style;
            this.style.height = opts.minHeight + 'px';
            if (this.scrollHeight > opts.minHeight) {
                if (opts.maxHeight && this.scrollHeight > opts.maxHeight) {
                    height = opts.maxHeight;
                    style.overflowY = 'scroll';
                } else {
                    height = this.scrollHeight;
                    style.overflowY = 'hidden';
                }
                style.height = height + 'px';
            }
        });
    });
};

$('body').on('click', '.com_msg_btn', function () {

    var num = $(this).attr('check');
    if (num == 0) {
        return false;
    }
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: Theme.ajaxurl,
        data: {
            'action': 'up_unread_msg',
        },
        beforeSend: function () {

        },
        success: function (data) {
            if (data.code == '0') {
                $('small.f_unread_num').remove();
            } else {
                return false;
            }
        }
    });

});

//搜索类型
$('body').on('click', '.s_set_box a', function () {
    var type = $(this).attr('data');
    $(this).addClass('active').siblings('a').removeClass('active');
    $('form#index_search input[type="hidden"]').val(type);
    $.cookie('s_type', type, { expires: 30, path: '/', domain: 'anian.cc' });
});

$('body').on('click', '.top_s_box a', function () {
    UIkit.dropdown('.s_set_box').hide(false);
    var data = $(this).attr('data');
    var type = $(this).text();
    $('.set_text').text(type);
    $('form#top_search input[type="hidden"]').val(data);
});

//黑暗模式
$('body').on('click', '.t_dark a', function () {
    var t = $('html');
    if (t.hasClass('dark')) {
        $.cookie('dark', 'normal', { path: '/', domain: 'anian.cc', expires: 30 });
    } else {
        $.cookie('dark', 'dark', { path: '/', domain: 'anian.cc', expires: 30 });
    }
    t.toggleClass('dark');

});
//片刻管理按钮  

$(document).on('mouseenter', '.post_control_btn', function (event) {
    var a = $(this).siblings('.post_control_box');
    a.addClass('show');
});

$(document).on('mouseleave', '.post_control', function (event) {
    $(this).find('.post_control_box').removeClass('show');
});


//社交小工具二维码
$(document).on('mouseenter', '.sw_social', function (event) {
    var qrbox = $(this).siblings('.sw_qrcode');
    if (qrbox.length > 0) {
        qrbox.addClass('active');
        qrbox.fadeIn(200);
    }
});

$(document).on('mouseleave', '.sw_social', function (event) {
    var qrbox = $(this).siblings('.sw_qrcode');
    if (qrbox.length > 0) {
        qrbox.removeClass('active');
        qrbox.fadeOut(200);
    }
});

//点击收起移动侧栏
$('body').on('click', '.m_offcanvas', function () {
    var target = event.target;
    var parentAnchor = $(target).closest('a');
    if (target.getAttribute('href') == '#' || parentAnchor.attr('href') == '#') {
    } else {
        UIkit.offcanvas('.m_offcanvas').hide();
    }
});

// 刷新内容（兼容一言和今日诗词API）
$('body').on('click', '.yiyan_box .change', function () {
    var url = Theme.widget_yiyan.api || 'https://v1.hitokoto.cn';
    fetch(url)
        .then(response => response.json())
        .then(data => {
            const targetElement = document.querySelector('.yiyan_box p');
            if (url.includes('jinrishici.com')) {
                // 今日诗词API处理逻辑：标题添加《》
                targetElement.innerText = data.content;
                targetElement.setAttribute("uk-tooltip", `《${data.origin}》${data.author}`);
            } else {
                // 原有一言API处理逻辑
                targetElement.innerText = data.hitokoto;
                targetElement.setAttribute("uk-tooltip", data.from);
            }
        })
});

$('.yiyan_box .change').click();

//pjax

$(document).on('pjax:error', function (event, xhr, textStatus, errorThrown, options) {
    cocoMessage.error('加载失败！');
});

if (Theme.pjax) {
    let pjaxSelectors = ["head title", "#pjax-container", ".pjax"]
    var pjax = new Pjax({
        elements: 'a:not([target="_blank"]):not([pjax="exclude"])',
        selectors: pjaxSelectors,
        analytics: false,
        cacheBust: false,
    });

    document.addEventListener('pjax:send', function () {
        NProgress.start();
    })

    document.addEventListener('pjax:complete', function () {
        lazyLoadInstance.update();
        typeof hljs === 'object' && hljs.highlightAll();
        typeof Prism === 'object' && Prism.highlightAll();
        autoload_posts_music();
        initBlog();
        refreshCommentWidget();
        NProgress.done();
        $.removeCookie('page', { path: '/', domain: 'anian.cc' });
    })
}

// 刷新评论组件函数
function refreshCommentWidget() {
    const commentWrappers = document.querySelectorAll('.halo-comment');
    if (commentWrappers.length > 0) {
        commentWrappers.forEach(wrapper => {
            const scripts = wrapper.querySelectorAll('script[type="module"]');
            scripts.forEach(oldScript => {
                const newScript = document.createElement('script');
                newScript.type = 'module';
                // 复制所有属性
                Array.from(oldScript.attributes).forEach(attr => {
                    if (attr.name !== 'type') {
                        newScript.setAttribute(attr.name, attr.value);
                    }
                });
                // 复制脚本内容
                newScript.textContent = oldScript.textContent;
                // 替换旧脚本
                oldScript.parentNode.insertBefore(newScript, oldScript);
                oldScript.remove();
            });
        });
    }
}

$('body').on('click', '.msg_modal_inner .unread_box .box .mark_read', function (e) {
    let msg_name = $(this).parent()[0].getAttribute('msg-name')
    let username = Theme.username
    $.ajax({
        type: "put",
        url: `/apis/api.notification.halo.run/v1alpha1/userspaces/${username}/notifications/${msg_name}/mark-as-read`,
        success: function (res) {
            pix.getMsg()
        }
    });
})


const getWindowInfo = () => {
    pix.initTopBar()
};
const debounce = (fn, delay) => {
    let timer;
    return function () {
        if (timer) {
            clearTimeout(timer);
        }
        timer = setTimeout(() => {
            fn();
        }, delay);
    }
};

const cancalDebounce = debounce(getWindowInfo, 1);
window.addEventListener('resize', cancalDebounce);

//固定顶部栏
$(document).scroll(function () {
    let topBar = $('#masthead .top_bar');
    var placeholder = document.querySelector('#masthead .uk-sticky-placeholder');
    var width = topBar.width();
    var scrT = document.documentElement.scrollTop;
    if (window.innerWidth > 960) {
        // 如果滚动的距离>60
        if (scrT > 60) {
            topBar.addClass('uk-active uk-sticky-fixed').css({ "width": width, "top": '0px', "position": "fixed" });
            placeholder.hidden = false;
        } else {
            topBar.removeClass('uk-active uk-sticky-fixed').css({ "width": "auto", "top": "auto", "position": "" });
            placeholder.hidden = true;
        }
    } else {
        // 如果滚动的距离>0
        if (scrT > 0) {
            topBar.addClass('uk-active uk-sticky-below')
        } else {
            topBar.removeClass('uk-active uk-sticky-below')
        }

    }

});

// 获取当前具有 "current" 类的元素
var currentElement = document.querySelector('.current');

// 检查是否找到了具有 "current" 类的元素
if (currentElement) {
    // 将当前元素滚动到视图中
    currentElement.scrollIntoView({
        behavior: 'smooth', // 平滑滚动
        block: 'start' // 将当前元素顶部对齐到视口顶部
    });
}

$('body').on('click', '.listree-btn', function () {
    "目录[+]" == $(".listree-btn").text() ? $(".listree-btn").attr("title", "收起").text("目录[-]").parent().next().show() : $(".listree-btn").attr("title", "展开").text("目录[+]").parent().next().hide();
    return !1
});

// toc
function autotree() {
    $(document).ready(function () {
        $('.listree-titles').html('<a class="listree-btn" title="展开">目录[+]</a>')
        $('#listree-ol').empty();
        $("#listree-ol").css("display", "none")

        // 获取所有标题元素
        var headings = document.querySelectorAll('.post-single .single-content *:is(h1, h2, h3, h4, h5, h6)');

        // 为标题元素添加 listree-list 类，并修改 id
        headings.forEach(function (heading, index) {
            heading.classList.add('listree-list');
            heading.id = 'listree-list' + index;
        });

        // 创建一个空的目录列表
        var tocList = document.querySelector('#listree-ol');

        // 保存每个级别的父列表项
        var parentItems = {};

        // 遍历每个标题元素
        headings.forEach(function (heading, index) {
            // 创建列表项
            var listItem = document.createElement('li');

            // 创建标题链接
            var link = document.createElement('a');
            // 直接设置链接的href为标题元素的id
            link.href = '#listree-list' + index;
            link.classList.add('toc-link', 'node-name--' + heading.tagName);
            link.textContent = heading.textContent.trim();

            // 将标题链接添加到列表项中
            listItem.appendChild(link);

            // 获取当前标题级别
            var level = parseInt(heading.tagName.slice(1));

            // 查找当前标题的父级列表项
            var parentItem = parentItems[level - 1];

            // 如果存在父级列表项，则将当前列表项作为其子项
            if (parentItem) {
                // 如果父级列表项还没有子列表，则创建一个子列表
                if (!parentItem.querySelector('ul')) {
                    var subList = document.createElement('ul');
                    parentItem.appendChild(subList);
                }

                // 将当前列表项添加到父级列表的子列表中
                parentItem.querySelector('ul').appendChild(listItem);
            } else {
                // 如果不存在父级列表项，则将当前列表项直接添加到根列表中
                tocList.appendChild(listItem);
            }

            // 更新当前级别的父列表项为当前列表项
            parentItems[level] = listItem;
        });


        $(".listree-btn").click(function () {
            "目录[+]" == $(".listree-btn").text() ? $(".listree-btn").attr("title", "收起").text("目录[-]").parent().next().show() : $(".listree-btn").attr("title", "展开").text("目录[+]").parent().next().hide();
            return !1
        });
        $("a.toc-link").click(function (event) {
            event.preventDefault();
            var targetId = $(this).attr("href");
            $("html, body").animate({
                scrollTop: $(targetId).offset().top - 77
            }, 800);
        });
        // 判断是否存在目录，设置显示或隐藏
        if ($('#listree-ol').children().length > 0) {
            $(".listree-box").css("display", "block");
        } else {
            $(".listree-box").css("display", "none");
        }
    })
    jQuery(document).ready(function (f) {
        var T = f(".listree-list");
        if (T.length < 1)
            return;
        var b = [];

        function Q() {
            T.each(function () {
                var T = f(this).offset();
                b.push(Math.floor(T.top))
            })
        }

        function a(T) {
            var b = f("#listree-ol li");
            var Q = f(".listree-list dt");
            if (b.length < 1)
                return;
            var a = b.outerHeight();
            if (!b.eq(T).hasClass("current")) {
                b.removeClass("current");
                b.eq(T).addClass("current");
                Q.animate({
                    top: a * T + (b.outerHeight() - Q.outerHeight()) / 2 + 1
                }, 50)
            }
        }

        function X() {
            if ($('#listree-ol').children().length < 1)
                return;
            var f = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
            var Q = Math.ceil(f + 77);
            var X = 0;
            for (var aU = 0; aU < b.length; aU++) {
                if (Q >= b[aU]) {
                    X = aU
                } else {
                    break
                }
            }
            if (X < 0)
                X = 0;
            if (!T.eq(X).hasClass("current")) {
                T.removeClass("current");
                T.eq(X).addClass("current");
                a(X)
            }
        }

        Q();
        setTimeout(X, 0);
        f(window).on("scroll", X)
    });
}

pix.addRuntime();

$(document).ready(function () {
    $("[data-fancybox]").fancybox({
        selector: '[data-fancybox]',
        loop: true,
        transitionEffect: 'slide',
        protect: true,
        buttons: ['slideShow', 'fullScreen', 'thumbs', 'close'],
        hash: false
    })
})

// 手机端底部菜单高亮
function highlightActiveMenu() {
    let currentPath = window.location.pathname.replace(/\/+$/, "");
    const pathParts = currentPath.split('/');
    let firstLevelPath = '/' + (pathParts[1] || '');
    if (firstLevelPath === '/') currentPath = '/';
    const menuRules = {
        diary: ['/', '/moments'],
        composition: ['/content', '/categories', '/tags', '/archives'],
        appendix: []
    };
    const footerMenu = document.querySelector('.footer_menu');
    if (!footerMenu) return;
    const diaryLi = footerMenu.querySelector('a[href="/"]').closest('li');
    const compositionLi = footerMenu.querySelector('a[href="/archives"]').closest('li');
    const appendixLi = footerMenu.querySelector('a[href="/navigator"]').closest('li');
    const allMenuLis = footerMenu.querySelectorAll('.item li');
    allMenuLis.forEach(li => {
        li.classList.remove('active');
        const link = li.querySelector('a');
        if (link) {
            link.classList.remove('active');
            link.style.color = "";
            const icon = link.querySelector("i");
            if (icon) icon.style.color = "";
        }
    });
    const highlightMenu = (menuLi) => {
        if (!menuLi) return;
        const link = menuLi.querySelector('a');
        menuLi.classList.add('active');
        link.classList.add('active');
        link.style.color = "#4CAF50";
        const icon = link.querySelector("i");
        if (icon) icon.style.color = "#4CAF50";
    };
    if (menuRules.diary.includes(firstLevelPath)) {
        highlightMenu(diaryLi);
    }
    else if (menuRules.composition.includes(firstLevelPath)) {
        highlightMenu(compositionLi);
    }
    else {
        highlightMenu(appendixLi);
    }
}


// 指定日期黑白效果
function setPageGrayscale() {
    const dateList = ['12-13'];
    const style = document.createElement('style');
    style.innerHTML = '.grayscale-mode{filter:grayscale(100%)!important;-webkit-filter:grayscale(100%)!important;transition:filter 60s ease!important;}';
    document.head.appendChild(style);
    const today = new Date();
    const todayMd = `${today.getMonth() + 1}-${today.getDate()}`;
    if (dateList.includes(todayMd)) {
        document.documentElement.classList.add('grayscale-mode');
    }
}

// 内容折叠初始化
function initializeMomentFold() {
    const foldConfig = {
        maxHeight: 240,
        moreText: '展开',
        lessText: '收起'
    };

    $('.moment-collapse-container').each(function () {
        const $contentContainer = $(this);
        if ($contentContainer.next('.moment-fold-btn').length > 0) return;

        // 完整内容高度
        const fullHeight = $contentContainer[0].scrollHeight;
        const needFold = fullHeight > foldConfig.maxHeight;
        if (!needFold) return;

        // 初始化折叠状态
        $contentContainer.addClass('collapsed').css('max-height', foldConfig.maxHeight + 'px');
        const $foldBtn = $(`<button type="button" class="moment-fold-btn">${foldConfig.moreText}</button>`);
        $contentContainer.after($foldBtn);

        // 折叠/展开切换
        $foldBtn.on('click', function () {
            const isCollapsed = $contentContainer.hasClass('collapsed');
            const heightDiff = fullHeight - foldConfig.maxHeight; // 完整高度与折叠高度的差值

            if (isCollapsed) {
                // 展开逻辑
                $contentContainer.removeClass('collapsed').css('max-height', fullHeight + 'px');
                $(this).text(foldConfig.lessText);
            } else {
                // 收起逻辑：页面向下滚动差值距离
                $('html, body').animate({ scrollTop: $(window).scrollTop() - heightDiff }, 300, 'linear');

                $contentContainer.addClass('collapsed').css('max-height', foldConfig.maxHeight + 'px');
                $(this).text(foldConfig.moreText);
            }
        });
    });
}


// // 更换banner函数封装（PJAX触发）
// const bg1 = document.querySelector('.bg1');
// const bg2 = document.querySelector('.bg2');
// let bannerIndex = 0;
// let isBg1Active = true;
// let hasSingleBannerShown = false;

// let bannerImages = [];

// function preloadImage(url) {
//     return new Promise((resolve, reject) => {
//         const img = new Image();
//         img.onload = () => resolve(url);
//         img.onerror = reject;
//         img.src = url;
//     });
// }

// function changeBanner(url) {
//     const show = isBg1Active ? bg2 : bg1;
//     const hide = isBg1Active ? bg1 : bg2;

//     show.style.backgroundImage = `url(${url})`;
//     show.style.opacity = 1;

//     hide.style.opacity = 0;

//     isBg1Active = !isBg1Active;
// }

// async function safeChangeBanner(url) {
//     await preloadImage(url);
//     changeBanner(url);
// }

// function nextBanner() {
//     if (bannerImages.length < 1) return;

//     if (bannerImages.length === 1) {
//         // 第一次调用：显示图片并标记为已显示
//         if (!hasSingleBannerShown) {
//             safeChangeBanner(bannerImages[bannerIndex]);
//             hasSingleBannerShown = true;
//         }
//         // 非第一次调用：直接返回，不触发切换
//         return;
//     }

//     safeChangeBanner(bannerImages[bannerIndex]);
//     bannerIndex = (bannerIndex + 1) % bannerImages.length;
// }

// // 重置状态函数
// function resetBannerState() {
//     bannerIndex = 0;
//     isBg1Active = true;
//     hasSingleBannerShown = false;
// }


// 更换背景函数封装
function setFixedBackground(bgImageUrl) {
    const body = document.body;
    body.classList.add('common-fixed-bg');
    body.style.backgroundImage = `url('${bgImageUrl}')`;
}

// 奇遇显示函数封装
function showCenterImage(imageUrl) {
    const container = document.getElementById('centerImgContainer');
    const img = document.getElementById('targetImg');

    // 重置所有状态（避免上一次操作残留影响）
    container.classList.remove('container-fade-active');
    img.classList.remove('img-fade-in', 'img-fade-out');
    img.style.opacity = 0;
    img.src = ""; // 先清空原有src

    // 定义图片加载成功的回调函数
    function handleImageLoad() {
        // 移除事件监听（防止重复绑定）
        img.removeEventListener('load', handleImageLoad);
        img.removeEventListener('error', handleImageError);

        // 图片加载成功后，再执行淡入动画
        container.classList.add('container-fade-active');
        img.classList.add('img-fade-in');

        // 图片显示1.5秒后，执行自动关闭逻辑
        setTimeout(() => {
            container.classList.remove('container-fade-active');
            img.classList.add('img-fade-out');

            setTimeout(() => {
                img.classList.remove('img-fade-in', 'img-fade-out');
            }, 500);
        }, 1500);
    }

    // 定义图片加载失败的回调函数
    function handleImageError() {
        // 移除事件监听
        img.removeEventListener('load', handleImageLoad);
        img.removeEventListener('error', handleImageError);

        // 重置容器和图片状态，不执行任何动画
        container.classList.remove('container-fade-active');
        img.classList.remove('img-fade-in', 'img-fade-out');
        img.style.opacity = 0;
        img.src = "";
    }

    // 绑定图片加载/失败事件监听（先绑定，再赋值src，避免缓存导致回调不触发）
    img.addEventListener('load', handleImageLoad);
    img.addEventListener('error', handleImageError);

    // 最后赋值src，触发图片加载（异步操作）
    img.src = imageUrl;
}

// 侧边栏农历信息显示函数
function anian_renderLunarUI() {
    const textEl = document.getElementById('anianLunarText');
    const cookieName = 'anian_lunar_info';
    const defaultText = '一日不见如隔三秋';
    const maxRetries = 20;

    if (!textEl) return;

    const doRender = (data) => {
        if (!data) {
            textEl.innerHTML = defaultText;
            return;
        }

        const { ganzhiYear = '', lunarMonth = '', lunarDay = '', jieqi = '', jijie = '', traditionalFestival = '' } = data;

        let text = `${ganzhiYear}年${lunarMonth}${lunarDay}`;
        const extra = [];
        if (jieqi) extra.push(jieqi);
        if (traditionalFestival) extra.push(traditionalFestival);
        if (extra.length) text += `<br>${extra.join('  ·  ')}`;

        textEl.innerHTML = text;

        if (jieqi === '大寒') showCenterImage('/upload/qiyu-jieqi-dahan.png');
        // setFixedBackground('/upload/JX3WJ.mp4_20260207_022345.027.jpg');
    };

    const tryRender = (attempt) => {
        let data = null;
        try {
            const cookieVal = anian_getCookie(cookieName);
            if (cookieVal) data = JSON.parse(cookieVal);
        } catch (_) { }

        if (data) {
            doRender(data);
        } else if (attempt < maxRetries) {
            setTimeout(() => tryRender(attempt + 1), 500);
        } else {
            doRender(null);
        }
    };

    tryRender(0);
}

// 绿色数字时钟粒子效果
function initGreenDigitalClock() {
    var t = 820;
    var a = 250;
    var r = 7;
    var n = 10;
    var e = .65;
    var f;
    var o = [];

    const v = ["#00FF7F", "#32CD32", "#228B22", "#008000", "#006400", "#90EE90", "#98FB98", "#00FA9A", "#00FF00", "#3CB371"];

    var h = [];
    var u = [
        [[0, 0, 1, 1, 1, 0, 0], [0, 1, 1, 0, 1, 1, 0], [1, 1, 0, 0, 0, 1, 1], [1, 1, 0, 0, 0, 1, 1], [1, 1, 0, 0, 0, 1, 1], [1, 1, 0, 0, 0, 1, 1], [1, 1, 0, 0, 0, 1, 1], [1, 1, 0, 0, 0, 1, 1], [0, 1, 1, 0, 1, 1, 0], [0, 0, 1, 1, 1, 0, 0]],
        [[0, 0, 0, 1, 1, 0, 0], [0, 1, 1, 1, 1, 0, 0], [0, 0, 0, 1, 1, 0, 0], [0, 0, 0, 1, 1, 0, 0], [0, 0, 0, 1, 1, 0, 0], [0, 0, 0, 1, 1, 0, 0], [0, 0, 0, 1, 1, 0, 0], [0, 0, 0, 1, 1, 0, 0], [0, 0, 0, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1]],
        [[0, 1, 1, 1, 1, 1, 0], [1, 1, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 1, 1, 0], [0, 0, 0, 1, 1, 0, 0], [0, 0, 1, 1, 0, 0, 0], [0, 1, 1, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 1, 1], [1, 1, 1, 1, 1, 1, 1]],
        [[1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 1, 1, 0], [0, 0, 0, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 0, 0, 1, 1, 0], [0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 1, 1], [1, 1, 0, 0, 0, 1, 1], [0, 1, 1, 1, 1, 1, 0]],
        [[0, 0, 0, 0, 1, 1, 0], [0, 0, 0, 1, 1, 1, 0], [0, 0, 1, 1, 1, 1, 0], [0, 1, 1, 0, 1, 1, 0], [1, 1, 0, 0, 1, 1, 0], [1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 1, 1, 0], [0, 0, 0, 0, 1, 1, 0], [0, 0, 0, 0, 1, 1, 0], [0, 0, 0, 1, 1, 1, 1]],
        [[1, 1, 1, 1, 1, 1, 1], [1, 1, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 1, 1], [1, 1, 0, 0, 0, 1, 1], [0, 1, 1, 1, 1, 1, 0]],
        [[0, 0, 0, 0, 1, 1, 0], [0, 0, 1, 1, 0, 0, 0], [0, 1, 1, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0], [1, 1, 0, 1, 1, 1, 0], [1, 1, 0, 0, 0, 1, 1], [1, 1, 0, 0, 0, 1, 1], [1, 1, 0, 0, 0, 1, 1], [1, 1, 0, 0, 0, 1, 1], [0, 1, 1, 1, 1, 1, 0]],
        [[1, 1, 1, 1, 1, 1, 1], [1, 1, 0, 0, 0, 1, 1], [0, 0, 0, 0, 1, 1, 0], [0, 0, 0, 0, 1, 1, 0], [0, 0, 0, 1, 1, 0, 0], [0, 0, 0, 1, 1, 0, 0], [0, 0, 1, 1, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0]],
        [[0, 1, 1, 1, 1, 1, 0], [1, 1, 0, 0, 0, 1, 1], [1, 1, 0, 0, 0, 1, 1], [1, 1, 0, 0, 0, 1, 1], [0, 1, 1, 1, 1, 1, 0], [1, 1, 0, 0, 0, 1, 1], [1, 1, 0, 0, 0, 1, 1], [1, 1, 0, 0, 0, 1, 1], [1, 1, 0, 0, 0, 1, 1], [0, 1, 1, 1, 1, 1, 0]],
        [[0, 1, 1, 1, 1, 1, 0], [1, 1, 0, 0, 0, 1, 1], [1, 1, 0, 0, 0, 1, 1], [1, 1, 0, 0, 0, 1, 1], [0, 1, 1, 1, 0, 1, 1], [0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 1, 1, 0], [0, 0, 0, 1, 1, 0, 0], [0, 1, 1, 0, 0, 0, 0]],
        [[0, 0, 0, 0], [0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
    ];

    function l(t) {
        var a = [];
        f.fillStyle = "#006400";
        var r = new Date;
        var e = 70, o = 30;
        var v = r.getHours();
        var u = Math.floor(v / 10);
        var l = v % 10;
        a.push({ num: u });
        a.push({ num: l });
        a.push({ num: 10 });
        var c = r.getMinutes();
        var u = Math.floor(c / 10);
        var l = c % 10;
        a.push({ num: u });
        a.push({ num: l });
        a.push({ num: 10 });
        var M = r.getSeconds();
        var u = Math.floor(M / 10);
        var l = M % 10;
        a.push({ num: u });
        a.push({ num: l });
        for (var p = 0; p < a.length; p++) {
            a[p].offsetX = e;
            e = m(e, o, a[p].num, t);
            if (p < a.length - 1) {
                if (a[p].num != 10 && a[p + 1].num != 10) {
                    e += n
                }
            }
        }
        if (h.length == 0) {
            h = a
        } else {
            for (var C = 0; C < h.length; C++) {
                if (h[C].num != a[C].num) {
                    s(a[C]);
                    h[C].num = a[C].num
                }
            }
        }
        i(t);
        g();
        return r
    }

    function s(t) {
        var a = t.num;
        var n = u[a];
        for (var e = 0; e < n.length; e++) {
            for (var f = 0; f < n[e].length; f++) {
                if (n[e][f] == 1) {
                    var h = {
                        offsetX: t.offsetX + r + r * 2 * f,
                        offsetY: 30 + r + r * 2 * e,
                        color: v[Math.floor(Math.random() * v.length)],
                        g: 1.5 + Math.random(),
                        vx: Math.pow(-1, Math.ceil(Math.random() * 10)) * 4 + Math.random(),
                        vy: -5
                    };
                    o.push(h)
                }
            }
        }
    }

    function i(t) {
        for (var a = 0; a < o.length; a++) {
            t.beginPath();
            t.fillStyle = o[a].color;
            t.arc(o[a].offsetX, o[a].offsetY, r, 0, 2 * Math.PI);
            t.fill()
        }
    }

    function g() {
        var n = 0;
        for (var f = 0; f < o.length; f++) {
            var v = o[f];
            v.offsetX += v.vx;
            v.offsetY += v.vy;
            v.vy += v.g;
            if (v.offsetY > a - r) {
                v.offsetY = a - r;
                v.vy = -v.vy * e
            }
            if (v.offsetX > r && v.offsetX < t - r) {
                o[n] = o[f];
                n++
            }
        }
        for (; n < o.length; n++) {
            o.pop()
        }
    }

    function m(t, a, n, e) {
        var f = u[n];
        for (var o = 0; o < f.length; o++) {
            for (var v = 0; v < f[o].length; v++) {
                if (f[o][v] == 1) {
                    e.beginPath();
                    e.arc(t + r + r * 2 * v, a + r + r * 2 * o, r, 0, 2 * Math.PI);
                    e.fill()
                }
            }
        }
        e.beginPath();
        t += f[0].length * r * 2;
        return t
    }

    var c = document.getElementById("canvas");
    c.width = t;
    c.height = a;
    f = c.getContext("2d");
    var M = new Date;
    setInterval(function () {
        f.clearRect(0, 0, f.canvas.width, f.canvas.height);
        l(f)
    }, 50)
}

// 侧边栏公告提醒 - 多RSS监控版（精准到秒+无兜底）
const COOKIE_EXPIRE_YEARS = 10;
const NOTICE_DETAIL_PATH = '/notice';
const RSS_URLS = [
    'https://git.anian.cc/anian/halo-theme-pix/releases.rss',
];

async function getAllLatestNoticeDatesFromRSS() {
    try {
        const rssResponses = await Promise.all(
            RSS_URLS.map(url => fetch(url))
        );
        for (let [index, response] of rssResponses.entries()) {
            if (!response.ok) {
                throw new Error(`RSS ${RSS_URLS[index]} 请求失败：${response.status}`);
            }
        }
        const allPubDates = [];
        for (let response of rssResponses) {
            const xmlText = await response.text();
            const parser = new DOMParser();
            const xmlDoc = parser.parseFromString(xmlText, 'text/xml');
            const items = xmlDoc.querySelectorAll('item');
            if (items.length === 0) continue;
            const itemDates = Array.from(items).map(item => {
                const pubDateStr = item.querySelector('pubDate').textContent;
                const pubDate = new Date(pubDateStr);
                return `${pubDate.getFullYear()}-${String(pubDate.getMonth() + 1).padStart(2, '0')}-${String(pubDate.getDate()).padStart(2, '0')}-${String(pubDate.getHours()).padStart(2, '0')}-${String(pubDate.getMinutes()).padStart(2, '0')}-${String(pubDate.getSeconds()).padStart(2, '0')}`;
            });
            allPubDates.push(itemDates[0]);
        }
        const sortedDates = allPubDates.sort((a, b) => new Date(b) - new Date(a));
        const combinedDateStr = sortedDates.join('_'); // 用_分隔多个日期

        return combinedDateStr;

    } catch (error) {
        return null;
    }
}

// 标记已读（接收拼接后的日期字符串）
function markAsRead(combinedDateStr) {
    if (!combinedDateStr) return; // 无有效日期则不操作
    const noticeBubble = document.getElementById('noticeBubble');
    const noticeBtn = document.getElementById('noticeBtn');
    if (!noticeBubble || !noticeBtn) return;

    $.cookie('latest_notice_date', combinedDateStr, { path: '/', domain: 'anian.cc', expires: 365 });
    noticeBubble.classList.remove('show');
    noticeBtn.classList.remove('has-notice');
}

// 检测公告详情页并标记已读
function checkNoticeDetailPage(combinedDateStr) {
    if (!combinedDateStr) return; // 无有效日期则不检查
    const currentPath = window.location.pathname.replace(/\/$/, '');
    const local_notice = $.cookie('latest_notice_date');
    const hasUnread = !local_notice || local_notice !== combinedDateStr;

    if (currentPath === NOTICE_DETAIL_PATH && hasUnread) {
        markAsRead(combinedDateStr);
    }
}

// 通知按钮绑定
function bindGlobalNoticeEvents(combinedDateStr) {
    if (document.body.dataset.noticeEventsBound) return;

    document.addEventListener('click', (e) => {
        const noticeBtn = document.getElementById('noticeBtn');
        if (noticeBtn && (e.target === noticeBtn || noticeBtn.contains(e.target))) {
            markAsRead(combinedDateStr);
        }
    });

    document.body.dataset.noticeEventsBound = 'true';
}

// 异步初始化（无兜底逻辑）
async function initNoticeTip() {
    // 1. 获取所有RSS的拼接日期字符串，失败则直接终止
    const combinedDateStr = await getAllLatestNoticeDatesFromRSS();
    if (!combinedDateStr) return; // 【核心】RSS失败则不检查、不显示提醒

    const noticeBubble = document.getElementById('noticeBubble');
    const noticeBtn = document.getElementById('noticeBtn');
    if (!noticeBubble || !noticeBtn) return;

    const local_notice = $.cookie('latest_notice_date');
    const hasUnread = !local_notice || local_notice !== combinedDateStr;

    // 2. 只有RSS获取成功，才判断并显示/隐藏提醒
    if (hasUnread) {
        noticeBubble.classList.add('show');
        noticeBtn.classList.add('has-notice');
        $('.footer_menu a[href="/navigator"]').css('color', '#ecc94b').find('i').css('color', '#ecc94b');
    } else {
        noticeBubble.classList.remove('show');
        noticeBtn.classList.remove('has-notice');
    }
    bindGlobalNoticeEvents(combinedDateStr);
    checkNoticeDetailPage(combinedDateStr);
}

// 测速侧边栏
const anianxSpeedBubble = document.getElementById("anianxBubble"),
    anianxSpeedBubbleText = document.getElementById("anianxBubbleText"),
    anianxSpeedGroup = document.querySelector(".anianx-group"),
    anianxSpeedMainLine = document.querySelector('[data-anianx-type="main"]'),
    anianxSpeedTestLine = document.querySelector('[data-anianx-type="test"]'),
    anianxSpeedSubLine = document.querySelector('[data-anianx-type="sub"]');
let anianxSpeedGlobalLock = false;

function anianxSpeedMoveArrow(btn) {
    if (anianxSpeedGlobalLock) return;
    const btnRect = btn.getBoundingClientRect(),
        groupRect = anianxSpeedGroup.getBoundingClientRect(),
        center = (btnRect.left - groupRect.left) + (btnRect.width / 2);
    anianxSpeedBubble.style.setProperty("--anianx-arrow", center + "px");
}

if (location.hostname === "anian.cc") {
    anianxSpeedMainLine.classList.add("anianx-fixed");
    anianxSpeedBubbleText.classList.remove("anianx-loading");
    anianxSpeedBubbleText.innerText = "当前为主线路";
    anianxSpeedMoveArrow(anianxSpeedMainLine);
} else if (location.hostname === "www.anian.cc") {
    anianxSpeedSubLine.classList.add("anianx-fixed");
    anianxSpeedBubbleText.classList.remove("anianx-loading");
    anianxSpeedBubbleText.innerText = "当前为副线路";
    anianxSpeedMoveArrow(anianxSpeedSubLine);
} else {
    anianxSpeedBubbleText.classList.remove("anianx-loading");
    anianxSpeedBubbleText.innerText = "当前为测试线路";
    anianxSpeedMoveArrow(anianxSpeedTestLine);
}

anianxSpeedMainLine.addEventListener("click", () => {
    if (anianxSpeedGlobalLock) return;
    anianxSpeedMoveArrow(anianxSpeedMainLine);
    anianxSpeedGlobalLock = true;
    if (anianxSpeedMainLine.classList.contains("anianx-fixed")) {
        anianxSpeedBubbleText.classList.remove("anianx-loading");
        anianxSpeedBubbleText.innerText = "当前为主线路";
        anianxSpeedGlobalLock = false;
    } else {
        anianxSpeedBubbleText.classList.add("anianx-loading");
        anianxSpeedBubbleText.innerText = "正在切换至主线路";
        anianxSpeedMoveArrow(anianxSpeedMainLine);
        const currentScrollTop = window.scrollY || document.documentElement.scrollTop;
        $.cookie('scrollParam', currentScrollTop, { path: '/', domain: 'anian.cc' });
        const finalUrl = anianxSpeedMainLine.dataset.anianxHref + location.pathname +
            location.search + location.hash;
        location.href = finalUrl;
    }
});

anianxSpeedSubLine.addEventListener("click", () => {
    if (anianxSpeedGlobalLock) return;
    anianxSpeedMoveArrow(anianxSpeedSubLine);
    anianxSpeedGlobalLock = true;
    if (anianxSpeedSubLine.classList.contains("anianx-fixed")) {
        anianxSpeedBubbleText.classList.remove("anianx-loading");
        anianxSpeedBubbleText.innerText = "当前为副线路";
        anianxSpeedGlobalLock = false;
    } else {
        anianxSpeedBubbleText.classList.add("anianx-loading");
        anianxSpeedBubbleText.innerText = "正在切换至副线路";
        anianxSpeedMoveArrow(anianxSpeedSubLine);
        const currentScrollTop = window.scrollY || document.documentElement.scrollTop;
        const baseUrl = anianxSpeedSubLine.dataset.anianxHref + location.pathname + location.search;
        $.cookie('scrollParam', currentScrollTop, { path: '/', domain: 'anian.cc' });
        const finalUrl = baseUrl + location.hash;
        location.href = finalUrl;
    }
});

anianxSpeedTestLine.addEventListener("click", () => {
    if (anianxSpeedGlobalLock) return;
    anianxSpeedMoveArrow(anianxSpeedTestLine);
    anianxSpeedGlobalLock = true;
    anianxSpeedBubbleText.classList.add("anianx-loading");
    anianxSpeedBubbleText.innerText = "测速中";
    anianxSpeedTestWithTimeout(7500).then(result => {
        anianxSpeedBubbleText.classList.remove("anianx-loading");
        anianxSpeedBubbleText.innerText = result;
    }).catch(() => {
        anianxSpeedBubbleText.classList.remove("anianx-loading");
        anianxSpeedBubbleText.innerText = "请检查网络或切换线路";
    }).finally(() => {
        anianxSpeedGlobalLock = false;
    });
});

function anianxSpeedTestWithTimeout(timeout) {
    const controller = new AbortController();
    const timer = setTimeout(() => controller.abort(), timeout);
    return new Promise(async (resolve, reject) => {
        try {
            const t0 = Date.now();
            const res = await fetch(`/upload/%E4%B8%8B%E8%BD%BD%20(9).mp4?t=${Date.now()}`, {
                signal: controller.signal
            });
            const blob = await res.blob();
            const size = blob.size;
            const sec = (Date.now() - t0) / 1000;
            const mbps = ((size * 8) / (sec * 1024 * 1024)).toFixed(2);
            clearTimeout(timer);
            resolve("正常：" + mbps + " Mbps");
        } catch (e) {
            clearTimeout(timer);
            reject(e);
        }
    });
}


function initTagSwitch() {
    return new Promise((resolve, reject) => {
        const pathname = window.location.pathname;
        const urlParams = new URLSearchParams(window.location.search);
        const activeTag = urlParams.get('tag');

        if (!activeTag || activeTag.trim() === '') {
            resolve();
            return;
        }

        let targetLink = $();

        if (pathname === '/archives') {
            targetLink = $('.posts_cat_nav ul li a').filter(function () {
                const linkText = $(this).contents().filter(function () {
                    return this.nodeType === 3;
                }).text().trim();
                return linkText === activeTag;
            });
        } else if (pathname === '/photos') {
            targetLink = $('.photos_cat_nav ul li a').filter(function () {
                const linkText = $(this).clone().find('span').remove().end().text().trim();

                return linkText === activeTag;
            });
        } else if (pathname === '/friends') {
            targetLink = $('.friends_cat_nav ul li a').filter(function () {
                const linkText = $(this).clone().find('span').remove().end().text().trim();
                return linkText === activeTag;
            });
        }

        if (targetLink.length > 0) {
            window._tagSwitchCallback = { resolve, reject };
            targetLink.click();
        } else {
            resolve();
        }
    });
}

// 1. 定义一个全局标志位，用于外部强制终止
window.forceStopLoading = false;
window.loadMoreTimeout = false;
function loadMultiplePages(pageCount) {
    return new Promise((resolve) => {
        let loadedCount = 0;
        window.forceStopLoading = false; // 每次启动重置标志位

        const getCurrentBtn = () => {
            const $allBtns = $('.post-paging a:visible');
            return $allBtns.filter(function () {
                const dataAttr = $(this).attr('data');
                return dataAttr !== undefined && dataAttr !== "" && dataAttr !== "无data属性";
            }).first();
        };

        const loadNext = () => {
            if (loadedCount >= pageCount || window.forceStopLoading) {
                resolve();
                return;
            }

            const $btn = getCurrentBtn();
            if (!$btn || !$btn.length) {
                resolve();
                return;
            }

            const $pager = $btn.closest('.post-paging');
            const oldHref = $btn.attr('data');
            const pageStartTime = Date.now();
            $btn.trigger('click'); // 加载更多

            const waitInterval = setInterval(() => {
                const now = Date.now();

                if (window.forceStopLoading) {
                    clearInterval(waitInterval);
                    resolve();
                    return;
                }
                if (now - pageStartTime > 3000) {
                    clearInterval(waitInterval);
                    window.loadMoreTimeout = true;
                    resolve(); // 解决 Promise，触发后续滚动
                    return;
                }

                const isLoading = $pager.find('[uk-spinner]').length > 0;
                if (isLoading) return; // 还在转圈，继续等待

                clearInterval(waitInterval);

                const $newBtn = getCurrentBtn();
                const newHref = $newBtn.length ? $newBtn.attr('data') : null;

                if (newHref && newHref !== oldHref) {
                    loadedCount++;
                    setTimeout(loadNext, 50);
                } else {
                    resolve();
                }
            }, 50);
        };
        loadNext();
    });
}

function initjump() {
    let closeLoading = null;
    const page = $.cookie('page');
    const scrollParam = $.cookie('scrollParam');
    const loadType = $.cookie('load_type')
    $.removeCookie('scrollParam', { path: '/', domain: 'anian.cc' });
    $.removeCookie('page', { path: '/', domain: 'anian.cc' });
    $.removeCookie('load_type', { path: '/', domain: 'anian.cc' });
    window.scroll_param = scrollParam;
    if (scrollParam && !isNaN(scrollParam)) {
        closeLoading = cocoMessage.loading('自动恢复原内容中...');
    }
    initTagSwitch().then(() => {
        const loadPageTask = () => {
            if (page && !isNaN(page)) {
                const targetPage = Number(page) - 1;
                return loadMultiplePages(targetPage);
            }
            return Promise.resolve();
        };
        if (scrollParam && !isNaN(scrollParam)) {
            loadPageTask().then(() => {
                closeLoading?.()
                const recordScroll = Number(scrollParam);
                const maxScroll = document.documentElement.scrollHeight - window.innerHeight;
                const scrollDistance = Math.min(recordScroll, maxScroll);
                $('html, body').animate({ scrollTop: scrollDistance }, 500);
                if (recordScroll > maxScroll && (recordScroll - maxScroll) > 200) {
                    cocoMessage.warning('请手动加载原线路内容');
                } else if (loadType === 'create') {
                    cocoMessage.success('发布成功！');
                } else if (loadType === 'update') {
                    cocoMessage.success('更新成功！');
                } else if (loadType === 'login') {
                    cocoMessage.success('登录成功！');
                } else if (loadType === 'logout') {
                    cocoMessage.success('登出成功！');
                } else {
                    const msg = location.hostname === "anian.cc" ? '主线路' : (location.hostname === "www.anian.cc" ? '副线路' : '测试线路');
                    cocoMessage.success('已成功切换至' + msg);
                }
            }).catch(() => closeLoading?.());
        }
    }).catch(() => closeLoading?.());
}


// 侧边栏订阅函数
async function handleSubscribe(event) {
    event.preventDefault();

    const form = event.target;
    const input = form.querySelector(".subscribe-input");
    const btn = form.querySelector(".subscribe-btn");
    const msg = document.getElementById("subscribe-message");

    const email = input.value.trim();
    if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
        return show("请输入正确格式的邮箱地址", "error");
    }

    btn.disabled = true;
    show("正在提交...");

    try {
        const res = await fetch(
            "https://anian.cc/apis/api.flow.post.kunkunyu.com/v1alpha1/follows/-/submit",
            {
                method: "POST",
                headers: { "Content-Type": "application/json" },
                body: JSON.stringify({ email })
            }
        );

        if (res.status === 200) {
            show("订阅成功", "success");
            input.value = "";
        } else {
            show("您已订阅过", "warning");
        }
    } catch {
        show("网络错误，请稍后再试", "error");
    }

    btn.disabled = false;

    function show(text, type) {
        msg.textContent = text;
        msg.className = `subscribe-message ${type}`;
    }
}

// 图库兼容视频监听刷新排版
let wfTimer = null;
document.addEventListener('loadedmetadata', function (e) {
    if (
        e.target.tagName === 'VIDEO' &&
        e.target.closest('.gallery-photos')
    ) {
        clearTimeout(wfTimer);
        wfTimer = setTimeout(() => {
            waterfall('.gallery-photos');
        }, 300);
    }
}, true);

function initBlog() {
    pix.initTopBar(),
        pix.getMsg(),
        initAgree(),
        pix.initGalleryPhotos(),
        pix.loadLightbox(),
        pix.topCategoriesBarScroll(),
        pix.topTableBarScroll(),
        getMomentAudio(),
        Theme.toc.all_open && autotree(),

        highlightActiveMenu(),
        syncFooterWithAudioState(),
        highlightMenu(),
        initializeMomentFold()
    // nextBanner()

}


$(document).ready(function () {
    pix.roleMoments();
    initBlog();

    setPageGrayscale();
    anian_renderLunarUI();
    updateMusicBtnColor('default');
    initGreenDigitalClock();
    initNoticeTip();
    initjump();
});



