$(function(){
    var tel={
        //总入口
        init:function(){
            var that=this;
            that.validate('#hack',that.msg.no_tel,that.btnname.just_white);
            //that.validate('#bind',that.msg.no_tel,that.btnname.just_white);
            that.time('#hack');
            //that.close('#close');
            that.close('#go');
            that.active();
            that.blurValidatetel('keyup');
            that.blurValidatetel('blur');
            that.message('#message');
            that.focu(document);
        },

        //弹出信息
        msg:{
            no_tel:'请认真填写你的手机号',
            no_yanz:'请填写验证码',
            yanz_err:'验证码错误',
            lg:'该手机已经领取过红包',
            no_lq:'该手机不能领取红包',
            remove:'获取验证码哦',
            over:'该手机已经领取过红包',
            no_pass:'该手机号不能领取红包'
        },
        //按钮信息
        btnname:{
            just_white:'这就写',
            rewrite:'重新填写',
            check:'知道了'
        },
        //判断验证顺序
        b:false,
        //插入验证结构
        validate:function(id,msg,btnmsg,sj){
            var that=this;
            $(id).on('tap',function(e){
                e.preventDefault();//阻止浏览器默认事件
                var telVal=$('#tel_number').val(),
                    tel=new RegExp("^[1][3-8]+\\d{9}");
                //console.log(tel.test(telVal));
                //验证输入手机规则
                if(telVal==''||(!tel.test(telVal))){
                    that.appendDom(msg,btnmsg);//插入HTML弹窗结构
                    that.b=false;
                }
                else{
                    that.b=true;
                    return;
                }
            })
        },
        blurValidatetel:function(eve){
            var that=this;
            $("#tel_number").on(eve,function(){
                var thisVal=$(this).val(),
                    tel=new RegExp("^[1][3-8]+\\d{9}");
                if(thisVal==''||(!tel.test(thisVal))){
                    that.b=false;
                    return false;
                }
                else {
                    that.b=true;
                }
            })
        },
        //dom结构
        appendDom:function(msg,btnmsg){
            var dom='<div class="layer_box center" id="bomb">'+
                '<div class="layer">'+
                '<div class="top_msg">'+
                '<p>'+msg+'</p>'+
                '</div>'+
                '<div class="bottom_btn">'+
                '<p id="go">'+btnmsg+'</p>'+
                '</div>'+
                '</div>'+
                '</div>';
            $('body').append(dom);
        },
        //留言
        input_msg:function(){
            var   dom='<div class="layer_box" id="bomb">'+
                '<div class="layer" style="padding-bottom: 2px">'+
                '<div class="top_msg" style="padding: 0 20px;line-height: 126px">'+
                '<div><textarea id="pass_msg" placeholder="写下你想说的话" style="width:98%;height:50px;font-size: 0.4rem;line-height: 0.4rem;"></textarea></div>'+
                '</div>'+
                '<div class="bottom_btn center" style="background: #fff;">'+
                '<p id="go" class="post_btn" style="float: left;width: 50%;border-top: 1px solid #bfbfbf;color: #000;">取消</p>'+
                '<p id="post" style="float:left; color: #ff4e4e;width: 49.5%;border-top: 1px solid #bfbfbf;border-left: 1px solid #bfbfbf;">发送</p>'+
                '</div>'+
                '</div>'+
                '</div>';
            $('body').append(dom);
        },
        //关闭弹出
        close:function(id){
            $(id).live('tap',function(e){
                e.preventDefault();
                $('#bomb').remove();
                $('body').css('overflow','');
            });
        },
        //倒计时
        time:function(id,msg,btnmsg){
            var that=this,
                times=function(){
                    if(that.b==false){
                        return;
                    }
                    tel = $("#tel_number").val();
                    var url_host=window.location.hostname;
                    $.post("/mobile/index.php?act=login&op=getcode&type=getbonus&client_type=ajax", {
                        mobile: tel
                    }), function (data, status) {
                        return;
                    };

                    var yzVal = $('#yanz').val(),
                        time = 60,
                        a = setInterval(function () {
                            time = time - 1;
                            $(id).text(time);
                            if (time == 0) {
                                clearInterval(a);
                                $(id).text('重新发送');
                            }
                        }, 1000);
                };
            $(id).on('tap', function () {
                //判断倒计时是否开始了
                var text = $(this).text();
                if (text == '重新发送' || text == '获取验证码') {
                    times();
                }
                else {
                    return;
                }
            })
        },
        //验证码验证
        active: function () {
            var that = this;
            $('#bind').on('tap', function () {
                var b = that.b;
                //yanzVal = $('#yanz').val();
                if (b == false) {
                    that.appendDom(that.msg.no_tel, that.btnname.just_white);
                }
                else {
                    if (yanzVal == '') {
                        that.appendDom(that.msg.no_yanz, that.btnname.just_white);
                    }

                    else {
                        var yanzVal = $("#yanz").val();
                        var tel = $("#tel_number").val();
                        var alink = $("#url").val();
                        var bonus_sn = $("#mine_bonus").val();
                        var relay_id = $("#relay_id").val();

                        $.ajax({
                            type:'post',
                            dataType:'json',
                            url:'/mobile/index.php?act=bonusex&op=bind&client_type=ajax',
                            data:{
                                mobile: tel,
                                code: yanzVal,
                                bonus_sn:bonus_sn,
                                relay_id:relay_id
                            },
                            success:function(data)
                            {
                                if (data.code==200) {
                                    location.reload();
                                }
                                else {
                                    that.appendDom(that.msg.yanz_err, that.btnname.rewrite);
                                }
                            }
                        });
                    }
                }
            })
        },
        //留言框
        message:function(id){
            var that=this;
            $(id).live('tap',function(){
                if($(this).text()!=='留言'){
                    return false;
                }
                else{
                    $('body').css('overflow','hidden');
                    that.input_msg(that.msg.no_yanz, that.btnname.just_white);
                    $('#pass_msg').focus();
                }
            });
            $('#post').live('tap',function(){
                var msg=$('#pass_msg').val();
                if(msg==''){return false;}
                else
                {
                    var my_bonus_sn=$('#mine_bonus').val();
                    $.post("/mobile/index.php?act=bonusex&op=comment&client_type=ajax", {
                        comment: msg,
                        bonus_sn:my_bonus_sn
                    }),function (data, status) {
                        return false;
                    };
                    $('#message').text(msg).css('color','#9b9b9b');
                    $('#bomb').remove();
                    $('body').css('overflow','');
                }
            });
            $('#pass_msg').live('textInput',function(){
                var msg_length=$(this).val().length;
                if(msg_length>20){
                    $(this).val($(this).val().substr(0,20));
                }
            })
        },
        //收起键盘
        focu:function(id){
            $(id).on('tap',function(event){
                var eventName=event.target.localName;
                if(eventName.indexOf('input')!==-1||eventName.indexOf('button')!==-1){
                    return;
                }
                $('input').blur();
            })
        }
    };
    tel.init();
    if($('.fixed_bottom').length > 0) {
        $('.maincontent').css('padding-bottom','1.33rem');
    }
});
