//替换首页上的a标签
$('a').each(function() {
    
    var href = $(this).attr('href');
    
    
    if (href && href.indexOf("http://"+window.location.hostname) !== -1) {
        
        var newHref = href.replace('http://', 'https://');
        $(this).attr('href', newHref);

    }
});

// 头部代码 begin
var _menuObj = {
    _menuIndex: -1,
    _menuUrl: location.href,
    _menuList: [
        "/gnfblm/sy/",
        "/gnfblm/jgsz/",
        "/gnfblm/zwgk/",
        "/gnfblm/zcjd/",
        "/gnfblm/hdjl/"
    ]
}
for (var index = 0; index < _menuObj._menuList.length; index++) {
    var element = _menuObj._menuList[index];
    if (_menuObj._menuUrl.indexOf(element) != -1) {
        _menuObj._menuIndex = index;
    }
}
_menuObj._menuIndex = _menuObj._menuIndex == -1 ? 0 : _menuObj._menuIndex;
$(".menu_hd ul li").eq(_menuObj._menuIndex).addClass("on");

// 顶部搜索
var Rs = avalon.define({
    $id: "list_rs",
    serKey: "",//搜索词
    keys: [],
    error: false,
    setPost: function (key, e) {//热搜词直接点击搜索
        if (e) e.preventDefault();
        $("#header form #selecttags").val(key);
        $("#header form").submit();
    }

})

function pushkeys(dataArr) {
    for (var i = 0; i < dataArr.length; i++) {
        var item = {
            keyword: dataArr[i],
            surl: "http://ybj.fujian.gov.cn/search/?key=" + encodeURIComponent(dataArr[i])
        }
        Rs.keys.push(item);
    }
}

//热搜词，读取省政府热搜词
var hotKeyArr = [];
var keystr = SiteInfo.defaultHotWords || '';
hotKeyArr = keystr.split(",");
if (hotKeyArr !== undefined && hotKeyArr.length > 0) { defKeywords = hotKeyArr };
pushkeys(defKeywords);


//一级顶部搜索
var clickOdd = false;
$('.picker_2021').on("click", function () {
    var showInput = $('.picker_2021 input');
    var siteId = $('#form #siteId');
    var isMain = $('#form #isMain');
    if (clickOdd) {
        showInput.val('本站');
        //改回旧版检索
        $('.form').attr('action', '/search/');
        isMain.val('');
        siteId.val('');
        clickOdd = false;
        //改回旧版检索 end
        // $('.form').attr('action', '/ssp/main/search.html?key=');
        // isMain.val('');
        // siteId.val('ff8080816e59baf3016e5e7a7fc64259');
        // clickOdd = false;
        return
    } else {
        showInput.val('全省');
        siteId.attr('name', 'siteId');
        isMain.attr('name', 'isMain');
        $('.form').attr('action', 'https://www.fujian.gov.cn/ssp/main/index.html');
        isMain.val('1');
        siteId.val('ff808081624641aa0162476c0e0e0055');
        clickOdd = true;
    }
})
// 顶部搜索 end


//浮动层定位设置插件
$.fn.selectCity = function (targetId) {
    var _seft = this;
    var targetId = $(targetId);
    this.click(function () {
        var A_top = $(this).offset().top + $(this).outerHeight(true);  //  1
        var A_left = $(this).offset().left;
        targetId.show();
    });
    targetId.find("#tagClose").click(function () {
        targetId.hide();
    });
    $(document).click(function (event) {
        if (event.target.id != _seft.selector.substring(1)) {
            targetId.hide();
        }
    });
    targetId.click(function (e) {
        e.stopPropagation(); //  2
    });
    return this;
}
//调用浮动层


$(function () {
    $("#selecttags").selectCity("#searchTag");
});


// 头部下拉框显示异常
$(".menu_hov").slide({ titCell: ".menu_hd li", mainCell: ".menu_bd", defaultIndex: 0, returnDefault: true, titOnClassName: "active1" });

