jQuery( document ).ready(function($) {
	
	$('.testi-slid').slick({
		arrows: true,
		slidesToShow: 1,
		slidesToScroll: 1,
		centerMode: false,
		rows: 0,	
		adaptiveHeight: true,
		arrows: false,
		dots:true,
		autoplay:true,
		 speed:200,
	});

	$('.btn-number').click(function(e){
		e.preventDefault();
		
		fieldName = $(this).attr('data-field');
		type      = $(this).attr('data-type');
		var input = $("input[name='"+fieldName+"']");
		var currentVal = parseInt(input.val());
		if (!isNaN(currentVal)) {
			if(type == 'minus') {
				
				if(currentVal > input.attr('min')) {
					input.val(currentVal - 1).change();
				} 
				if(parseInt(input.val()) == input.attr('min')) {
					$(this).attr('disabled', true);
				}

			} else if(type == 'plus') {

				if(currentVal < input.attr('max')) {
					input.val(currentVal + 1).change();
				}
				if(parseInt(input.val()) == input.attr('max')) {
					$(this).attr('disabled', true);
				}

			}
		} else {
			input.val(0);
		}
	});
	$('.input-number').focusin(function(){
	   $(this).data('oldValue', $(this).val());
	});
	$('.input-number').change(function() {
		
		minValue =  parseInt($(this).attr('min'));
		maxValue =  parseInt($(this).attr('max'));
		valueCurrent = parseInt($(this).val());
		
		name = $(this).attr('name');
		if(valueCurrent >= minValue) {
			$(".btn-number[data-type='minus'][data-field='"+name+"']").removeAttr('disabled')
		} else {
			alert('Sorry, the minimum value was reached');
			$(this).val($(this).data('oldValue'));
		}
		if(valueCurrent <= maxValue) {
			$(".btn-number[data-type='plus'][data-field='"+name+"']").removeAttr('disabled')
		} else {
			alert('Sorry, the maximum value was reached');
			$(this).val($(this).data('oldValue'));
		}
	});

	$(".input-number").keydown(function (e) {
		// Allow: backspace, delete, tab, escape, enter and .
		if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 190]) !== -1 ||
			 // Allow: Ctrl+A
			(e.keyCode == 65 && e.ctrlKey === true) || 
			 // Allow: home, end, left, right
			(e.keyCode >= 35 && e.keyCode <= 39)) {
				 // let it happen, don't do anything
				 return;
		}
		// Ensure that it is a number and stop the keypress
		if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
			e.preventDefault();
		}
	});
	
	$('.product-slider').slick({
		arrows: true,
		slidesToShow: 4,
		slidesToScroll: 1,
		centerMode: false,
		rows: 0,	
		adaptiveHeight: true,
		dots:false,
		prevArrow: '<div class="class-to-style"><span class="fa-solid fa-arrow-left"></span><span class="sr-only">Prev</span></div>',
		nextArrow: '<div class="class-to-style right"><span class="fa-solid fa-arrow-right"></span><span class="sr-only">Next</span></div>',
		 responsive: [{
		  breakpoint: 1199,
		  settings: {
			slidesToShow: 3,
			slidesToScroll: 1,

		  }
		},   {
			 breakpoint: 767,
			 settings: {
			slidesToShow: 2,
			slidesToScroll: 1,

		  }
		},   {
		  breakpoint: 480,
		  settings: {
			slidesToShow: 1,
			slidesToScroll: 1,
			infinite: true,
			autoplay: true,
			autoplaySpeed: 2000,
		  }
		}]	
	});

	$('.navbar').addClass('js-enabled');
    $('.navbar .has_sub_menu > a').after('<div class="child-trigger"><i class="fa-solid fa-angle-down"></i></div>');    
	jQuery('.child-trigger').click(function() {
        jQuery(this).toggleClass('child-open');
        return false;
    }); 

   jQuery('.back-btn').click(function() {
        jQuery(this).parent().siblings('.child-trigger').removeClass('child-open');
        return false;
    });
	
	jQuery(".child-trigger").click(function(){
        jQuery(this).next(".submenu").slideToggle();
    });
	
	$('.next_prev_button button.next_sec_button').click(function(e){
		e.preventDefault();
		//$(this).closest('.customize_section_item').removeClass('active');
		//	$(this).closest('.customize_section_item').removeClass('active');
	});
	
	var customize_slick = $('.customize_product_detail_section .stick-text').slick({
		dots: false,
		arrows: false,
		infinite: false,
		speed: 300,
		slidesToShow: 1,
		slidesToScroll: 1,
		draggable: false,
		swipe: false,
		touchMove: false
	}); 
	
	$('.next_prev_button button.prev_sec_button').click(function(e){ 
      	e.preventDefault(); 
		$('.customize_product_detail_section .stick-text').slick('slickPrev');
	} );
	$('.next_prev_button button.next_sec_button').click(function(e){
		e.preventDefault(); 
		$('.customize_product_detail_section .stick-text').slick('slickNext');
	} );  
	
	$('.select_design_tab a').click(function(e){
		e.preventDefault();
		if(!$(this).parent().hasClass('active')){
			var prev_active_div = $('.select_design_tab li.active a').attr('data-id');
			var curent_div = $(this).attr('data-id');
			$('.select_design_tab li.active').removeClass('active');
			$(this).parent().addClass('active');
			$('.'+prev_active_div).hide();
			$('.'+curent_div).show();		
		}
	});
	
	$('button.custom_simple_add_to_cart.single_add_to_cart_button.button').click(function(e){
		e.preventDefault();
		var price = $('.customize_quantity select').val();
		var quan  = $('.customize_quantity select option[value="'+price+'"]').attr('date-qty');
		var id = $(this).val();
		if(price != null && quan != null && id != null){
			$.ajax({
				type: 'POST',
				url: wpObj.ajaxurl,
				data: {
					action:'custom_simple_add_to_cart',
					price:price,
					id:id,
					quan:quan
				},
				dataType:'json',
				beforeSend: function() {
					$('body').addClass('open_customize_loader');  
					$('body').append('<div class="customize_product_loader"><div class="customize-lds-ellipsis"><div></div><div></div><div></div><div></div></div</div>');
				},
				success: function (data) {
					if(data.error){
						$('body').removeClass('open_customize_loader');  
						$('body .customize_product_loader').remove();
						alert('Please try again');
					}else{
						window.location.href = wpObj.cart_url;
					}
				}, error: function (jqXHR, exception) {
					$('body').removeClass('open_customize_loader');  
					$('body .customize_product_loader').remove();
				}
			}); 
		}
	});
});


 

