$(function () {
    var sTop = 0;
    var x = $("#fixedNav").offset().top;
    $(window).scroll(function () {
        sTop = $(this).scrollTop();
        if (sTop >= 1) {
            $("#fixedNav").addClass("fixedNav");
        } else {
            $("#fixedNav").removeClass("fixedNav");
        }
    });
});

$(function () {
    $('.button span').html((i, html) => {
        html = html.replace(/\s+/g, ' ');
        return '<i>' + $.trim(html).split('').join('</i><i>') + '</i>';
    });
    document.querySelectorAll(".button2 span").forEach(link => {
        link.innerHTML = link.textContent.replace(/\s+/g, ' ')
            .split("")
            .map((letter, i) => `<i style="transition-delay:${i / 15}s;" data-text="${letter}">${letter}</i>`)
            .join("");
    });
});

//导航
$(function () {
    $('.navico').click(function(){
        $(".menu").toggleClass("on");
        $(this).toggleClass("on");
    });
    $('.menu>ul > li').each(function () {
		if($(this).find('.sec').children().length>0){
            $(this).addClass('dow');
            $(this).children('a').append("<em></em>");
        }
    });
    $('.menu>ul > li .sec >li').each(function () {
		if($(this).find('ul').children().length>0){
            $(this).addClass('dow');
        }
    });
    $('.menu>ul > li.dow').click(function(){
        $(this).children('.sec').slideToggle(300);
    });
    $('.menu>ul > li .sec li').click(function(){
        $(this).children('ul').slideToggle(300);
    });
    $(document).on('click', function(event) {  
        // 检查点击事件的目标是否不是.menu或其子元素  
        if (!$(event.target).closest('.menu').length && !$(event.target).closest('.navico').length) {  
            $('.menu').removeClass("on"); 
            $(".navico").removeClass("on");
        }
    });

    $(window).scroll(function() {  
        if ($(this).scrollTop() > 1) {  
            $('.menu').removeClass("on"); 
            $(".navico").removeClass("on");
        }
    });
    
    //search
    $('.submitBtn').on('click', function () {
        var keywords = $('#keywords').val();
        if (keywords == "") {
          alert('关键字不能为空！')
          $("#keywords").focus();
          return false;
        }else{
            window.open('search_' +  keywords  + '.html','_self')
        }
    })
    
})

$(function () {
    $('.businessLicense').click(function () {
        $(".businessLicenseBox").fadeIn(400);
    });
    $('.businessLicenseClose').click(function () {
        $(".businessLicenseBox").fadeOut(400);
    });
});

//提交
$(function () {
    $('.submitform').on('click', function () {
        var kcontent = $('.xqcontent').val();
        var kuser = $('.username').val();
        var kphone = $('.usertel').val();
        // var kadd = $('.useradd').val();
        var kcode = $('.codeText').val().trim();
        // var kemail = $('.useremail').val();
        var reg = /^1[3|4|5|6|7|8|9]\d{9}$/;
        if (kuser == "") {
            alert('姓名不能为空！')
            $(".username").focus();
            return false;
        }
        if (kphone == "") {
            alert('号码不能为空！')
            $(".usertel").focus();
            return false;
        }
        if(!reg.test(kphone)) {
            alert('号码填写有误！')
            $(".usertel").focus();
            return false;
        }
        if (kcontent == "" || kcontent.length < 10) {
            alert('留言内容必须大于10字！')
            $(".xqcontent").focus();
            return false;
        }
    
        if (kcode == "") {
            alert('验证码不能为空')
            $(".codeText").focus();
            return false;
        }
    
        $.post('/api/message.ashx?action=add', {
            "kcid": 1,
            "kcontent":  '留言：' + kcontent ,
            "kuser": kuser,
            "kphone": kphone,
            "kcode": kcode
        }, function (res) {
            if (res == 1) {
                alert("留言提交成功");
                $('.xqcontent').val('');
                $('.username').val('');
                $('.usertel').val('');
                $('.usermail').val('');
                $('.codeText').val('');
            }else {
                alert("留言提交失败")
            }
        })
    })
    //验证码刷新B
    $(".imgCode").click(function () {
        $(".imgCode")[0].src = '/api/yanzhengma.aspx?action=code' + Math.random()
    });
})
