var type_id = 1;
var sideLock=false;
var u = navigator.userAgent;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;
$(document).ready(function () {

    try {
        window.Origami.fastclick(document.body);
    } catch (e) {
    }
    var clickEvent = $.isFunction($.fn.tap) ? 'tap' : 'click';
    window['max_zindex'] = 100;
    $.fn.show_dialog = function () {
        window['max_zindex'] += 1;
        this.css('z-index', window['max_zindex']);
        this.show();
    };
    $('.btn-submit').on(clickEvent, function () {
        if($(this).hasClass('lock')){
            my_notify('亲，你已提交过信息');
            return;
        }
        var name = $('.name').val();
        var tel = $('.tel').val();
        var address = $('.address').val();
        if (name.match(/^\s*$/)) {
            my_notify('请填写姓名哦！');
            return false;
        }
        if (!tel.match(/^1\d{10}$/)) {
            my_notify('手机号好像不对～');
            return false;
        }
        if(!address){
            my_notify('请输入地址');
            return false;
        }
        $.ajax({
            url: '/index/addInfo',
            type: 'post',
            data: {
                "name":name,
                "tel":tel,
                "address":address
            },
            success: function (resp) {
                if(resp.errcode==0){
                    my_notify('提交成功');
                    $('.btn-submit').addClass('lock');
                    $('.page-info input').attr('disabled','disabled');
                }
            }
        });
    });
    var prize_lock = true;
    $('.btn-prize').on(clickEvent, function () {
        if (!prize_lock) {

            return;
        }

        $('.text_').html('抽奖中，<br>莫捉急～');
        $('.page_checking').show();

        prize_lock = false;
        $.ajax({
            url: '/index/get_prize',
            type: 'post',
            data: {
                'photo_url':$('.h-b').attr('src')
            },
            success: function (resp) {
                setTimeout(function () {
                    prize_lock = true;
                    $('.page-s-c').hide();
                    $('.page_checking').hide();
                    if (resp.errcode == 0) {
                        if (resp.data.prize_type == 0) {
                            $('.page-no-prize').show();
                        }else {
                            $('.page-prize').show();
                            if(resp.data.info_type==0 || resp.data.info_type==2){
                                $('.btn-submit').show();
                            }else {
                                $('.page-info input').attr('disabled','disabled');
                                $('.btn-submit').addClass('lock').show();
                                $('.name').val(resp.data.name);
                                $('.tel').val(resp.data.tel);
                                $('.address').val(resp.data.address);
                            }
                            if(resp.data.info_type==2 || resp.data.info_type==1){
                                my_notify('你已中过奖');
                            }
                        }
                    }
                }, 1000);

            }
        });
    });
    $('.btn-close-two').on('click',function () {
        $('.page_home').hide();
        $('.dialog_game_rule').hide();
        $('.page-through').show();
        $('#container,.wrap-inner').addClass('transform');
        setTimeout(function () {
            $('.page-through').hide();
            $('#container,.wrap-inner').removeClass('transform');
            $('.page-lang').show();
        },5000)
    });
    $('.btn-next-map').on('click',function () {
        $('.dialog_game_rule').show();
    });
    $('.btn-share').on('click',function () {
        $('.dialog-share').show();
    });
    $('.btn-j-x').on('click',function () {
        $('.page-no-prize').hide();
        $('.page-s-c').show();
    });
    $('.dialog-share').on('click',function () {
       $('.dialog').hide();
    });
    $('.btn-info').on('click',function () {
        $('.page-prize').hide();
        $('.page-info').show();
    });
    $('.c-d').on('click',function () {
        if($(this).hasClass('rotate-sate')){
            $('.c-d').removeClass('rotate-sate')
            document.getElementById('mp3-bj').pause();
        }else {
            $('.c-d').addClass('rotate-sate')
            document.getElementById('mp3-bj').play();
        }
    });
    $('input').on('blur',function(){
        window.scroll(0,0);
    });
    $(window).scroll(function(event){
        var wScrollY = window.scrollY; // 当前滚动条位置
        if(wScrollY>=270){
            $('.map-1').show()
        }
        if(wScrollY>=624){
            $('.map-2').show()
        }
        if(wScrollY>=969){
            $('.map-3').show()
        }
        if(wScrollY>=1331){
            $('.map-4').show()
        }
        if(wScrollY>=1715){
            $('.map-5').show()
            $('.map-6').show()
        }
        if(wScrollY>=2341){
            $('.map-7').show()

        }
        if(wScrollY>=2591){
            $('.map-8').show()

        }
        if(wScrollY>=2793){
            $('.map-9').show()

        }
        if(wScrollY>=2961){
            $('.map-10').show()

        }
        if(wScrollY>=3250){
            $('.map-11').show()

        }
        if(wScrollY>=3549){
            $('.map-12').show()
            $('.map-35').show()
        }
        if(wScrollY>=3692){
            $('.map-13').show()
            $('.map-36').show()
            $('.map-37').show()
        }
        if(wScrollY>=3882){
            $('.map-14').show()

        }
        if(wScrollY>=4266){
            $('.map-16').show()

        }
        if(wScrollY>=4557){
            $('.map-17').show()
            $('.map-31').show()
        }
        if(wScrollY>=5202){
            $('.map-18').show()
        }
        if(wScrollY>=5433){
            $('.map-19').show()
            $('.map-32').show()
        }
        if(wScrollY>=5788){
            $('.map-20').show()
            $('.map-33').show();
        }
        if(wScrollY>=5988){
            $('.map-34').show()

        }
        if(wScrollY>=6297){
            $('.map-21').show()
        }
        if(wScrollY>=6552){
            $('.map-22').show()
        }
        if(wScrollY>=6613){
            $('.map-23').show()
        }
        if(wScrollY>=7094){
            $('.map-24').show()
        }
        if(wScrollY>=7304){
            $('.map-25').show()
        }
        if(wScrollY>=7533){
            $('.map-26').show()
        }
        if(wScrollY>=7916){
            $('.map-27').show()
        }
        if(wScrollY>=8464){
            $('.map-28').show()
        }
        if(wScrollY>=8808){
            $('.map-29').show()
            $('.map-30').show()
        }
        console.log(wScrollY)
    });
    $('.map-29').on('click',function () {
        window.scroll(0,0);
        $('.page-lang').hide();
        $('.page-m-b').show();
        if(!sideLock){
            sideLock=true
            var mySwiper = new Swiper('.swiper-container', {
                navigation: {
                    nextEl: '.btn-left',
                    prevEl: '.btn-right'
                },
                on: {
                    slideChangeTransitionEnd: function () {
                        type_id = parseInt(this.activeIndex + 1);
                        if (type_id == 4) {
                            $('.btn-left').hide();
                        } else if (type_id == 1) {
                            $('.btn-right').hide();

                        } else {
                            $('.btn-left').show();
                            $('.btn-right').show();
                        }
                    }
                }

            });
        }
    });
    var width = '515';
    var height = '671';
    var stage = new Konva.Stage({
        container: 'b-j',
        width: width,
        height: height
    });

    var layer = new Konva.Layer();
    stage.add(layer);
    layer.on('tap', '#del', function (e) {
        this.getParent().remove();
        layer.draw();
        return false;
    });
    $('.btn-x-map').on('click', function () {
        var id = $(this).attr('data-id');
        var imageObj1 = new Image();
        imageObj1.src = 'Public/static/images/b-' + id + '.png';
        imageObj1.onload = function() {
        var darthVaderGroup = new Konva.Group({
            x: 100,
            y: 50,
            draggable: true
        });
        var yoda = new Konva.Image({
            x: 0,
            id: 'del',
            y: 263,
            image: imageObj1,
            width: imageObj1.width,
            height: imageObj1.height
        });
        darthVaderGroup.add(yoda);
        layer.add(darthVaderGroup);
        layer.draw();

        };
    });
    $('.btn-b-y').on('click',function () {
        $('.x-z').hide();
        $('.btn-map').show();
      // var url =stage.toDataURL();
    });
    $('.btn-j-c').on('click',function () {
        $('.text_').html('海报合成中，<br>莫捉急～');
        $('.page_checking').show();
        var url =stage.toDataURL();
        mergeImage_two(url)

    });
    $('.btn-s').on('click',function () {
        $('.page-s-c').hide();
        $('.page-x-z').show();
    });
    $('.btn-w-p .btn-f-h').on('click',function () {
        $('.x-z').hide();
        $('.x-z-sate').show();
    });
    $('.btn-close').on('click',function () {
        $('.dialog').hide();
    });
    $('.btn-next').on('click',function () {
        var bjGroup = new Konva.Group({
            x: 0,
            y: 0
        });
        var bj = new Image();
        bj.src = 'Public/static/images/side-'+type_id+'-'+type_id+'.png';
        bj.onload = function() {
            var bjImg = new Konva.Image({
                image: bj,
                width: 515,
                height: 671
            });
            bjGroup.add(bjImg);
            layer.add(bjGroup);
            layer.draw();
        };
        $('.x-z').hide();
        $('.x-z-sate').show()
        $('.dialog_rule').show();
        $('.page-m-b').hide();
        $('.page-x-z').show();
    });
    $('.btn-c-w').on('click',function () {
        $('.page-s-c').hide();
        $('.page-m-b').show()
    });
    $('.b-y .btn-f-h').on('click',function () {
        $('.page-x-z').hide();
        $('.page-m-b').show();

    });

});