//头部下滑动到一定程度时悬浮顶部 begin
var ua = navigator.userAgent.toLowerCase();
$(window).scroll(function () {
    if (ua.match(/iPad/i) == "ipad" || ua.match(/Mobile/i) == "mobile") {

    }
    else {
        if (!avalon.vmodels.windowRoot.isSlb && !avalon.vmodels.windowRoot.isWza) {
            $(this).scrollTop() > 150 ? $(".header").addClass('fixed') : $(".header").removeClass('fixed');
            $(this).scrollTop() > 150 ? $('.rig-fix2').show() : $('.rig-fix2').hide();
        }
    }
});

//头部下滑动到一定程度时悬浮顶部 end

if (ua.match(/iPad/i) == "ipad" || ua.match(/Mobile/i) == "mobile") {
    $('.out_links li p').click(function () {
        $('body').css({
            "overflow": "hidden"
        })
        $(this).next('div').show().css({
            "position": "fixed",
            "top": ".1rem"
        });
        $(this).parent().siblings().find('div').hide().css({
            "position": "absolute",
            "top": "inherit"
        });
        $('.out_links_bg').show()
        $('.out_links_close').show();
    })
    $('.out_links_close').click(function () {
        $('body').css({
            "overflow": "inherit"
        })
        $('.out_links_bg').hide()
        $('.out_links_close').hide();
        $(this).prev('ul').find('div').hide();
    })
} else {
    $('.out_links li p').click(function () {
        $(this).next('div').slideToggle();
        $(this).parent().siblings().find('div').hide();
        return false;
    })
    $(document).click(function () {
        $('.out_links>ul>li>div').hide()
    })
}



function zz_con() {
    $('html').addClass('slb');
}

function close_zz_con() {
    $('html').removeClass('slb');
}



// 全站js

// 全站脚本

// 无障碍tab无缝切换
function tabWza(id, className, hdClassName) {
    var $hdLi = $(id).find(hdClassName || '.hd li')
    className = className || '.bd .wza-list'
    var $bdList = $(id).find(className)
    if ($bdList.length != $hdLi.length) return;
    $hdLi.each(function (index) {
        var $a = $(this).find('a:eq(0)')
        if ($a.length > 0) {
            $a.focus(function (e) {
                e = $(this).mouseover()
                $(this).bind('keydown.wza_tab', function (e) {
                    if (!e.ctrlKey && !e.altKey && e.keyCode === 9) {
                        var $curList = $bdList.eq(index)
                        avalon.log($curList.find('a').length)
                        if ($curList.find('a').length < -1) {
                            $hdLi.eq(index + 1).find('a:eq(0)').focus()
                            return false
                        }
                        $curList.find('a:eq(0)').focus()
                        return false
                    }
                })
            })
            $a.blur(function () {
                $(this).unbind('keydown.wza_tab')
            })
        }
    })
    $bdList.each(function (index) {
        if (index + 1 == $bdList.length) return;
        var $curList = $(this)
        var $li = $curList.find('li')
        var endTabLen = $li.length;
        $li.eq(endTabLen - 1).find('a:eq(0)').focus(function () {
            avalon.log($(this))
            $(this).bind('keydown.wza_tab_leave', function (e) {
                if (!e.ctrlKey && !e.altKey && e.keyCode === 9) {
                    $hdLi.eq(index + 1).find('a:eq(0)').focus()
                    return false;
                }
            })
        })
        $li.eq(endTabLen - 1).find('a:eq(0)').blur(function () {
            $(this).unbind('keydown.wza_tab_leave')
        })
    })


}


