// JavaScript Document
jQuery( document ).ready(function($) {
	$(window).scroll(function() {
		// sticky menu
		if ($(window).scrollTop() > 50) {
			$("body").addClass("sticky");
		} else {
			$("body").removeClass("sticky");
		}		
	});
	
	$("#scrollTop").click(function() {
		$('html, body').stop().animate({'scrollTop': 0}, 1000);
	});
	
	let hda = "";
	for (let i = 1; i <= $(".homeslide").length; i++) {hda += "<a>" + i + "</a>";}	
	$("#homedots .elementor-widget-container").append("<p>" + hda + "</p>");
	$("#homedots a").click(function() {
		changeslide($(this).index());
		$(this).addClass("stop");
	});

	if ($(".visiteguidate").length > 0) {
		$("html").css("background-color","#171715");
	}


	jQuery.fn.sortDomElements = (function() {
		return function(comparator) {
			return Array.prototype.sort.call(this, comparator).each(function(i) {
				  this.parentNode.appendChild(this);
			});
		};
	})();
	$(".eventinewshome").children().sortDomElements(function(a,b){
		akey = $(a).attr("sortkey");
		bkey = $(b).attr("sortkey");
		if (akey == bkey) return 0;
		if (akey > bkey) return -1;
		if (akey < bkey) return 1;
	})

	setTimeout(NFL, 2000);

	if ($('.rtec-guests').length > 0) {
		$('.rtec-guests input').on('change',function() {
			console.log($(this).val());
			if ($(this).val() > 1) {
				$('#rtec_other').html("");
				$('label[for="rtec_other"]').html("Note*");
				$('.specifica').show();
			} else {
				$('#rtec_other').html("-");
				$('label[for="rtec_other"]').html("Note");
				$('.specifica').hide();
			}
		});
		$('#rtec_other').html("-");
		$('label[for="rtec_other"]').html("Note");
		$('.specifica').hide();
	}
	
	if ($(".carousel .eventinewshome li").length > 3) {
		$(".carousel .eventinewshome").slick({
			infinite: false,
			slidesToShow: 3,
			slidesToScroll: 1,
			dots: true,
			responsive: [{
				breakpoint: 900,
				settings: {
				slidesToShow: 2
				}
			}, {
				breakpoint: 600,
				settings: {
				slidesToShow: 1
				}
			}]
		});
	}
});

jQuery(window).on('load', function () {
	//
	if (jQuery("#homedots").length > 0) {
		jQuery("#homedots a").html("")
		changeslide(0);
		setTimeout(homeslide, 5000);
	}

	jQuery(".archive.date .breadcrumb_last").before("<span><a href='https://www.risorgivedelbacchiglione.it/attivita/eventi-e-news/'>News</a></span> | ");

	if (jQuery(".privacypolicylink label").length > 0) {
		let pplab = jQuery(".privacypolicylink label").html();
		pplab = pplab.replace("privacy policy", "<a href='https://www.risorgivedelbacchiglione.it/privacy-policy/' target='_blank'>Privacy Policy</a>");
		jQuery(".privacypolicylink label").html(pplab);
	}

});

function homeslide() {
	if (!jQuery('#homedots a.stop').length) {
		let index = jQuery("#homedots a.active").index();
		index = (index < jQuery("#homedots a").length-1)?(index + 1):0;
		changeslide(index);
		setTimeout(homeslide, 5000);
	}	
}

function changeslide(index) {
	let nth = index + 1;
	jQuery("#homedots a").removeClass("active");
	jQuery("#homedots a:nth-child(" + nth + ")").addClass("active");
	jQuery(".homeslide").removeClass("manifest");
	jQuery(".homeslide:nth-child(" + nth + ")").addClass("manifest");
}

function NFL() {
	jQuery('.opzioni_e_tariffe li label').each(function( index ) {
	  let oet = jQuery(this).html();
	  let oetp = oet.indexOf("|");
	  oetc = oet.substring(0,oetp);
	  jQuery(this).parent().addClass(oetc.trim());
  
	  oet = oet.substring(oetp+1);
	  oet = oet.replace("(","<br>(");
	  jQuery(this).html(oet.trim());
	})

	if (jQuery('.opzioni_e_tariffe').length > 0) {
		let pplab = jQuery(".privacypolicylink label").html();
		pplab = pplab.replace("privacy policy", "<a href='https://www.risorgivedelbacchiglione.it/privacy-policy/' target='_blank'>Privacy Policy</a>");
		jQuery(".privacypolicylink label").html(pplab);
	}
  }