function preloadimages(obj, complete_cb, progress_cb) {
    var loaded = 0;
    var toload = 0;
    var images = obj instanceof Array ? [] : {};
    toload = obj.length;
    for (var i = 0; i < obj.length; i++) {
        images[i] = new Image();
        images[i].src = obj[i];
        console.log(obj[i]);
        images[i].onload = load;
        images[i].setAttribute('crossOrigin', 'anonymous');
        images[i].onerror = load;
        images[i].onabort = load;
    }
    if (obj.length == 0) {
        complete_cb(images);
    }

    function load() {
        ++loaded;
        if (progress_cb) {
            progress_cb(loaded / toload);
        }
        if (loaded >= toload) {
            complete_cb(images);
        }
    }
}

var picUrl = window.base_resource_url + '/image2/';

var preloadImageList = [
    // picUrl + 'share.png',
];
if ($('div').length > 0) {
    $.each($('div'), function (index, val) {
        var img = $(val).css('background-image').replace(/^url\((['"]?)(.*)\1\)$/, '$2');
        img = $.trim(img);
        if (img && img.match(/[^/]+(jpg|png|gif)$/)) {
            preloadImageList.push(img);
        }
    });
}

const linkAddress = {
    //了解更多请点击
    'more': 'https://mp.weixin.qq.com/mp/homepage?__biz=MjM5ODA1NzAyMA==&hid=11&sn=54f0bdb2d5bc4a953ac4733e9bd1611a&scene=18',
    //马明哲部署平安集团开年四件大事：加强党建、抗击疫情、决胜脱贫、科技创新
    'link1': 'https://mp.weixin.qq.com/s/7hfzV8z2SaaKtZMQay8Mvw',
    //进展更新 | 总保额1万亿 平安人寿全国送1000万份"E路平安"抗"疫"
    'link2': 'https://mp.weixin.qq.com/s/RjDYhpIKn3PjKeoMwB_x8Q',
    // 新华社：平安医保科技：共抗疫情，打好科技战“疫”组合拳
    'link3': 'https://t.xinhuaapp.com/rR154rZ',
    // 和讯：平安战“疫”打出游击术 医疗科技“侦察兵”助力临床诊断
    'link4': 'https://m.hexun.com/news/2020-02-13/200283704.html?from=groupmessage&isappinstalled=0',
    // 秦朔朋友圈：智慧抗疫：挺进疫区中心的民营健康“黑马”
    'link5': 'https://mp.weixin.qq.com/s/ZfOQsQ37f-NRiT85drgzZw',
    // 新华网：专业抗疫 | 驰援新冠病毒核酸检验，平安旗下医学实验室整装备战！
    'link6': 'http://www.xinhuanet.com/money/2020-02/04/c_1125529659.htm',
    // 中华网：武汉平安好医医学检验实验室正式承接新冠病毒核酸检测服务
    'link7': 'https://m.tech.china.com/redian/2020/0204/459904.html',
    // 搜狐健康：武汉新型肺炎 | 武汉平安好医医学检验实验室承接武汉市新冠病毒核酸检测工作
    'link8': 'https://m.sohu.com/a/370546276_359980/?pvid=000115_3w_a&from=groupmessage&isappinstalled=0',
    // 人民日报：助力早发现，来自上海的移动CT影像车驰援湖北
    'link9': 'https://wap.peopleapp.com/article/5091672/4987004?from=groupmessage&isappinstalled=0',
    // 新华网：奔赴前线 | 平安移动CT影像车驰援武汉，专业医护团队展开多城救援
    'link10': 'http://www.xinhuanet.com/money/2020-02/03/c_1125527240.htm',
    // 健康时报：诊断力不够？移动CT影像车助力新型冠状病毒感染的肺炎诊断
    'link11': 'http://www.jksb.com.cn/index.php?m=wap&a=show&catid=583&id=158821',
    // 中国日报：免费阅片！平安健康（检测）中心70余位专家远程支援新型肺炎影像诊断
    'link12': 'https://cn.chinadaily.com.cn/a/202002/05/WS5e3a63cba3107bb6b579d579.html',
    // 健康报：“平安好影像”助力阻击疫情 科技抗“疫”见成效
    'link13': 'http://www.jkb.com.cn/yzyd/2020/0211/470330.html',
    // 第一财经：科技战疫丨千里之外 我也能为你日夜读片
    'link14': 'https://m.yicai.com/news/100503814.html?from=groupmessage&isappinstalled=0',
    // 新华社：共抗疫情|平安医保科技助力国家卫健委继教中心建设线上教学系统 
    'link15': 'https://t.xinhuaapp.com/rQg80Ss',
    // 健康报：基层疫情防控培训课程上线
    'link16': 'http://share.591adb.com/sharearticle/article/article_id/2977545/app_key/e70891ddafa5e012828c143390cdad9e',
    // 上海证券报：平安医保科技助力国家卫生健康委继教中心建设线上教学系统
    'link17': 'http://news.cnstock.com/news,jg-202002-4488234.htm',
    // 新华社：平安医保科技助力青岛市医保局打造“掌上办”服务 高频事项“不见面办”更方便！
    'link18': 'https://t.xinhuaapp.com/rPHzNGB',
    // 新华网：中国平安联合日本最大医疗检测公司捐赠防疫物资全部配送完毕
    'link19': 'http://www.xinhuanet.com/money/2020-02/03/c_1125527413.htm',
    // 了解更多请点击
    'link20': 'https://mp.weixin.qq.com/mp/homepage?__biz=MzU4OTQ0ODQ5OQ==&hid=7&sn=259750919ccdb2b34a17e2e64d3fb8ea&scene=1&devicetype=iOS12.3.1&version=1700092d&lang=zh_CN&nettype=3G+&ascene=7&session_us=gh_a1afb9b2b88b&fontScale=100&wx_header=1'
};

var readingJson = {
    // 'link1':0,   
    // 'link2':0,   
    // 'link3':0,   
    // 'link4':0,   
    // 'link5':0,   
    // 'link6':0,   
    // 'link7':0,   
    // 'link8':0,   
    // 'link9':0,   
    // 'link10':0,   
    // 'link11':0,   
    // 'link12':0,   
    // 'link13':0,   
    // 'link14':0,   
    // 'link15':0,   
    // 'link16':0,   
    // 'link17':0,   
    // 'link18':0,   
    // 'link19':0,   
    // 'link20':0  
};

$('.video1').append('<video controls poster="/Public/static/image2/video1.jpg" webkit-playsinline="webkit-playsinline" playsinline="playsinline" x5-playsinline="x5-playsinline" x-webkit-airplay="allow" src="/Public/static/media/video1.mp4" id="video1"></video>');
$('.video2').append('<video controls poster="/Public/static/image2/video2.jpg" webkit-playsinline="webkit-playsinline" playsinline="playsinline" x5-playsinline="x5-playsinline" x-webkit-airplay="allow" src="/Public/static/media/video2.mp4" id="video2"></video>');
$('.video3').append('<video controls poster="/Public/static/image2/video3.jpg" webkit-playsinline="webkit-playsinline" playsinline="playsinline" x5-playsinline="x5-playsinline" x-webkit-airplay="allow" src="/Public/static/media/video3.mp4" id="video3"></video>');
const video1 = document.getElementById("video1");
const video2 = document.getElementById("video2");
const video3 = document.getElementById("video3");
$('.page1').show();
preloadimages(preloadImageList, function () {
    // $('.home-page').show();
}, function (progress) {});

$(document).ready(function () {
    readingJson = {};
    readingJson = JSON.stringify(getCookie('reading'));
    console.log(readingJson);

    $("input").on("blur", function () {
        window.scroll(0, 0); //失焦后强制让页面归位
    });
    // $('.logo').css('top',getTranslateDistance(15));

    $('.play1').on('click', {
        id: 'video1'
    }, playVideo);

    $('.play2').on('click', {
        id: 'video2'
    }, playVideo);

    $('.play3').on('click', {
        id: 'video3'
    }, playVideo);

    video1.addEventListener('pause', function () {
        $('.play1').show();
    });

    video2.addEventListener('pause', function () {
        $('.play2').show();
    });

    video3.addEventListener('pause', function () {
        $('.play3').show();
    });

    $('.btn-more').on('click', {
        link: linkAddress.more
    }, jumpTolink);

    $('.link1').on('click', {
        link: linkAddress.link1,
        index: 1
    }, jumpTolink);
    $('.link2').on('click', {
        link: linkAddress.link2
    }, jumpTolink);
    $('.link3').on('click', {
        link: linkAddress.link3
    }, jumpTolink);
    $('.link4').on('click', {
        link: linkAddress.link4
    }, jumpTolink);
    $('.link5').on('click', {
        link: linkAddress.link5
    }, jumpTolink);
    $('.link6').on('click', {
        link: linkAddress.link6
    }, jumpTolink);
    $('.link7').on('click', {
        link: linkAddress.link7
    }, jumpTolink);
    $('.link8').on('click', {
        link: linkAddress.link8
    }, jumpTolink);
    $('.link9').on('click', {
        link: linkAddress.link9
    }, jumpTolink);
    $('.link10').on('click', {
        link: linkAddress.link10
    }, jumpTolink);
    $('.link11').on('click', {
        link: linkAddress.link11
    }, jumpTolink);
    $('.link12').on('click', {
        link: linkAddress.link12
    }, jumpTolink);
    $('.link13').on('click', {
        link: linkAddress.link13
    }, jumpTolink);
    $('.link14').on('click', {
        link: linkAddress.link14
    }, jumpTolink);
    $('.link15').on('click', {
        link: linkAddress.link15
    }, jumpTolink);
    $('.link16').on('click', {
        link: linkAddress.link16
    }, jumpTolink);
    $('.link17').on('click', {
        link: linkAddress.link17
    }, jumpTolink);
    $('.link18').on('click', {
        link: linkAddress.link18
    }, jumpTolink);
    $('.link19').on('click', {
        link: linkAddress.link19
    }, jumpTolink);
    $('.link20').on('click', {
        link: linkAddress.link20
    }, jumpTolink);
});

function jumpTolink(event) {
    const link = event.data.link;
    if (event.data.index) {
        const index = event.data.index;
        readingJson['link' + index] = 1;
        console.log(readingJson);

        setCookie('reading', JSON.parse(readingJson));
    }
    // window.location.href = link;
}

function playVideo(event) {
    const id = event.data.id;
    if (id == 'video1') {
        video1.play();
        video2.pause();
        video3.pause();
        $('.play1').hide();
        $('.play2').show();
        $('.play3').show();
    } else if (id == 'video2') {
        video2.play();
        video1.pause();
        video3.pause();
        $('.play2').hide();
        $('.play1').show();
        $('.play3').show();
    } else if (id == 'video3') {
        video3.play();
        video2.pause();
        video1.pause();
        $('.play3').hide();
        $('.play2').show();
        $('.play1').show();
    }
}


function getTranslateDistance(cardinal = 0, abs = false) {
    var wrapInnerTransform = $('.wrap-inner').css('transform');
    var wrapInnertop = parseFloat($('.wrap-inner').css('top'));
    wrapInnerTransform = wrapInnerTransform.match(/^matrix\(1,\s0,\s0,\s1,\s0,\s\-?(\d+)/);
    wrapInnerTransform = parseFloat(wrapInnerTransform[1]);
    if (abs) {
        var wrapInnerjl = Math.abs(wrapInnertop - wrapInnerTransform);
    } else {
        var wrapInnerjl = wrapInnertop - wrapInnerTransform;
    }

    return wrapInnerjl + cardinal + 'px';
}

//获取随机数方法
//min 最小值
//max 最大值
function RandomNumBoth(Min, Max) {
    var Range = Max - Min;
    var Rand = Math.random();
    var num = Min + Math.round(Rand * Range); //四舍五入
    return num;
}

//写cookies

function setCookie(name, value) {
    var Days = 30;
    var exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}

//读取cookies
function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");

    if (arr = document.cookie.match(reg))

        return unescape(arr[2]);
    else
        return null;
}