var type_id = 1;
var sideLock=false;
$(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();
        setTimeout(function () {
            $('.page-through').hide();
            $('.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')
        }else {
            $('.c-d').addClass('rotate-sate')
        }
    });
    $('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()

        }
        if(wScrollY>=3692){
            $('.map-13').show()

        }
        if(wScrollY>=3882){
            $('.map-14').show()

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

        }
        if(wScrollY>=4557){
            $('.map-17').show()

        }
        if(wScrollY>=5202){
            $('.map-18').show()
        }
        if(wScrollY>=5433){
            $('.map-19').show()
        }
        if(wScrollY>=5788){
            $('.map-20').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 () {
        $('.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 url_three = 'Public/static/images/s-c.jpg';
    var url_two_t = url;
    cover_four.src = url_two_t;
    cover_three.src = url_three;
    // 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);
        }

    }
}

window.wx && wx.ready(function () {
    var base = location.origin;
    var wxFriendData = {
        'link': base,
        'imgUrl': base + '/Public/static/images/share.jpg',
        // 内容
        'desc': '',
        // 标题
        'title': '',
        trigger: function () {
        },
        success: function () {
        }
    };
    var wxTimelineData = {
        'link': base,
        'imgUrl': base + '/Public/static/images/share.jpg',
        'title': '',
        trigger: function () {


        },
        success: function () {

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