function mergeImage_two(url) {
    var cover = new Image();
    var cover_three = new Image();
    var cover_four = new Image();
    var ceshi=new Image();;

    var url_three = '/Public/static/images/s-c.jpg?t=1';
    var url_two_t = url;
    cover_four.src = url_two_t;
    cover_three.src = url_three;
    if(isAndroid){
        cover_three.onload = function () {
            // cover_four.onload = function () {
            var tmp = $('<canvas></canvas>')[0];
            tmp.width = 640;
            tmp.height = 1238;
            cover_three.width = 640;
            cover_three.height = 1238;
            var ctx = tmp.getContext('2d');
            ctx.drawImage(cover_three, 0, 0);
            ctx.drawImage(cover_four, 63, 190);
            setTimeout(function () {

                $('.h-b').attr('src', tmp.toDataURL('image/jpeg', 0.5));
                $('.page-x-z').hide();
                $('.page-s-c').show();
                $('.page_checking').hide()
            }, 6000);
            // }

        }

    }else {
        cover_four.onload = function () {
             cover_three.onload = function () {
            var tmp = $('<canvas></canvas>')[0];
            tmp.width = 640;
            tmp.height = 1238;
            cover_three.width = 640;
            cover_three.height = 1238;
            var ctx = tmp.getContext('2d');
            ctx.drawImage(cover_three, 0, 0);
            ctx.drawImage(cover_four, 63, 190);
            setTimeout(function () {

                $('.h-b').attr('src', tmp.toDataURL('image/jpeg', 0.5));
                $('.page-x-z').hide();
                $('.page-s-c').show();
                $('.page_checking').hide()
            }, 6000);
            }

         }

    }


}

window.wx && wx.ready(function () {
    $('.c-d').addClass('rotate-sate')
    document.getElementById('mp3-bj').play();
    var base = location.origin;
    var wxFriendData = {
        'link': base,
        'imgUrl': base + '/Public/static/images/share-two.png',
        // 内容
        'desc': '重温八十年时代变迁，感受人生冷热酸甜迸发的力量',
        // 标题
        'title': '冷酸灵邀你“打卡”八十年的辉煌记忆',
        trigger: function () {
        },
        success: function () {
        }
    };
    var wxTimelineData = {
        'link': base,
        'imgUrl': base + '/Public/static/images/share-two.png',
        'title': '冷酸灵邀你“打卡”八十年的辉煌记忆',
        trigger: function () {


        },
        success: function () {

        }
    };
    wx.onMenuShareAppMessage(wxFriendData);
    wx.onMenuShareTimeline(wxTimelineData);
});