jQuery(document).ready(function($) {
    
    if ( $(".tcstope_c_pb_cp_r_images").length > 0 ){ 
    
    $.each(all_sliders, function(key, slider_num){
        console.log( '#slider_'+slider_num );
        $('#slider_'+slider_num).slick({
            dots: false,
            infinite: true,
            speed: 300,
            //adaptiveHeight: true,
            //arrows: true,
            //lazyLoad: 'ondemand',
            //slidesToShow: 1,
            //centerMode: true,
            //variableWidth: true,
            autoplaySpeed: 3000,
            //autoplay: true,
            prevArrow: $("#prev_"+slider_num),
            nextArrow: $("#next_"+slider_num),
            responsive: [
                {
                  breakpoint: 1200,
                  settings: {
                    //slidesToShow: 4,
                    //slidesToScroll: 2
                  }
                },
                {
                  breakpoint: 991,
                  settings: {
                    //slidesToShow: 3,
                    //slidesToScroll: 2
                  }
                },
                {
                  breakpoint: 600,
                  settings: {
                    //slidesToShow: 2,
                    //slidesToScroll: 2
                  }
                },
                {
                  breakpoint: 480,
                  settings: {
                    //slidesToShow: 2,
                    //slidesToScroll: 1
                  }
                }
            ]
        });
    });
    
    }
    
    if ( $(".tcstope_c_pi_all_images").length > 0 ){ 

        $('.tcstope_c_pi_all_images').slick({
            dots: false,
            infinite: true,
            speed: 300,
            //adaptiveHeight: true,
            //arrows: true,
            //lazyLoad: 'ondemand',
            slidesToShow: 4,
            //centerMode: true,
            //variableWidth: true,
            swipeToSlide: true,
            autoplaySpeed: 3000,
            //autoplay: true,
            draggable: true,
            prevArrow: $("#tcstope_c_pi_all_images_up"),
            nextArrow: $("#tcstope_c_pi_all_images_down"),
            vertical: true,
            responsive: [
                {
                  breakpoint: 1200,
                  settings: {
                    //slidesToShow: 4,
                    //slidesToScroll: 2
                  }
                },
                {
                  breakpoint: 991,
                  settings: {
                    vertical: false,
                    slidesToScroll: 1,
                    slidesToShow: 1
                  }
                }
            ]
        });
        
    }
    
    $("body").on("click",".tcstope_c_pb_cp_fr_addtocart", function(e){
        e.preventDefault();
        
        if ( !$(this).hasClass("tcstope_c_pb_cp_fr_addtocart_pressed") && !$(this).hasClass("tcstope_c_pfr_addtocart_out_of_stock") ){
            
            $(this).addClass("tcstope_c_pb_cp_fr_addtocart_pressed");
        
            var product_id =            $(this).attr("data-product-id");
            var product_title =         $(this).attr("data-product-title");
            
            $(".tcstope_added_to_cart").remove();
            
            var this_button = $(this);
            
            console.log( '<div class="tcstope_added_to_cart"><strong>"'+product_title+'"</strong> wurde deinem Warenkorb hinzugefügt. <a href="/cart/">Weiter einkaufen</a></div>' );
                    
            $.ajax({
                    type: 'POST',
                    dataType: 'json',
                    url: tcstope_main_js_array.ajaxurl,
                    data: { 
                        'action':               'tcstope_c_pb_cp_fr_addtocart', 
                        'product_id':           product_id,        
                    },
                    success: function(response){
                        $(document.body).trigger('wc_fragment_refresh');
                        this_button.removeClass("tcstope_c_pb_cp_fr_addtocart_pressed");
                        this_button.parent().append('<div class="tcstope_added_to_cart"><strong>"'+product_title+'"</strong> wurde deinem Warenkorb hinzugefügt. <a href="/cart/">Weiter einkaufen</a></div>');
                        setTimeout(function(){
                            $(".tcstope_added_to_cart").remove();
                        }, 8000);
                        
                    }
            });
            
        
        }
        
    });
    
    $("body").on("click",".tcstope_c_pfr_addtocart", function(e){
        e.preventDefault();
        
        if ( !$(this).hasClass("tcstope_c_pfr_addtocart_pressed") && !$(this).hasClass("tcstope_c_pfr_addtocart_out_of_stock") ){
            
            $(this).addClass("tcstope_c_pfr_addtocart_pressed");
        
            var product_id =            $(this).attr("data-product-id");
            var product_title =         $(this).attr("data-product-title");
            
            var this_button = $(this);
            
            $(".tcstope_added_to_cart").remove();
            
            console.log( '<div class="tcstope_added_to_cart"><strong>"'+product_title+'"</strong> wurde deinem Warenkorb hinzugefügt. <a href="/cart/">Weiter einkaufen</a></div>' );
                    
            $.ajax({
                    type: 'POST',
                    dataType: 'json',
                    url: tcstope_main_js_array.ajaxurl,
                    data: { 
                        'action':               'tcstope_c_pb_cp_fr_addtocart', 
                        'product_id':           product_id,    
                        'product_number':       $(".tcstope_c_product_footer_right_number").attr("data-number")
                    },
                    success: function(response){
                        $(document.body).trigger('wc_fragment_refresh');
                        this_button.removeClass("tcstope_c_pfr_addtocart_pressed");
                        this_button.parent().append('<div class="tcstope_added_to_cart"><strong>"'+product_title+'"</strong> wurde deinem Warenkorb hinzugefügt. <a href="/cart/">Weiter einkaufen</a></div>');
                        setTimeout(function(){
                            $(".tcstope_added_to_cart").remove();
                        }, 8000);
                        
                    }
            });
            
        
        }
        
    });
    
    setInterval(function(){
        $("body").find(".woocommerce-billing-fields").first().find("input,textarea").each(function(){
            //console.log( $(this).val() );
            if ( $(this).val() == $(this).attr("placeholder") ){
                //console.log("yes");
                $(this).val("");
            }
        });
        
        $("body").find(".woocommerce-additional-fields").first().find("input,textarea").each(function(){
            //console.log( $(this).val() );
            if ( $(this).val() == $(this).attr("placeholder") ){
                //console.log("yes");
                $(this).val("");
            }
        });
    },100);
    
    $(".tcstope_c_mapb_search").click(function(){ 
        window.scrollTo({
            top: 0,
            behavior: "smooth"
        });
        setTimeout( function(){ $(".search-icon").first().click(); }, 400 ); 
    });
    
    $(".tcstope_c_pi_ai_image").click(function(){
        $(".tcstope_c_pi_main_image").attr( "style", "background-image:url("+$(this).attr("data-src")+")" );
    });
    
    $(".tcstope_prs_idb_rating_stars").starRating({
      useFullStars: true,
      hoverColor: "#b9decd",
      disableAfterRate: false,
      activeColor: '#b9decd',
      starSize: 32,
      emptyColor: "transparent",
      starGradient: {
          start: '#b9decd',
          end: '#b9decd'
      },
      callback: function(currentRating, $el){
          //console.log($el.parent().attr("class"));
          $el.parent().attr("data-rating",currentRating);
      }
    });
    
    $(".tcstope_prs_sb_name").click(function(){
        $(".tcstope_prs_sb_name").removeClass("tcstope_prs_sb_name_active");
        $(this).addClass("tcstope_prs_sb_name_active");
    });
    
    $(".tcstope_prs_sb_button").click(function(){
        
        if ( $(this).hasClass("tcstope_prs_sb_button_pressed") ){ return; }
        $(this).addClass("tcstope_prs_sb_button_pressed");
        
        var order_id =              $(".tcstope_page_review_submit_container").attr("data-order-id");
        var order_review_key =      $(".tcstope_page_review_submit_container").attr("data-order-key");
        var products =              [];
        var custom_name =           $(".tcstope_prs_sb_name_active").attr("data-actual-val");
        
        $(".tcstope_prs_item").each(function(){
            var product_array = {
                "product_id":       $(this).attr("data-product-id"),
                "product_rating":   $(this).find(".tcstope_prs_idb_rating").attr("data-rating"),
                "product_comment":  $(this).find(".tcstope_prs_idb_comentar_text").find("textarea").val(),
            };
            products.push( product_array );
        });
        
                
        $.ajax({
                type: 'POST',
                dataType: 'json',
                url: tcstope_main_js_array.ajaxurl,
                data: { 
                    'action':               'tcstope_prs_sb_button', 
                    'order_id':             order_id,
                    'order_review_key':     order_review_key,
                    'products':             products,
                    'custom_name':          custom_name                    
                },
                success: function(response){
                    console.log(response);
                    if ( response.status === false ){
                        
                    }else{
                        
                    }
                    window.location.href = "https://diekim.de/reviews-form-thank-you/";
                    $(".tcstope_prs_sb_button").removeClass("tcstope_prs_sb_button_pressed");
                }
        });
        
    });
    
    $(".tcstope_c_pdar_raf_rh_stars").starRating({
      initialRating: $(this).attr("data-rating"),
      readOnly: true,
      useFullStars: true,
      hoverColor: "#b9decd",
      disableAfterRate: false,
      activeColor: '#b9decd',
      strokeColor: '#b9decd',
      strokeWidth: 15,
      starSize: 28,
      emptyColor: "transparent",
      starGradient: {
          start: '#b9decd',
          end: '#b9decd'
      }
    });
    
    $(".tcstope_c_pdar_raf_rh_rating").starRating({
      initialRating: $(this).attr("data-rating"),
      readOnly: true,
      useFullStars: true,
      hoverColor: "#b9decd",
      strokeColor: '#b9decd',
      disableAfterRate: false,
      activeColor: '#b9decd',
      strokeWidth: 15,
      starSize: 18,
      emptyColor: "transparent",
      starGradient: {
          start: '#b9decd',
          end: '#b9decd'
      }
    });
    
    $(".tcstope_c_pdar_raf_fb_rating").starRating({
      strokeWidth: 15,
      useFullStars: true,
      hoverColor: "#b9decd",
      disableAfterRate: false,
      activeColor: '#b9decd',
      strokeColor: '#b9decd',
      starSize: 32,
      emptyColor: "transparent",
      starGradient: {
          start: '#b9decd',
          end: '#b9decd'
      },
      callback: function(currentRating, $el){
          //console.log($el.parent().attr("class"));
          $el.parent().attr("data-rating",currentRating);
      }
    });
    
    $(".tcstope_c_pdar_raf_more_reviews").click(function(){
        $(".tcstope_c_pdar_raf_review").each(function(){
            if ( $(".tcstope_c_pdar_raf_review").hasClass("tcstope_c_pdar_raf_review_hidden") ){
                $(this).show("slow");
            }
        });
        $(this).hide();
    });
    
    
    $(".tcstope_c_pdar_raf_fb_text, .tcstope_c_pdar_raf_fb_name, .tcstope_c_pdar_raf_fb_email").on("keyup keydown keypress change blur",function(){
        $(".tcstope_c_pdar_raf_fb_submit_message").remove();
    });
    
    $(".tcstope_c_pdar_raf_fb_submit_button").click(function(){
        
        $(".tcstope_c_pdar_raf_fb_submit_message").remove();
        
        if ( $(this).hasClass("tcstope_c_pdar_raf_fb_submit_button_pressed") ){ return; }
        $(this).addClass("tcstope_c_pdar_raf_fb_submit_button_pressed");
        
        var product_id =            $(this).attr("data-product-id");
        var email =                 $(".tcstope_c_pdar_raf_fb_email").val();
        var name =                  $(".tcstope_c_pdar_raf_fb_name").val();
        var text =                  $(".tcstope_c_pdar_raf_fb_text").val();
        var rating =                $(".tcstope_c_pdar_raf_form_body").attr("data-rating");        
                
        $.ajax({
                type: 'POST',
                dataType: 'json',
                url: tcstope_main_js_array.ajaxurl,
                data: { 
                    'action':               'tcstope_c_pdar_raf_fb_submit_button', //submit product review from: single-product page -> review form
                    'product_id':           product_id,
                    'email':                email,
                    'text':                 text,
                    'name':                 name,
                    'rating':               rating                    
                },
                success: function(response){
                    console.log(response);
                    if ( response.status === false ){
                        $(".tcstope_c_pdar_raf_fb_submit").append('<div class="tcstope_c_pdar_raf_fb_submit_message tcstope_c_pdar_raf_fb_submit_message_error">'+response.message+'</div>');
                    }else{
                        $(".tcstope_c_pdar_raf_fb_submit").append('<div class="tcstope_c_pdar_raf_fb_submit_message">'+response.message+'</div>');
                        $(".tcstope_c_pdar_raf_fb_text, .tcstope_c_pdar_raf_fb_name, .tcstope_c_pdar_raf_fb_email").val("");
                    }
                    $(".tcstope_c_pdar_raf_fb_submit_button").removeClass("tcstope_c_pdar_raf_fb_submit_button_pressed");
                }
        });
        
    });
    
    $(".tcstope_c_product_footer_right_number").click(function(){
        $(".tcstope_c_product_footer_right_numbers").toggleClass("tcstope_c_product_footer_right_numbers_show");
    });
    $(".tcstope_c_product_footer_right_numbers_number").click(function(){
        $(".tcstope_c_product_footer_right_number").html("Anzahl "+$(this).html());
        $(".tcstope_c_product_footer_right_number").attr("data-number",$(this).html());
        $(".tcstope_c_product_footer_right_numbers").toggleClass("tcstope_c_product_footer_right_numbers_show");
    });
    
    if ( $(".tcstope_woocommerce_checkout").length > 0 ){
    
        $("#ship-to-different-address label").click(function(){
            setTimeout(function(){
            if ( $("#ship-to-different-address-checkbox").prop("checked") === true ){
                $("#ship-to-different-address").addClass("ship-to-different-address-checked");
            }else{
                $("#ship-to-different-address").removeClass("ship-to-different-address-checked");
            }
            },500);
        });
        
        //$(".wc_payment_methods li > label").first().addClass("tcstope_label_checked");
        
        
        setInterval(function(){
            $("body").find("label").each(function(){
                if ( $(this).parent().hasClass("wc_payment_method") ){
                    var this_label = $(this);
                    if ( this_label.closest(".wc_payment_method").find("input").first().prop("checked") === true ){
                        if ( !this_label.hasClass("tcstope_label_checked") ){
                            this_label.addClass("tcstope_label_checked");
                        }
                    }else{
                        this_label.removeClass("tcstope_label_checked");
                    }
                }
            });
        },300);
        
        $(".woocommerce-gzd-legal-checkbox-text").prepend('<div class="tcstope_agree_gzd_checkbox" ></div>');
        
        setInterval(function(){
            $("body").find(".woocommerce-gzd-legal-checkbox-text").each(function(){
                var this_label = $(this);
                if ( this_label.parent().find("input").first().prop("checked") === true ){
                    if ( !$(".tcstope_agree_gzd_checkbox").hasClass("tcstope_agree_gzd_checkbox_checked") ){
                        $(".tcstope_agree_gzd_checkbox").addClass("tcstope_agree_gzd_checkbox_checked");
                    }
                }else{
                    $(".tcstope_agree_gzd_checkbox").removeClass("tcstope_agree_gzd_checkbox_checked");
                }
            });
        },300);
        
        setInterval(function(){
            $("body").find(".woocommerce-checkout-review-order-table .cart_item").each(function(){
                var height = $(this).find(".wc-gzd-product-name-right").first().height()+42;
                if ( $(this).find(".product-total").css("top") != height+"px" ){
                    console.log( $(this).find(".product-total").css("top")+" == "+height+"px" );
                    $(this).find(".product-total").css("top",height+"px");
                }
            });
        },300);
        
        
    }
    
    
    $(".widget-mobile-top-panel ul").append('<li class="services search-icon-link tcstope_search_icon"><a class="search-icon" ><i class="hs-icon hs hs-search-2"></i></a></li>');
    
    setInterval(function (){ 
        $(".product-subtotal-price-mobile").each(function(){
            if ( typeof $(this).closest(".cart_item").find("input[type='number']").first().val() !== "undefined"  ){
                if ( $(this).find(".tcstope_qty_mobile_new").length == 0 ){
                    $(this).append('x <input type="number" class="tcstope_qty_mobile_new" value="'+$(this).closest(".cart_item").find("input[type='number']").first().val()+'" />');
                    
                    $(".tcstope_qty_mobile_new").on("keyup keydown keypress change blur",function(){
                        if ( typeof $(this).closest(".cart_item").find("input[type='number']").first().val() !== "undefined"  ){
                            $(this).closest(".cart_item").find("input[type='number']").first().val( $(this).val() );
                        }
                    });
                    
                }
            }
        });
        
    },500);
    
    $(".tcstope_ssc_subs_button").click(function(){
        if ( !$(".tcstope_ssc_subs_button").hasClass("tcstope_ssc_subs_button_not_allowed") ){
            
            //console.log($(".tcstope_ssc_f_name").val().trim());
            
            if ( $(".tcstope_ssc_f_name").val().trim() == "" || $(".tcstope_ssc_f_name").val().trim() == "Dein Name" ){
                $(".tcstope_ssc_f_name").css("border-color","#c71111");
            }else{
                $(".tcstope_ssc_f_name").css("border-color","#9e9e9e");
            }
            
            if ( $(".tcstope_ssc_f_email").val().trim() == "" || $(".tcstope_ssc_f_email").val().trim() == "E-Mail Adresse" || !isEmail($(".tcstope_ssc_f_email").val().trim()) ){
                $(".tcstope_ssc_f_email").css("border-color","#c71111");
            }else{
                $(".tcstope_ssc_f_email").css("border-color","#9e9e9e");
            }
            
            if ( $(".tcstope_ssc_f_email").val().trim() != "" && $(".tcstope_ssc_f_email").val().trim() != "" && $(".tcstope_ssc_f_name").val().trim() != "Dein Name" && $(".tcstope_ssc_f_email").val().trim() != "E-Mail Adresse" && isEmail($(".tcstope_ssc_f_email").val().trim()) ){
            
                $(this).addClass("tcstope_ssc_subs_button_pressed");
                
                $.ajax({
                    type: 'POST',
                    dataType: 'json',
                    url: tcstope_main_js_array.ajaxurl,
                    data: { 
                        'action':           'tcstope_ssc_subs_button_subscribe',
                        'fname':            $(".tcstope_ssc_f_name").val(),
                        'email':            $(".tcstope_ssc_f_email").val() 
                    },
                    success: function(data){
                        if (data.subs == 1){
                            $(".tcstope_shop_subscribe_content_form").css("visibility","hidden");
                            setTimeout(function(){
                                $(".tcstope_shop_subscribe_content_thankyou").fadeIn();                
                            },1500);
                            
                        }else{
                            
                        }
                    }
                });
                                    
            }
        }
    });
    
    $(".tcstope_ssc_checkbox, .tcstope_ssc_label").click(function(){
        $(".tcstope_ssc_checkbox").toggleClass("tcstope_ssc_checkbox_checked");
        
        if ( $(".tcstope_ssc_checkbox").hasClass("tcstope_ssc_checkbox_checked") ){
            $(".tcstope_ssc_subs_button").removeClass("tcstope_ssc_subs_button_not_allowed");
        }else{
            $(".tcstope_ssc_subs_button").addClass("tcstope_ssc_subs_button_not_allowed");
        }
    });

    
});

function isEmail(email) {
  var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  return regex.test(email);
}
