jQuery(document).ready(function($){

	/*if ($(window).width() < 480) {
		$('.woocommerce-loop-product__title').each(function(i, obj) {
		$(obj).text($(obj).text().substr(0, 40)+'...');
 		});
	}*/

	/*$( ".variations_form" ).on( "woocommerce_variation_select_change", function () {
		var id = $('input.variation_id').val();
		console.log( id ); // fires, but returns empty
	} );*/

	$( ".single_variation_wrap" ).on( "show_variation", function ( event, variation ) {
		if($('#btnPrice').length !== 0){
			if($('.variations_form').length !== 0){
				$('#btnPrice').attr('data-price', variation.display_price);
				var price = variation.display_price.replace(/,/g, '') * $('.qty').val();
				$('#btnPrice').html('£'+price+' - Add to basket');
			}
			else{
				var price = $('#btnPrice').attr('data-price').replace(/,/g, '') * $('.qty').val();
				$('#btnPrice').html('£'+price+' - Add to basket');
			}
		}
	} );
	if($('#btnPrice').length !== 0){
		var price = $('#btnPrice').attr('data-price').replace(/,/g, '') * $('.qty').val();
		$('#btnPrice').html('£'+price+' - Add to basket');
	}

	$('.qty').change(function(){
		var price = $('#btnPrice').attr('data-price').replace(/,/g, '') * $('.qty').val();
		$('#btnPrice').html('£'+price+' - Add to basket');
	});


	$(window).resize(function(){
		if(jQuery('.woocommerce-product-gallery--with-images').length !== 0){
			jQuery('.woocommerce-product-gallery--with-images')[0].slick.refresh();
			if(jQuery('.related .row.products').length > 0){
				jQuery('.related .row.products')[0].slick.refresh();
			}
		}
	});
	var $slider = jQuery('.woocommerce-product-gallery--with-images').slick({
		dots: true,
		slidesToShow: 1,
		slidesToScroll: 1,
		adaptiveHeight: true,
		focusOnSelect:true,
    });

	$slider.find(".slick-slide a").on("click", function(e){
	   e.preventDefault();
	   jQuery('.woocommerce-product-gallery--with-images').slick("slickNext");
	   return false;
	});

	var $related = jQuery('.related .row.products').slick({
		dots: true,
		slidesToShow: 4,
		slidesToScroll: 4,
		responsive: [
			{
			  breakpoint: 1300,
			  settings: {
				slidesToShow: 3,
				slidesToScroll: 3
			  }
			},
			{
			  breakpoint: 870,
			  settings: {
				slidesToShow: 2,
				slidesToScroll: 2
			  }
			},
			{
			  breakpoint: 560,
			  settings: {
				slidesToShow: 2,
				slidesToScroll: 2
			  }
			},

	    ]
    });

	/*$('#tab-title-deliverytab a').click(function(e){
		e.preventDefault();
		//window.open($(this).attr('data-url'), '_blank');
		console.log('triggered');
		return false;
	});*/
	$('#tab-title-reviews a').click(function(e){
		e.preventDefault();
		var $topMargin = $('.header').outerHeight() - 1;

		$('html, body').animate({
          scrollTop: $('.product-reviews-wrap').offset().top - $topMargin
        }, 1000, function() {
			$('.product-reviews').fadeIn(600);
        });
		return false;
	});

	$('.quantity-field-wrap #quantity-inc, .quantity-field-wrap #quantity-dec').click(function() {

	// Get current quantity values
	var qty = $( this ).closest( 'form.cart' ).find( '.qty' );
	var val   = parseFloat(qty.val());
	var max = parseFloat(qty.attr( 'max' ));
	var min = parseFloat(qty.attr( 'min' ));
	var step = parseFloat(qty.attr( 'step' ));

	// Change the value if plus or minus
	if ( $( this ).is( '#quantity-inc' ) ) {
	   if ( max && ( max <= val ) ) {
		  qty.val( max );
	   }
	else {
	   qty.val( val + step );
		 }
	}
	else {
	   if ( min && ( min >= val ) ) {
		  qty.val( min );
	   }
	   else if ( val > 1 ) {
		  qty.val( val - step );
	   }
	}

	if($('#btnPrice').length !== 0){
		var price = $('#btnPrice').attr('data-price').replace(/,/g, '') * qty.val();
		$('#btnPrice').html('Add to basket - £'+parseFloat(price).toFixed(2));
	}
 });

	$('#eventselect').change(function(){
		var current_page = $('#current_page').val();
		var getvalue = $(this).val();
		console.log(current_page);
		console.log(getvalue);
		if (current_page == getvalue){
			return;
		}
		else{
			window.location.replace("/product/"+getvalue);
		}
	});

	$(".booking-floating-button-wrapper").click(function (){
				let movehere= $(this).closest('main').find('#wc-bookings-booking-form').first().offset().top - $("header").outerHeight() - 30;
				console.log(movehere);
                $('html').animate({
                    scrollTop: movehere
                }, 500);
	});

    $(".single-product-description .read-more-product-description").on('click', function(e) {
        e.preventDefault();

        $(".single-product-description .product-description").toggleClass('trimmed');
        $(".single-product-description .read-more-product-description").toggleClass('more');
    })
	
	$slider.on('beforeChange', function(event, slick, currentSlide, nextSlide){
	  const slide = slick.$slides.get(nextSlide);
	  if($(slide).find('video').length > 0){
		  $(slide).find('video').get(0).pause();
		  $(slide).find('video').get(0).currentTime = 0;
	  }
	});		
	$slider.on('afterChange', function(event, slick, currentSlide, nextSlide){
	  const slide = slick.$slides.get(currentSlide);
	  if($(slide).find('video').length > 0){
		  $(slide).find('video').get(0).pause();
		  $(slide).find('video').get(0).currentTime = 0;
		  $(slide).find('video').get(0).play();
	  }
	});	
	
	
	resizeSliderVideoItems();
	
	$(window).resize(function(){
		resizeSliderVideoItems();
	});
	
	function getVideoSlideNumber(){
		
		let slideCount = 0;
		
		$('.woocommerce-product-gallery figure').each(function(){
			if(!$(this).hasClass('slick-cloned')){
				
				slideCount++;
				
				if($(this).find('.video-slide').length > 0){
					return slideCount;
				}
			}
		});
		
		return false;
	}
	
	$('#watchProductVideo').on('click', function (e) {
		e.preventDefault();
		$('.woocommerce-product-gallery').slick('slickGoTo', getVideoSlideNumber());
		return false;
	})
				
    function resizeSliderVideoItems(){
		let maxHeight = 0;
		$('.woocommerce-product-gallery figure').each(function(){
			if($(this).outerHeight() > maxHeight){
				maxHeight = $(this).outerHeight();
			}
		});	
		$('.woocommerce-product-gallery figure').each(function(){
			if($(this).find('.video-slide').length > 0){
				if($(this).outerHeight() < maxHeight){
					$(this).find('.video-slide').attr('style', 'height: ' + maxHeight + 'px !important');
				}
			}
		});		
	}
});
