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.jpg',
];
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 = {
    'link1': 'https://www.zhi-niao.com/app/sharePage/index.html?courseId=N523943&appid=com.pingan.zhiniao&source=courseDetail&storeCourse=0',
    'link2': 'https://mp.weixin.qq.com/s/EGuxYBCuHXWLxnNGcFZhFg',
    'link3': 'https://mp.weixin.qq.com/s/zQITbJPoNVLZn8Zd7lkYOg',
    'link4': 'https://mp.weixin.qq.com/mp/homepage?__biz=MzU4OTQ0ODQ5OQ==&hid=8&sn=cf67d42f6baff02df58cfb16b8a2621c&devicetype=iOS12.3.1&version=1700092d&lang=zh_CN&nettype=WIFI&ascene=1&fontScale=100&scene=1&from=singlemessage&isappinstalled=0'
};

var readingJson = {};

let play = 'music';
let musicStatus = true;

$('.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="http://media.izhida.cn/pingan-journal-1.mp4" id="video1"></video>');
const video1 = document.getElementById("video1");
$('.page1').show();
preloadimages(preloadImageList, function () {}, function (progress) {});


$(document).ready(function () {
    carousel(100);
    document.addEventListener('touchstart', function () {}, false);
    $('.wrap').on('scroll', pageOnScroll);
    // for (let i = 0; i < 21; i++) {
    //     $('.read' + i).show().addClass('read');
    //     $('.link' + i).hide();
    // }


    // $('.music').click(function () {
    //     if ($('#music')[0].paused) {
    //         $('#music')[0].play();
    //         $('.music.stop').hide();
    //         $('.music.play').show();
    //         musicStatus = true;
    //     } else {
    //         $('#music')[0].pause();
    //         $('.music.play').hide();
    //         $('.music.stop').show();
    //         musicStatus = false;
    //     }
    // });

    // video1.addEventListener('pause', function () {
    //     if (video1.paused && video2.paused && video3.paused) {
    //         if (musicStatus) {
    //             $('#music')[0].play();
    //         }
    //     }
    // });

    // video1.addEventListener('play', function () {
    //     play = 'video1';
    //     $('#music')[0].pause();
    //     video2.pause();
    //     videoBreak = true;
    //     video3.pause();
    // });

    $('.read1').on('click', {
        link: linkAddress.link1,
        index: 1
    }, jumpTolink);
    $('.read2').on('click', {
        link: linkAddress.link2,
        index: 2
    }, jumpTolink);
    $('.read3').on('click', {
        link: linkAddress.link3,
        index: 3
    }, jumpTolink);
    $('.read4').on('click', {
        link: linkAddress.link4,
        index: 4
    }, jumpTolink);
});

function jumpTolink(event) {
    const link = event.data.link;
    setTimeout(function () {
        window.location.href = link;
    }, 100);

}

function showRead(event) {
    const index = event.data.index;
    $('.read' + index).show();
}

function pageOnScroll(event) {
    const top = $('.wrap').scrollTop();
    console.log(top);
}

function carousel(time) {
    let index = 1;
    setInterval(() => {
        $('.gif-block div').hide();
        $('.block7-img' + index).show();
        index = ((index + 1) > 6) ? 1 : index + 1;
    }, time);
}


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;
}