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 + '/images/';

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


preloadimages(preloadImageList, function () {

}, function (progress) {

});

// $('input[name=customize]').attr('placeholder','输入自定义文字（仅限四个字）').attr('maxlength','4');
$('input[name=tel]').attr('type','tel');
$('input[name=tel]').attr('maxlength',11);
$('.video-block').append('<video class="video" src="" x5-playsinline="" playsinline="" webkit-playsinline="" x-webkit-airplay="allow" x-webkit-airplay="true" ><video>');

$('.page1').show();

var videoStatus = false;
var video = document.getElementsByClassName('video');
video = video[0];

var allowChangeTel = false;

$(document).ready(function () {
    $("input").on("blur",function(){
        window.scroll(0,0);//失焦后强制让页面归位
    });
    //显示活动规则
    $('.btn-rule').on('click', showRule);
    //隐藏活动规则
    $('.page1 .btn-close').on('click', hideRule);
    //首页跳转到城市选择页
    $('.page1 .btn-start').on('click', showMap);
    //哈尔滨
    $('.map-page .heb').on('click', {types: 1}, handleCityClick);
    //北京
    $('.map-page .bj').on('click', {types: 2}, handleCityClick);
    //上海
    $('.map-page .sh').on('click', {types: 3}, handleCityClick);
    //三亚
    $('.map-page .sy').on('click', {types: 0}, handleCityClick);
    //奖品页返回城市选择页
    $('.prize-page .btn-backtomap').on('click',backToMap);
    //手机号被修改
    $('input[name=tel]').on('input',changeTel);
    //提交手机号
    $('.prize-page .btn-submit').on('click',submitTel);
});

function submitTel () {
    var tel = $('input[name=tel]').val();
    if(!tel.match(/^1\d{10}$/))
    {
        $('#notification_area').remove();
        my_notify('手机号码格式不正确');
        return false;
    }

    $.ajax({
        url: '/index/submit',
        type: 'POST',
        data: {
            tel: tel
        },
        success: function (resp) {
            if(resp.status == 1)
            {
                $('#notification_area').remove();
                my_notify('提交成功');
                window.tel = tel;
                $('input[name=tel]').val('');

            }
        }
    })
}

function changeTel () {
    if($('input[name=tel]').val() != window.tel)
    {
        $('.prize-page .btn-submit').show();
    } else {
        $('.prize-page .btn-submit').hide();
    }
}

function backToMap () {
    $('.prize-page').hide();
    $('.map-page').show();
}

function videoEnd (event) {
    $('.video-block').hide();
    if(window.type == 0)
    {
        $.ajax({
            url: '/index/tryprize',
            type: 'POST',
            success: function (resp) {
                if(resp.status == 1 || resp.status == 0) {
                    $('.map-page').hide();
                    $('.prize-page').show();
                    showPrize(resp.prize_type);
                    window.type = resp.prize_type;

                    video.removeEventListener('ended', videoEnd);
                }
            }
        });
    } else {
        $('.map-page').hide();
        $('.prize-page').show();
        showPrize(window.type);

        //提示领过奖并显示手机号
        $('#notification_area').remove();
        my_notify('您已经中过奖啦！');
        $('input[name=tel]').val(window.tel);
        $('.btn-submit').hide();
        allowChangeTel = true;

        video.removeEventListener('ended', videoEnd);
    }
}

function handleCityClick (event) {
    //播放哈尔滨视频
    $('.video').attr('src', '/Public/static/media/1.mp4?v=123');
    $('.video')[0].play();
    $('.video-block').show();

    //视频播放结束事件
    video.addEventListener('ended',videoEnd);
}

function showMap () {
    $('.page1').hide();
    $('.map-page').show();
}

function hideRule () {
    $('.dialog-rule').fadeOut(300);
}

function showRule () {
    $('.dialog-rule').fadeIn(300);
}

function showPrize (types) {
    //1 视频月卡
    //2 话费
    //3 电影票
    //0 未中奖
    $('.prize-page .prize').hide();
    if (types > 0) {
        $('.prize-page .prize' + types).show();
        $('.prize-page .tel-block').show();
    } else {
        $('.prize-page .tel-block').hide();
        $('.prize-page .no-prize').show();
    }
}

function getTranslateDistance(cardinal = 0) {
    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]);
    var wrapInnerjl = Math.abs(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;
}