$(document).ready(function(){
    window.onbeforeunload = function () {
        if(window.scrollTo) window.scrollTo(0,0);
    };
    if(history && history.scrollRestoration) history.scrollRestoration = "manual";

    
    $(".dropdown.dropdown-mega").hover(
        function(){
            $(this).addClass("hovering")
        },
        function(){
            $(this).removeClass("hovering")

        }
    )
    if ($('#header').length > 0) {
         // header navbar
         $(window).scroll(function(){
             var position = $(window).scrollTop();
             if (position > 10) {
                 $("#header").addClass("changing-header");
             }else if($(".dropdown.dropdown-mega").hasClass("hovering")){
                 $("#header").addClass("changing-header");
             }
             else{
                 if ($(".header-search-wrapper").hasClass("hidden")) {
                    $("#header").removeClass("changing-header");
                }
             }
         })
     
         if ($(window).width() > 1024) {
             $("#header").hover(
                 function(){
                     if (!$(this).hasClass("changing-header")) {
                         $(this).addClass("changing-header");
                     }
                 },
                 function(){
                     var position = $(window).scrollTop();
                     if (position <= 50 && $(".header-search-wrapper").hasClass("hidden")) {
                         $(this).removeClass('changing-header')
                     }
                     
                 }
             );
         }
         
         $("#menuToggle [type=checkbox]").click(function(){
             if ($(this).is(":checked")) {
                 $(".hamburger-focus").addClass("open")
             }else{
                 $(".hamburger-focus").removeClass("open");
             }
         })

         $(".hamburger-focus").click(function(){
             $("#menuToggle [type=checkbox]").trigger("click");
         })
     
         $(".hamburger-dropdown-link").click(function(){
             if ($(this).siblings(".humburger-dropdown-menu").hasClass("show")) {
                 $(this).siblings(".humburger-dropdown-menu").removeClass("show");
                  $(this).siblings(".humburger-dropdown-menu").slideUp({
                      duration: 500, 
                     easing: "easeInOutQuart" 
                  });
             }else{
                 $(this).siblings(".humburger-dropdown-menu").addClass("show")
                  $(this).siblings(".humburger-dropdown-menu").slideDown({
                      duration: 500, 
                     easing: "easeInOutQuart" 
                  });
             }
         })
     
         $(".hamburger-dropdown-link-lv2").click(function(){
             
             if ($(this).next().hasClass("show")) {
                 $(this).next().removeClass("show");
                  $(this).next().slideUp({
                      duration: 800, 
                     easing: "easeInOutQuart" 
                  });
             }else{
                 $(this).next().addClass("show")
                  $(this).next().slideDown({
                      duration: 800, 
                     easing: "easeInOutQuart" 
                  });
             }
         })

        //  1025以上大螢幕header search bar
         $("#search-btn").click(function(){
             $(".header-search-wrapper").removeClass("hidden");
             setTimeout(function(){
                 $(".header-search-wrapper input").focus();
             },80)
         })
         $(".header-search-wrapper .close-btn").click(function(){
             $(".header-search-wrapper").addClass("hidden");
         })
         $(".header-search-wrapper .search-btn").click(function(){
            var e = $.Event("keypress");
            e.keyCode = 13; // # Some key code value
            $('.header-search-wrapper input').trigger(e);
         })
          $('.header-search-wrapper input').keypress(function(e){
                if (e.keyCode == '13') {
                    if ($(this).val() == "" || $(this).val() == undefined) {
                        (e.preventDefault) ? e.preventDefault(): e.returnValue = false;
                        return false
                    }else {
                        document.location.href = "search_result.php?key_word="+$(this).val();
                    }
                }
            });

        // 手機板search 
         $(".big-search-header .search-btn").click(function(e){
             if ($(this).siblings("input").val() == "" || $(this).siblings("input").val() == undefined) {
                 (e.preventDefault) ? e.preventDefault(): e.returnValue = false;
                return false
            }else {
                 document.location.href = "search_result.php?key_word="+$(this).prev().val();
                
             }
         })

         $("#lang-mobile-trigger").click(function(){
             $("#lang-mobile").trigger("focus");
         })

        $("#lang-mobile").on('click change',function(){
            let url="../"+getfilename();
            if( getfilename("urlParam")!='' ) url+=getfilename("urlParam");

            if( url.match(/news/) ) url="../news.php";
            if( $(this).val()=='中' ) location.href=url;
        })


         
    }

    if ($(".checkthisform").length > 0) {
        $('.colorholder').click(function(){
            $('.havePlace',this).focus();
        })
        
        $('.haveplace').on('input',function(){
            var len = ($(this).val()).length;
            if (len)
                $(this).next('label').hide();
            else
                $(this).next('label').show();
        })
        
        $("input").focus(function(){
            // $(this).closest(".form-group").find(".alert-text").removeClass("show")
            $(this).closest(".form-group").find(".alert-text").removeClass("show")
            // if($(this).is(":focus")){
            //     $(this).closest(".form-group").find(".alert-text").removeClass("show")

            // }
        })

        // 選擇其他顯示輸入框
        $("select").on('change',function(){
            $(this).closest(".form-group").find(".alert-text").removeClass("show")
            if ($(this).val() == "other") {
                $(this).parent().find(".reveal-input").removeClass("hidden");
            }else{
                $(this).parent().find(".reveal-input").addClass("hidden");
            }
        })
        $(".reveal-trigger").click(function(){
            if ($(this).prop("checked")) {
                $(this).parent().next(".reveal-input").removeClass("hidden");
            }else{
                $(this).parent().next(".reveal-input").addClass("hidden")
            }
        })
        
        $(".submit-btn").click(function(e){
            e.preventDefault();
            var sum_arr = [];
            var this_form = Boolean($(this).parents('form'))? $(this).parents('form'): $('#'+$(this).attr('form'));

            this_form.find("[req=Y]").each(function() {
                    var tooltips = $(this).attr("data-tooltip");
                    var title = Boolean(tooltips) ? $.trim($(this).attr("data-tooltip")) : $.trim($(this).attr("title"));

                    if ($(this).val() == "" || $(this).val() == null ) {
                        $(this).closest(".warning-box").find(".alert-text").addClass("show");
                        
                        sum_arr.push(title);
                    }
                    else if ((this.className).indexOf("check-need") >= 0){
                        if ($("[check=need]:checked").val() == null || $("[check=need]:checked").val() == "") {
                            $(this).closest(".warning-box").find(".alert-text").addClass("show");

                            sum_arr.push(title);
                        }
                    }
                    else if ((this.className).indexOf("check-know") >= 0){
                        if ($("[check=know]:checked").val() == null || $("[check=know]:checked").val() == "") {
                            $(this).closest(".warning-box").find(".alert-text").addClass("show");

                            sum_arr.push(title);
                        }
                    }
                    else if ((this.id).indexOf("privacy") >= 0){
                        if ($("#privacy:checked").val() == null || $("#privacy:checked").val() == "") {
                            $(this).closest(".warning-box").find(".alert-text").addClass("show");

                            sum_arr.push(title);
                        }
                    }
                    
                     else {
                        if ((this.id).indexOf("email") >= 0) {
                            if (validateEmail($(this).val()) == false) {
                                 $(this).closest(".warning-box").find(".alert-text").addClass("show");
                                 sum_arr.push(title);
                            }
                        }
                    }
                })

                if($("#password1").val()!== $("#password2").val() && $("#password1").val()!=="" && $("#password2").val()!==""){     
                    $("#password2").siblings(".alert-text").html("Your two input passwords are inconsistent").addClass("show");
                    
                    sum_arr.push("Your two input passwords are inconsistent");
                }

            if(sum_arr.length > 0){
              
                $('html, body').animate({
                    scrollTop: $('.alert-text.show:first').offset().top-200
                }, 500);
                // (e.preventDefault) ? e.preventDefault(): e.returnValue = false;
                // return false
            } else {
                if ((location.pathname).indexOf("signup") >= 0) {
                    $("#alert-window").modal("show");
                    $("#alert-window [type=submit]").click(function(){
                        $("#singupForm").submit();
                    })
                }else if ((location.pathname).indexOf("cart") >= 0){
                    $("#alert-window").modal("show");
                    $("#alert-window [type=submit]").click(function(){
                        this_form.submit();
                    })
                }else if ((location.pathname).indexOf("contact") >= 0){
                    this_form.submit();
                }else if ((location.pathname).indexOf("login") >= 0){
                    this_form.submit();
                }else if ((location.pathname).indexOf("forgot") >= 0){
                    this_form.submit();
                }
            }

        }) 
    }
    
    if (((location.pathname).indexOf("index.") >= 0  || (location.pathname).slice(-1) == "/")) {
        // hero carousel animation
        $("#slider").on("change.owl.carousel", function () {
            // $(".hero-imgbox .image").on('animationend webkitAnimationEnd', function() { 
            //     setTimeout(function(){
            //         alert('end');
    
            //     },0)
            // });
            
            $(".hero-imgbox .image").css("animation", "none");
            window.requestAnimationFrame(function () {
                $(".hero-imgbox .image").css("animation", "");
            });
           
            
        });
        
        if ($(window).width() < 430) {
            setTimeout(function(){
                $('#slider video').trigger('touchstart');
            },0)


            $('#slider video').on('touchstart', function() {
                // Change is here -------^

                $(this)[0].play();


            });
        }
        
       
    }
    if ((location.pathname).indexOf("product") >= 0) {
        // 下載提示登入會員
        $(".download-btn").click(function(){
            $("#member-check-modal").modal("show");
            if( $(this).attr("href")=="javascript:void(0)" ) return false;
        })
        $('#member-check-modal').on('show.bs.modal', function () {
			$('.modal-pdf').css('z-index', 1039);
		});
		$('#member-check-modal').on('hidden.bs.modal', function () {
			$('.modal-pdf').css('z-index', 1041);
		});
    }
    if ((location.pathname).indexOf("product_a_support") >= 0 ||
        (location.pathname).indexOf("product_a_rail") >= 0 ||
        (location.pathname).indexOf("product_a_table") >= 0 ||
        (location.pathname).indexOf("product_a_ballscrew") >= 0 ||
        (location.pathname).indexOf("product_b_table") >= 0 ||
        (location.pathname).indexOf("product_b_actuator") >= 0 || 
        (location.pathname).indexOf("product_b_block") >= 0 ) {
        // 選大分類
        $(document).on('click','.type-btn',function(){
            if(!$(this).hasClass("chosen")){
                $(this).addClass("chosen");
                $(this).siblings().removeClass("chosen");
            }
            
            var type_name = $(this).attr("types");
            $("#filter-wrapper").find("[types]").each(function(i,n){
                if (type_name !== $(this).attr("types")) {
                    $(this).addClass("hidden");
                }else{
                    $(this).removeClass("hidden")
                }
            })
            $(".filter-result[types]").each(function(i,n){
                if (type_name !== $(this).attr("types")) {
                    $(this).addClass("hidden");
                }else{
                    $(this).removeClass("hidden")
                }
            })

            // 隱藏全部商品 顯示相關推薦
            $("#all-products").fadeOut().addClass("hidden");
            $(".relate-product-carousel").fadeIn().removeClass("hidden");
            $(".desc-box").fadeIn().removeClass("hidden");
            $(".desc-img-box").fadeIn().removeClass("hidden");

            // 顯示篩選功能區
            $("#filter-wrapper").fadeIn().removeClass("hidden");
            $("#filter-wrapper").find("[types="+type_name+"]").removeClass("hidden");

            // 顯示對應篩選顯示區
            $(".filter-result[types="+type_name+"]").fadeIn().removeClass("hidden");

            // 每次點選分類時 產品型號選擇重置為預設第一個
            // $(".filter-item-content").find("[pcode]:first").trigger("click");
            
            
            
        })
        // 篩選區選擇後文字換色效果
        $(".filter-item-content li a").click(function(){
            if(!$(this).hasClass("chosen")){
                $(this).addClass("chosen");
                $(this).parent().siblings().children("a").removeClass("chosen");
            }
        })


        // 點選型號 換型號代表圖片&敘述字
        $(document).on('click','#filter-wrapper [pcode]',function(){
            var type_name = $(this).parents("[types]").attr("types");
            var pcode_number = $(this).attr("pcode");
            $(this).parents("[types]").find(".represent-text").text(pcode_number);
            
            var filter = $(".filter-result[types="+type_name+"]");
            // 換底下的商品
            filter.find("[pcode]").each(function(i,n){
                if($(this).attr("pcode") !== pcode_number){
                    $(this).addClass("hidden");
                }else{
                    $(this).removeClass("hidden");
                }
            })
            $(this).parents("[types]").find("a.represent").attr("href","assets/img/products_R/"+pcode_number+".png");
                $(this).parents("[types]").find("a.represent .image").attr("style","background-image: url('assets/img/products_R/"+pcode_number+".png');");
            

            

        })
       

        $(document).on("click",".result-item .cart-btn",function(event) {
            /* $(".result-item .cart-btn").click(function(){ */
            let product_infos=$(this).attr('product_infos');
            let option=0;
            var amount = $("body").find(".in_cart").length;
            if (!$(this).hasClass("chosen")) {     
                $(this).addClass("chosen");
                $(this).parents(".result-item-box").addClass("chosen");
                $(this).addClass("in_cart"); //找被選過的幫他加.in_cart
                $("header .header-icon-links .cart-btn").addClass("cart-in-btn").addClass("shake"); //電腦版
                $(".hamburger").addClass("cart-in-btn"); //漢堡外層
                $(".hamburger-top-box .cart-btn").addClass("cart-in-btn"); //漢堡裡面
                option=1;
                
            }else{
                $(this).removeClass("chosen");
                $(this).parents(".result-item-box").removeClass("chosen");
                $(this).removeClass("in_cart"); 
                option=2;
            }

            var event_arr = [];
            //以下success必填, 其餘選填
            event_arr["success"] = function(data){
                /* console.log(data) */
                // 有多少放進估價車的項目
                amount = data.amount; //重新計算有多少個.in_cart
                if (amount > 0) { //如果有人有有in_cart
                    $(".cart-in-icon").attr('data-content', amount); //替換.cart-in-btn::after裡的content
                }else{ //沒有人有in_cart就移除綠色點點
                    $("header .header-icon-links .cart-btn").removeClass("cart-in-btn");
                    $(".hamburger").removeClass("cart-in-btn"); //漢堡外層
                    $(".hamburger-top-box .cart-btn").removeClass("cart-in-btn"); //漢堡裡面
                }
                // navbar詢價表單提示小動畫定期重置
                setTimeout(function(){
                    $("header .header-icon-links .cart-btn").removeClass("shake");
                },200)
            }

            ajax_pub_adv("ajax/setCart.php", {'product_infos':product_infos,'option':option}, event_arr ,{"async":true,"timeout":8000})
        })
        
        $(".filter-extruder").click(function(){
            var content = $(this).parent().find(".filter-item-content");
            content.slideToggle();
           

            if (content.find("a.show").length > 0) {
                var $el = $(this);
                setTimeout(function(){
                    $el.parent().find(".show-choose-box").toggleClass("show");
                }, 0)
            }
        })
        $(document).on("click",".filter-item-content > li > a",function(event) {
        /* $(".filter-item-content > li > a").click(function(){ */
            var chosen_name = $(this).html();
            $(this).addClass("show");
            $(this).parents(".filter-item").find(".filter-extruder").trigger("click");
            var $el = $(this);


            setTimeout(function(){
                $el.closest(".filter-item").find(".show-choose-box").addClass("show");
            }, 0)
            
            // $(this).parents(".filter-item").find(".show-choose-box").addClass("show");
            $(this).parents(".filter-item").find(".show-choose-box>li>a").html(chosen_name);
        })

        

    }

    if((location.pathname).indexOf("product_a_miniature") >= 0 ){
        let product_infos=$(this).attr('product_infos');
        let option=0;
        var amount = $("body").find(".in_cart").length;

        var event_arr = [];
        //以下success必填, 其餘選填
        event_arr["success"] = function(data){
            /* console.log(data) */
            // 有多少放進估價車的項目
            amount = data.amount; //重新計算有多少個.in_cart
            if (amount > 0) { //如果有人有有in_cart
                $(".cart-in-icon").attr('data-content', amount); //替換.cart-in-btn::after裡的content
            }else{ //沒有人有in_cart就移除綠色點點
                $("header .header-icon-links .cart-btn").removeClass("cart-in-btn");
                $(".hamburger").removeClass("cart-in-btn"); //漢堡外層
                $(".hamburger-top-box .cart-btn").removeClass("cart-in-btn"); //漢堡裡面
            }
            // navbar詢價表單提示小動畫定期重置
            setTimeout(function(){
                $("header .header-icon-links .cart-btn").removeClass("shake");
            },200)
        }

        $(".result-item .cart-btn").click(function(){
            product_infos=$(this).attr('product_infos');

            if (!$(this).hasClass("chosen")) {     
                $(this).addClass("chosen").html("Cancel the inquiry from cart");
                $(this).parents(".result-item-box").addClass("chosen");
                $(this).addClass("in_cart"); //找被選過的幫他加.in_cart
                $("header .header-icon-links .cart-btn").addClass("cart-in-btn").addClass("shake"); //電腦版
                $(".hamburger").addClass("cart-in-btn"); //漢堡外層
                $(".hamburger-top-box .cart-btn").addClass("cart-in-btn"); //漢堡裡面
                option=1;
                
            }else{
                $(this).removeClass("chosen").html("Add to the inquiry cart");
                $(this).parents(".result-item-box").removeClass("chosen");
                $(this).removeClass("in_cart"); 
                option=2;
            }

            ajax_pub_adv("ajax/setCart.php", {'product_infos':product_infos,'option':option}, event_arr ,{"async":true,"timeout":8000})
        })
    }

    
    if ((location.pathname).indexOf("product_a_coupling") >= 0) {
        let product_infos="";
        const option=1;
        
        $(document).on('click', 'a.dash-item[href^="#"]', function (event) {
            event.preventDefault();

            $('html, body').animate({
                scrollTop: $($.attr(this, 'href')).offset().top
            }, 500);
        });
        
        stickybits('.sticky-select-wrapper', {stickyBitStickyOffset: 80,useStickyClasses: false,useStickyClasses: true});
        
        var event_arr_choose = [];
        //以下success必填, 其餘選填
        event_arr_choose["success"] = function(data){
            /* console.log(data); */

            $('#img_'+data.set_id).css('background-image', 'url(' + data.coupling_infos.small_img + ')');
            $('#desc_'+data.set_id).attr('href',data.coupling_infos.desc_img);
            $('#twod_'+data.set_id).attr({'href': data.coupling_infos.files_2d, 'target': data.coupling_infos.files_2d_attr});
            $('#threed_'+data.set_id).attr('d_href',data.coupling_infos.url_3d);
            $('#threed_'+data.set_id).attr({'href': data.coupling_infos.url, 'data-toggle': data.coupling_infos.url_attr});
        }
     
        $(".dropdown-menu .spec-btn").click(function(){
            var spec = $(this).text();
            $(this).addClass("chosen"); //當次被選中
            $(this).siblings().removeClass("chosen");
            $(this).parent().siblings(".dropdown-toggle").html(spec);
            $(this).parent().parent().find('a[class="cart-btn"]').attr('product_infos',$(this).attr('product_infos'));

            ajax_pub_adv("ajax/getCoupling.php", {'product_infos':$(this).attr('product_infos'),'set_id':$(this).attr('set_id')}, event_arr_choose ,{"async":true,"timeout":8000});
        })

        var event_arr = [];
        //以下success必填, 其餘選填
        event_arr["success"] = function(data){
            /* console.log(data) */
            // 有多少放進估價車的項目
            amount = data.amount; //重新計算有多少個.in_cart
            if (amount > 0) { //如果有人有有in_cart
                $(".cart-in-icon").attr('data-content', amount); //替換.cart-in-btn::after裡的content
            }else{ //沒有人有in_cart就移除綠色點點
                $("header .header-icon-links .cart-btn").removeClass("cart-in-btn");
                $(".hamburger").removeClass("cart-in-btn"); //漢堡外層
                $(".hamburger-top-box .cart-btn").removeClass("cart-in-btn"); //漢堡裡面
            }
            // navbar詢價表單提示小動畫定期重置
            setTimeout(function(){
                $("header .header-icon-links .cart-btn").removeClass("shake");
            },200)
        }


        // click放入詢價車按鈕時
        $(".single-p-wrapper .cart-btn").click(function(){
            product_infos=$(this).attr('product_infos');

            var amount = $("body").find(".in_cart").length;
            var chosen_btn = $(this).parent().find(".spec-btn.chosen");
            if (chosen_btn.length > 0) {
                $(this).parent().children(".dropdown-menu").find(".spec-btn.chosen").addClass("in_cart"); //找被選過的幫他加.in_cart
                $(this).parents(".spec-bar").addClass("chosen"); //被選過的該項商品規格條+chosen變色
                $("header .header-icon-links .cart-btn").addClass("cart-in-btn").addClass("shake");
                $(".hamburger").addClass("cart-in-btn");
                $(".hamburger-top-box .cart-btn").addClass("cart-in-btn");
            }

            ajax_pub_adv("ajax/setCart.php", {'product_infos':product_infos,'option':option}, event_arr ,{"async":true,"timeout":8000})
        })

        if ($(window).width() > 1024) {     
            // hover function包含.mouseenter( handlerIn ).mouseleave( handlerOut )兩個
            $(".single-p-wrapper").hover(function(){
                $(this).find(".hover-show").addClass("show");
            },function(){
                $(this).find(".hover-show").removeClass("show");
            })

            $(".hover-item").hover(function(){
                var hint_type = $(this).attr("hint");
                if (hint_type == 'info') {
                    $(this).children("img").attr("src","assets/img/icons/info_change.svg");
                }else if(hint_type == '2d') {
                    $(this).children("img").attr("src","assets/img/icons/pdf_green_change.svg");
                }else {
                    $(this).children("img").attr("src","assets/img/icons/cad_green_change.svg");
                }

                $(this).children(".mouse-hint").addClass("show");
            },function(){
                var hint_type = $(this).attr("hint");
                if (hint_type == 'info') {
                    $(this).children("img").attr("src","assets/img/icons/info.svg");
                }else if(hint_type == '2d') {
                    $(this).children("img").attr("src","assets/img/icons/pdf_green.svg");
                }else {
                    $(this).children("img").attr("src","assets/img/icons/cad_green.svg");
                }

                $(this).children(".mouse-hint").removeClass("show");
            })
        }else {
            $(".single-p-wrapper").find(".hover-show").addClass("show");
        }


        
    }
    
    if ((location.pathname).indexOf("cart") >= 0) {
        var event_arr = [];
        //以下success必填, 其餘選填
        event_arr["success"] = function(data){
            /* console.log(data) */
        }

        // 數量選擇  
        $("[add=amount]").click(function () { 
            var base = parseInt($(this).siblings("[number=number]").attr("base"));
            var product_infos = $(this).siblings("[number=number]").attr("product_infos");
            const option=3;

            base = base + 1;
            $(this).siblings("[number=number]").text(base);
            $(this).siblings("[number=number]").attr("base",base);

            ajax_pub_adv("ajax/setCart.php", {'product_infos':product_infos,'option':option,'base':base}, event_arr ,{"async":true,"timeout":8000})
        })
        $("[sub=amount]").click(function () { 
            var base = parseInt($(this).siblings("[number=number]").attr("base"));
            var product_infos = $(this).siblings("[number=number]").attr("product_infos");
            const option=3;

            if (base > 1) {
                base = base - 1;
                $(this).siblings("[number=number]").text(base);
                $(this).siblings("[number=number]").attr("base",base);
            }else {
                base -= 0
            }

            ajax_pub_adv("ajax/setCart.php", {'product_infos':product_infos,'option':option,'base':base}, event_arr ,{"async":true,"timeout":8000})
        })

        // 刪除商品
        $(document).on('click','.cart-cancel-btn',function(){
            $(this).parents(".cart-item").fadeOut(function(){
                $(this).remove();
            });

            let product_infos=$(this).attr('product_infos');
            const option=2;

            ajax_pub_adv("ajax/setCart.php", {'product_infos':product_infos,'option':option}, event_arr ,{"async":true,"timeout":8000})
        })

        // $(".submit-btn").click(function(e){
        //     e.preventDefault();
        //     var sum_arr = [];
        //     var this_form = Boolean($(this).parents('form'))? $(this).parents('form'): $('#'+$(this).attr('form'));

        //     this_form.find("[req=Y]").each(function() {
        //             var tooltips = $(this).attr("data-tooltip");
        //             var title = Boolean(tooltips) ? $.trim($(this).attr("data-tooltip")) : $.trim($(this).attr("title"));

        //             if ($(this).val() == "" || $(this).val() == null ) {
        //                 $(this).closest(".warning-box").find(".alert-text").addClass("show");
                        
        //                 sum_arr.push(title);
        //             }
        //             else if ((this.className).indexOf("check-need") >= 0){
        //                 if ($("[check=need]:checked").val() == null || $("[check=need]:checked").val() == "") {
        //                     $(this).closest(".warning-box").find(".alert-text").addClass("show");

        //                     sum_arr.push(title);
        //                 }
        //             }
        //             else if ((this.className).indexOf("check-know") >= 0){
        //                 if ($("[check=know]:checked").val() == null || $("[check=know]:checked").val() == "") {
        //                     $(this).closest(".warning-box").find(".alert-text").addClass("show");

        //                     sum_arr.push(title);
        //                 }
        //             }
        //             else if ((this.id).indexOf("privacy") >= 0){
        //                 if ($("#privacy:checked").val() == null || $("#privacy:checked").val() == "") {
        //                     $(this).closest(".warning-box").find(".alert-text").addClass("show");

        //                     sum_arr.push(title);
        //                 }
        //             }
                    
        //              else {
        //                 if ((this.id).indexOf("email") >= 0) {
        //                     if (validateEmail($(this).val()) == false) {
        //                          $(this).closest(".warning-box").find(".alert-text").addClass("show");
        //                          sum_arr.push(title);
        //                     }
        //                 }
        //             }
        //         })

        //         if($("#password1").val()!== $("#password2").val() && $("#password1").val()!=="" && $("#password2").val()!==""){     
        //             $("#password2").siblings(".alert-text").html("兩次輸入的密碼不同").addClass("show");
                    
        //             sum_arr.push("兩次輸入的密碼不同");
        //         }

        //     if(sum_arr.length > 0){
        //         $('html, body').animate({
        //             scrollTop: $('.alert-text.show:first').offset().top-200
        //         }, 500);
        //     } else {
        //         $("#alert-window").modal("show");
        //         $("#alert-window [type=submit]").click(function(){
        //             this_form.submit();
        //         })
        //     }

        // }) 
    }

    // if ((location.pathname).indexOf("contact") >= 0) {
    //     $(".submit-btn").click(function(e){
    //         e.preventDefault();
    //         var sum_arr = [];
    //         var this_form = Boolean($(this).parents('form'))? $(this).parents('form'): $('#'+$(this).attr('form'));

    //         this_form.find("[req=Y]").each(function() {
    //                 var tooltips = $(this).attr("data-tooltip");
    //                 var title = Boolean(tooltips) ? $.trim($(this).attr("data-tooltip")) : $.trim($(this).attr("title"));

    //                 if ($(this).val() == "" || $(this).val() == null ) {
    //                     $(this).closest(".warning-box").find(".alert-text").addClass("show");
                        
    //                     sum_arr.push(title);
    //                 }
    //                 else if ((this.className).indexOf("check-need") >= 0){
    //                     if ($("[check=need]:checked").val() == null || $("[check=need]:checked").val() == "") {
    //                         $(this).closest(".warning-box").find(".alert-text").addClass("show");

    //                         sum_arr.push(title);
    //                     }
    //                 }
    //                 else if ((this.className).indexOf("check-know") >= 0){
    //                     if ($("[check=know]:checked").val() == null || $("[check=know]:checked").val() == "") {
    //                         $(this).closest(".warning-box").find(".alert-text").addClass("show");

    //                         sum_arr.push(title);
    //                     }
    //                 }
    //                 else if ((this.id).indexOf("privacy") >= 0){
    //                     if ($("#privacy:checked").val() == null || $("#privacy:checked").val() == "") {
    //                         $(this).closest(".warning-box").find(".alert-text").addClass("show");

    //                         sum_arr.push(title);
    //                     }
    //                 }
                    
    //                  else {
    //                     if ((this.id).indexOf("email") >= 0) {
    //                         if (validateEmail($(this).val()) == false) {
    //                              $(this).closest(".warning-box").find(".alert-text").addClass("show");
    //                              sum_arr.push(title);
    //                         }
    //                     }
    //                 }
    //             })

    //             if($("#password1").val()!== $("#password2").val() && $("#password1").val()!=="" && $("#password2").val()!==""){     
    //                 $("#password2").siblings(".alert-text").html("兩次輸入的密碼不同").addClass("show");
                    
    //                 sum_arr.push("兩次輸入的密碼不同");
    //             }

    //         if(sum_arr.length > 0){
              
    //             $('html, body').animate({
    //                 scrollTop: $('.alert-text.show:first').offset().top-200
    //             }, 500);
    //             // (e.preventDefault) ? e.preventDefault(): e.returnValue = false;
    //             // return false
    //         } else {


    //                 this_form.submit();

    //         }

    //     }) 
    // }

    if ((location.pathname).indexOf("signup") >= 0) {

        // $(".submit-btn").click(function(e){
        //     e.preventDefault();
        //     var sum_arr = [];
        //     var this_form = Boolean($(this).parents('form'))? $(this).parents('form'): $('#'+$(this).attr('form'));

        //     this_form.find("[req=Y]").each(function() {
        //             var tooltips = $(this).attr("data-tooltip");
        //             var title = Boolean(tooltips) ? $.trim($(this).attr("data-tooltip")) : $.trim($(this).attr("title"));

        //             if ($(this).val() == "" || $(this).val() == null ) {
        //                 $(this).closest(".warning-box").find(".alert-text").addClass("show");
                        
        //                 sum_arr.push(title);
        //             }
        //             else if ((this.className).indexOf("check-need") >= 0){
        //                 if ($("[check=need]:checked").val() == null || $("[check=need]:checked").val() == "") {
        //                     $(this).closest(".warning-box").find(".alert-text").addClass("show");

        //                     sum_arr.push(title);
        //                 }
        //             }
        //             else if ((this.className).indexOf("check-know") >= 0){
        //                 if ($("[check=know]:checked").val() == null || $("[check=know]:checked").val() == "") {
        //                     $(this).closest(".warning-box").find(".alert-text").addClass("show");

        //                     sum_arr.push(title);
        //                 }
        //             }
        //             else if ((this.id).indexOf("privacy") >= 0){
        //                 if ($("#privacy:checked").val() == null || $("#privacy:checked").val() == "") {
        //                     $(this).closest(".warning-box").find(".alert-text").addClass("show");

        //                     sum_arr.push(title);
        //                 }
        //             }
                    
        //              else {
        //                 if ((this.id).indexOf("email") >= 0) {
        //                     if (validateEmail($(this).val()) == false) {
        //                          $(this).closest(".warning-box").find(".alert-text").addClass("show");
        //                          sum_arr.push(title);
        //                     }
        //                 }
        //             }
        //         })

        //         if($("#password1").val()!== $("#password2").val() && $("#password1").val()!=="" && $("#password2").val()!==""){     
        //             $("#password2").siblings(".alert-text").html("兩次輸入的密碼不同").addClass("show");
                    
        //             sum_arr.push("兩次輸入的密碼不同");
        //         }

        //     if(sum_arr.length > 0){
              
        //         $('html, body').animate({
        //             scrollTop: $('.alert-text.show:first').offset().top-200
        //         }, 500);
        //         // (e.preventDefault) ? e.preventDefault(): e.returnValue = false;
        //         // return false
        //     } else {
        //         $("#alert-window").modal("show");
        //         $("#alert-window [type=submit]").click(function(){
        //             document.location.href = "login.php"
        //         })
        //     }

        // }) 

    }

    // if ((location.pathname).indexOf("login") >= 0) {
    //     $(".submit-btn").click(function(e){
    //         e.preventDefault();
    //         var sum_arr = [];
    //         var this_form = Boolean($(this).parents('form'))? $(this).parents('form'): $('#'+$(this).attr('form'));

    //         this_form.find("[req=Y]").each(function() {
    //                 var tooltips = $(this).attr("data-tooltip");
    //                 var title = Boolean(tooltips) ? $.trim($(this).attr("data-tooltip")) : $.trim($(this).attr("title"));

    //                 if ($(this).val() == "" || $(this).val() == null ) {
    //                     $(this).closest(".warning-box").find(".alert-text").addClass("show");
                        
    //                     sum_arr.push(title);
    //                 }
    //                 else if ((this.className).indexOf("check-need") >= 0){
    //                     if ($("[check=need]:checked").val() == null || $("[check=need]:checked").val() == "") {
    //                         $(this).closest(".warning-box").find(".alert-text").addClass("show");

    //                         sum_arr.push(title);
    //                     }
    //                 }
    //                 else if ((this.className).indexOf("check-know") >= 0){
    //                     if ($("[check=know]:checked").val() == null || $("[check=know]:checked").val() == "") {
    //                         $(this).closest(".warning-box").find(".alert-text").addClass("show");

    //                         sum_arr.push(title);
    //                     }
    //                 }
    //                 else if ((this.id).indexOf("privacy") >= 0){
    //                     if ($("#privacy:checked").val() == null || $("#privacy:checked").val() == "") {
    //                         $(this).closest(".warning-box").find(".alert-text").addClass("show");

    //                         sum_arr.push(title);
    //                     }
    //                 }
                    
    //                  else {
    //                     if ((this.id).indexOf("email") >= 0) {
    //                         if (validateEmail($(this).val()) == false) {
    //                              $(this).closest(".warning-box").find(".alert-text").addClass("show");
    //                              sum_arr.push(title);
    //                         }
    //                     }
    //                 }
    //             })

    //             if($("#password1").val()!== $("#password2").val() && $("#password1").val()!=="" && $("#password2").val()!==""){     
    //                 $("#password2").siblings(".alert-text").html("兩次輸入的密碼不同").addClass("show");
                    
    //                 sum_arr.push("兩次輸入的密碼不同");
    //             }

    //         if(sum_arr.length > 0){
              
    //             $('html, body').animate({
    //                 scrollTop: $('.alert-text.show:first').offset().top-200
    //             }, 500);
    //         } else {
    //             this_form.submit();
    //         }

    //     }) 
    // }

    if ((location.pathname).indexOf("member") >= 0) {
        var city_param = [];

		city_param["city_name"] = "city";
		city_param["region_name"] = "region";
		city_param["zipcode_name"] = "zipcode";
        city_param["city_default"] = "台中市";
        city_param["region_default"] = "大里區";
		city_param["callback"] = function(data){
			// console.log(data);
		};

		city_related(city_param);
    }

    if ((location.pathname).indexOf("download") >= 0) {
        stickybits('.search-section', {stickyBitStickyOffset: 80,useStickyClasses: false,useStickyClasses: true});

        stickybits('.download-category-carousel', {stickyBitStickyOffset: 152,useStickyClasses: false,useStickyClasses: true});
        
        $(document).on('click', 'a.dash-item[href^="#"]', function (event) {
            event.preventDefault();

            $('html, body').animate({
                scrollTop: $($.attr(this, 'href')).offset().top
            }, 500);
        });
        
        $(".simple-search button").click(function(){
            if ($(this).parents(".simple-search").find("input").val() == "" || $(this).parents(".simple-search").find("input").val() == null) {

            }else{
                document.location.href = "download_search.php?key_word="+$(this).parents(".simple-search").find("input").val();
            }
        })

        $(".delete-search").click(function(){
            document.location.href = "download.php";
        })

    }

    
    // $(document).on('click','.mfp-img',function(){
    //     var URL = $(this).attr("src");
    //     window.open(URL,'_blank','',''); 
    // })
    
    if ($(".fixed-link-wrapper").length > 0) {
        $(window).scroll(function(){
			var position = $(window).scrollTop();
			if( position > 150) {
				$(".fixed-link-wrapper").addClass('show');
			} else {
				$(".fixed-link-wrapper").removeClass('show');
			}
		})	
    }
    
});