require(["browser", "tab", "site", "list2.trsapi", "addForm.letter", "tools", "dialog", "jquery", "pageview"], function (browser, tab, siteFun, listFun, addFun, tools, dialog, jQuery, pageview) {
    //适老版相关的方法
    var _cookie_BFree = $.cookie("BFree") || "";//获取适老版的cookie
    window._cookie_BFree = _cookie_BFree;
    window.bf = new BarrierFree({
        callback: function (state) {
            // state 1: 无障碍
            // state 2: 适老版
            if (state == 2) {
                var oldModeMenu = "";
                oldModeMenu += "<div class=\"rig-fix2 bf-pass\" id=\"oldOtherEvent\"  style=\"display:none;\"><ul class=\"wxmz wxmz1\" ><li event-type=\"zoomIn\"><a href=\"javascript:;\" ><span class=\"slb-rig01\"><\/span><p>页面放大<\/p><\/a><\/li><li event-type=\"zoomOut\"><a href=\"javascript:;\" >";
                oldModeMenu += "            <span class=\"slb-rig02\"><\/span>";
                oldModeMenu += "            <p>页面缩小<\/p>";
                oldModeMenu += "        <\/a><\/li>";
                oldModeMenu += "        <li event-type=\"caption\"><a href=\"javascript:;\" >";
                oldModeMenu += "            <span class=\"slb-rig03\"><\/span>";
                oldModeMenu += "            <p>显示屏<\/p>";
                oldModeMenu += "        <\/a><\/li>";
                oldModeMenu += "        <li event-type=\"readThrough\"><a href=\"javascript:;\" >";
                oldModeMenu += "            <span class=\"slb-rig04\"><\/span>";
                oldModeMenu += "            <p>阅读方式<\/p>";
                oldModeMenu += "        <\/a><\/li>";
                oldModeMenu += "        <li event-type=\"close\"><a href=\"javascript:;\">";
                oldModeMenu += "            <span class=\"slb-rig05\"><\/span>";
                oldModeMenu += "            <p>退出<\/p>";
                oldModeMenu += "        <\/a><\/li>";
                // oldModeMenu += "        <li><a href=\"/zzb/\">";
                // oldModeMenu += "            <span class=\"slb-rig06\"><\/span>";
                // oldModeMenu += "            <p>长者专题<\/p>";
                // oldModeMenu += "        <\/a><\/li>";
                oldModeMenu += "    <\/ul>";
                oldModeMenu += "<\/div>";
                oldModeMenu += "";
                $(document.body).append($(oldModeMenu));
                $("#oldOtherEvent li").on('click', function (e) {
                    e.stopPropagation();
                    if (!bf) return;
                    var eventType = $(this).attr("event-type");
                    if (eventType === 'close') {
                        bf.close();
                        return;
                    }
                    bf.register(eventType);
                });
                $(window).scroll(function () {
                    $(this).scrollTop() > 126 ? $('.rig-fix2').show() : $('.rig-fix2').hide();
                });
                // 加载适老版css
                // dynamicLoadCss("//images/21nd_zzms_style.css");
            }
        }
    });
    // 2022年3月19日 修正适老版class 添加问题 begin
    if (_cookie_BFree === '2') {
        $("html").addClass("slb");
    }
    // 2022年3月19日 修正适老版class 添加问题 end
    siteFun({   //初始化windowRoot
        vmArgsExt: {     //给windowRoot增加扩展vm属性
            siteFlag: "ybj"  //测试属性，允许扩展
            , weather: ''
            , Tem: ''
            , todayCount: ''
            , tipList: [] //头部搜索下拉框提示词
            , tixing: ''   //提醒、头部搜索框输入监听
            , flags: [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false]   // 预设的状态标记
            , isSlb: (_cookie_BFree === '2')       //是否是适老版
            , isWza: (_cookie_BFree === '1')       //是否是无障碍
            , $inSlb: function (e) { //进入适老版
                e && e.preventDefault();
                jQuery.cookie("BFree", "2", { path: '/', domain: 'fujian.gov.cn' });
                window.location.reload();
            }
            , $outSlb: function (e) { //退出适老版
                e && e.preventDefault();
                jQuery.removeCookie("BFree", { path: '/', domain: 'fujian.gov.cn' });
                window.location.reload();
            }
            , $togs: function (name, i, e) {
                e && e.preventDefault();
                avalon.vmodels.windowRoot[name].set(i, !avalon.vmodels.windowRoot[name][i]);
            }
            , getTip: function (name, i, val) {
                avalon.vmodels.windowRoot.flags.set(1, true);
                avalon.vmodels.windowRoot.tixing = val;
                avalon.vmodels.windowRoot[name].set(i, false);
                var showInput = $('.picker_2021 input').val();
                if (showInput == "本站") {
                    window.open("https://ybj.fujian.gov.cn/ssp/main/search.html?siteId=ff8080816e59baf3016e5e7a7fc64259&key=" + val);
                } else {
                    window.open("https://www.fujian.gov.cn/ssp/main/index.html?siteId=ff808081624641aa0162476c0e0e0055&isMain=1&key=" + val);
                }
            }
        }
    });

    avalon.vmodels.windowRoot.$watch("tixing", function(val){
        if (avalon.vmodels.windowRoot.flags[1]) {
            avalon.vmodels.windowRoot.flags.set(1, false);
            return false;
        }
        if (val !== ""){
            var nowDate = new Date().getTime();
            $.ajax({
                url: '/ssp/search/api/word?time=' + nowDate,
                type: "POST",
                dataType: "json",
                data: {
                    keyWord: val,
                    rows: 5,
                    siteId: "ff8080816e59baf3016e5e7a7fc64259",
                    pageSize: 5
                },
                success: function (res) {
                    if (res.error === false) {
                        avalon.vmodels.windowRoot.tipList = res.datas;
                        avalon.vmodels.windowRoot.flags.set(0, true);
                    }
                },
                error: function (e) {
                    avalon.log(e);
                }
            });
        } else {
            avalon.vmodels.windowRoot.flags.set(0, false);
        }
    })

    var pvTools = pageview();
    pvTools.view(PageInfo, function (d) {
        if (!d.error) {
            avalon.vmodels.windowRoot.todayCount = d.today;
            avalon.vmodels.windowRoot.siteCount = d.pvst;
            avalon.vmodels.windowRoot.pvCount = d.pv;
        }
    });

    //底部链接导航
    $('.yqlj_tit1_2021>ul>li').click(function () {
        if ($(this).hasClass('on')) {
            return
        } else {
            $(this).addClass('on').siblings().removeClass('on');
            $(this).siblings().children('.yqlj_con1_2021').slideUp()
            $(this).children('.yqlj_con1_2021').slideDown();
        }
        return false;   //阻止冒泡
    })
    $(document).click(function () {
        $('.yqlj_con1_2021').slideUp()
        $('.yqlj_tit1_2021>ul>li').removeClass('on');
    })
    //底部显隐 end

    // 手机版底部导航显隐 begin
    $('.xzk_2021>ul>li').click(function () {
        if ($(this).hasClass('on')) {
            return
        } else {
            $(this).addClass('on').siblings().removeClass('on');
            $(this).siblings().children('.mark_2021').slideUp()
            $(this).children('.mark_2021').slideDown();
        }
        return false;   //阻止冒泡
    })
    $(document).click(function () {
        $('.mark_2021').slideUp()
        $('.xzk_2021>ul>li').removeClass('on');
    })
    // 手机版底部导航显隐 end

    // 网址收藏 2022-11-1
    function _addFav2() {
        var url = window.location;
        var title = document.title;
        var ua = navigator.userAgent.toLowerCase();
        if (ua.indexOf("msie 8") > -1) {
            external.AddToFavoritesBar(url, title, ''); //IE8
        } else {
            try {
                window.external.addFavorite(url, title);
            } catch (e) {
                try {
                    window.sidebar.addPanel(title, url, ""); //firefox
                } catch (e) {
                    alert("加入收藏失败，请使用Ctrl+D进行添加");
                }
            }
        }
    }

    window._addFav = _addFav2;

    jQuery(".tabs").slide({
        titCell: ".tabs_hd li",
        mainCell: ".tabs_bd",
        effect: "left",
        targetCell: ".tabs_hd .more a"
    });
    jQuery(".tit_hov").slide({
        titCell: ".hd li",
        mainCell: ".bd",
        effect: "left",
        targetCell: ".hd .more a"
    });
    if (jQuery("html.slb").length) {
        jQuery(".tabs").slide({
            titCell: ".tabs_hd li",
            mainCell: ".tabs_bd",
            effect: "left",
            targetCell: ".tabs_hd .more a"
        });
        // window.location.reload();
    }

    function getRelDoc(relArgs) {
        var docsids = [];
        var docs = jQuery(relArgs.selecter || ".wj-a");
        docs.each(function (i) {
            docsids.push(~~this.id.replace(relArgs.idPre || "rel_", ""));
        });
        if (docsids.length === 0) return;
        var _docids = docsids.join(",");
        jQuery.ajax({
            url: "/fjdzapp/search"
            , type: 'POST'
            , dataType: "json"
            , data: {
                channelid: 100600,
                classsql: "(docid2=" + _docids + ")+(reldocid=" + _docids + ")",
                prepage: 150
            }
            , success: function (res) {
                if (res.error || res.data.length === 0) return;
                for (var d = 0; d < res.data.length; d++) {
                    var reldoc = res.data[d];
                    var _index = docsids.indexOf(reldoc.docid2);
                    var _index2 = docsids.indexOf(reldoc.reldocid);
                    if (_index > -1) {
                        reldoc._reltype = 1; //正向关联
                        reldoc.title = reldoc.tardoctitle;
                        reldoc.url = reldoc.tardocpuburl;
                        reldoc.docid = docsids[_index];
                    } else if (_index2 > -1) {
                        reldoc._reltype = 2; //反向关联
                        reldoc.title = reldoc.srcdoctitle;
                        reldoc.url = reldoc.srcdocpuburl;
                        reldoc.docid = docsids[_index2];
                    } else {
                        avalon.log("计算相关文档的类型错误，当前文档ID与返回的数据ID无法匹配");
                        avalon.log(reldoc.docid2, reldoc.reldocid, docsids)
                    }

                    if (reldoc.tardoctitle.indexOf("H5解读") > -1) {
                        reldoc._doctype = 'H5解读';
                    } else if (reldoc.tardoctitle.indexOf("图解") > -1) {
                        reldoc._doctype = '图解';
                    } else if (reldoc.tardoctitle.indexOf("政策解读") > -1) {
                        reldoc._doctype = '解读';
                    } else if (reldoc.tardoctitle.indexOf("媒体解读") > -1 || reldoc.tardoctitle.indexOf("媒体报道") > -1) {
                        reldoc._doctype = '媒体报道';
                    } else if (reldoc.tardoctitle.indexOf("动画解读") > -1) {
                        reldoc._doctype = '动画解读';
                    } else if (reldoc.tardoctitle.indexOf("视频解读") > -1) {
                        reldoc._doctype = '视频解读';
                    } else {
                        reldoc._doctype = "文件";
                    }

                    
                }
                var objectArraySort = function (keyName) {
                    var sort = [{ 'name': '解读', sort: 1 }, { 'name': '图解', sort: 2 }, { 'name': '动画解读', sort: 3 }, { 'name': '视频解读', sort: 4 }, { 'name': 'H5解读', sort: 5 }, { 'name': '媒体报道', sort: 6 }, { 'name': '文件', sort: 7 }]
                    return function (objectN, objectM) {

                        var valueN = objectN[keyName]
                        var valueM = objectM[keyName]

                        for (var i = 0; i < sort.length; i++) {
                            if (sort[i].name == valueN) {
                                valueN = sort[i].sort
                            }
                            if (sort[i].name == valueM) {
                                valueM = sort[i].sort
                            }
                        }

                        if (valueN < valueM) return -1
                        else if (valueN > valueM) return 1
                        else return 0
                    }
                }
                res.data.sort(objectArraySort("_doctype"))
                for(var a=0;a < res.data.length; a++){
                    var reldoc = res.data[a];
                    var _index = docsids.indexOf(reldoc.docid2);
                    var _index2 = docsids.indexOf(reldoc.reldocid);
                    if (_index > -1) {
                        reldoc._reltype = 1; //正向关联
                        reldoc.title = reldoc.tardoctitle;
                        reldoc.url = reldoc.tardocpuburl;
                        reldoc.docid = docsids[_index];
                    } else if (_index2 > -1) {
                        reldoc._reltype = 2; //反向关联
                        reldoc.title = reldoc.srcdoctitle;
                        reldoc.url = reldoc.srcdocpuburl;
                        reldoc.docid = docsids[_index2];
                    } else {
                        avalon.log("计算相关文档的类型错误，当前文档ID与返回的数据ID无法匹配");
                        avalon.log(reldoc.docid2, reldoc.reldocid, docsids)
                    }
                    if (typeof relArgs.callPreRel === 'function') {
                        relArgs.callPreRel(reldoc);
                    }
                }

                if (typeof relArgs.callPreRels === 'function') {
                    relArgs.callPreRels(res.data);
                }
            }
        });
    }
    //定义概览列表中的双向关联效果的展示
    function list_reldoc(d) {
        var d_data = d.data;
        if (d.hasOwnProperty("alldata")) {
            d_data = d.alldata;
        }
        var rel_push=[];
        
        for (var k = 0; k < d_data.length; k++) {
            var _doc = d_data[k];
            if (_doc.hasOwnProperty("rels") && _doc.rels.length > 0) {
                
                
                for (var r = 0; r < _doc.rels.length; r++) {
                    var _reldoc = _doc.rels[r];
                    
                    if (_reldoc.docid2 === _doc.docid2) { //相关文档的id等于当前文档ID，为正向关联
                        _reldoc._reltype = 1; //正向关联
                        _reldoc.title = _reldoc.tardoctitle;
                        _reldoc.url = _reldoc.tardocpuburl;
                    } else if (_reldoc.reldocid === _doc.docid2) {
                        _reldoc._reltype = 2; //反向关联
                        _reldoc.title = _reldoc.srcdoctitle;
                        _reldoc.url = _reldoc.srcdocpuburl;
                    } else {
                        avalon.log("计算相关文档的类型错误，当前文档ID与返回的数据ID无法匹配");
                        avalon.log(_reldoc.docid2, _reldoc.reldocid, _doc.docid2);
                    }

                    //下面是宁德关于是否是文件或解读的判断，要根据实际情况调整
                    if (_reldoc.tardoctitle.indexOf("H5解读") > -1) {
                        _reldoc._doctype = 'H5解读';
                    } else if (_reldoc.tardoctitle.indexOf("图解") > -1) {
                        _reldoc._doctype = '图解';
                    }else if (_reldoc.tardoctitle.indexOf("政策解读") > -1) {
                        _reldoc._doctype = '解读';
                    } else if (_reldoc.tardoctitle.indexOf("媒体解读") > -1 || _reldoc.tardoctitle.indexOf("媒体报道") > -1 || _reldoc.reldocid == 6093660) {
                        _reldoc._doctype = '媒体报道';
                    } else if (_reldoc.tardoctitle.indexOf("动画解读") > -1) {
                        _reldoc._doctype = '动画解读';
                    }else if (_reldoc.tardoctitle.indexOf("视频解读") > -1) {
                        _reldoc._doctype = '视频解读';
                    } else {
                        _reldoc._doctype = "文件";
                    }
                    rel_push.push(_reldoc)
                     
                }
                
            }
        }
        var objectArraySort = function (keyName) {
            var sort = [{ 'name': '解读', sort: 1 }, { 'name': '图解', sort: 2 }, { 'name': '动画解读', sort: 3 }, { 'name': '视频解读', sort: 4 }, { 'name': 'H5解读', sort: 5 }, { 'name': '媒体报道', sort: 6 }, { 'name': '文件', sort: 7 }]
            return function (objectN, objectM) {

                var valueN = objectN[keyName]
                var valueM = objectM[keyName]

                for (var i = 0; i < sort.length; i++) {
                    if (sort[i].name == valueN) {
                        valueN = sort[i].sort
                    }
                    if (sort[i].name == valueM) {
                        valueM = sort[i].sort
                    }
                }

                if (valueN < valueM) return -1
                else if (valueN > valueM) return 1
                else return 0
            }
        }
        rel_push.sort(objectArraySort("_doctype"))
        for (var g = 0; g < rel_push.length; g++) {
            var _reldoc = rel_push[g];
            var rel_html = []; //静态部分，使用jquery写入html代码
            if (_reldoc._reltype === 1 || _reldoc._reltype === 2) {
                rel_html=['[<a href="' + _reldoc.url + '" title="' + _reldoc.title + '" target="_blank">' + _reldoc._doctype + '</a>]'];
            }
            if (rel_html.length > 0) { //在列表页面的静态部分增加一个span，ID为 rel_*****
                jQuery("#relw_" + _reldoc.docid2).append(rel_html.join(""));
            }
        }
        
        return d;
    }
    window.list_reldoc = list_reldoc;
    window.getRelDoc = getRelDoc;
    //定义细览中的相关文档  end
})