// source --> https://www.kurija-mihalovic.hr/wp-content/themes/destinacija-fericanci/assets/js/default.js?ver=1.0.0 
$( document ).ready(function() {

	$('#booking-form').on('submit', function(e) {
        e.preventDefault();
        checkin = $(this).find('input[data-field="checkin"]').val();
		$(this).find('input[name="from"]').val(checkin.replace(/\./g, '-'));
        checkout = $(this).find('input[data-field="checkout"]').val();
		$(this).find('input[name="to"]').val(checkout.replace(/\./g, '-'));
        this.submit();
    });

	/* ------------------------------ NAVIGATION TOGGLE ------------------------------ */
	$('.nav-toggle').on('click',function() {
		$('body').toggleClass('mobile-nav-active');
	});

	/* ------------------------------ DROPDOWN TOGGLE ------------------------------ */
	$.fn.onClassChange = function(callback) {
		return this.each(function() {
			new MutationObserver(function(mutations) {
				mutations.forEach(function(mutation) {
					if (mutation.attributeName === "class") {
						callback && callback(mutation.target, mutation.target.className);
					}
				});
			}).observe(this, {
				attributes: true,
				attributeFilter: ['class']
			});
		});
	};

	$('.mobile-menu nav > ul > li').onClassChange(function(el, nc){
		if($(el).hasClass('active')) $(el).find('.dropdown').css('max-height', $(el).find('.dropdown')[0].scrollHeight + 'px');
		else $(el).find('.dropdown').css('max-height', '0px');

	});

	$('.nav-head .dropdown-toggle').on('click', function(){
		var el = $(this).closest('li');
		$('.mobile-menu nav > ul > li').not(el).removeClass('active');
		el.toggleClass('active');
	});

	/* --------------------------- SCROLL HEADER ------------------------ */
	var prevScrollpos = window.pageYOffset;
	$(window).on( "scroll", function() {
		var currentScrollPos = window.pageYOffset;
		if (prevScrollpos > currentScrollPos) {
			$('body').addClass('scroll-top');
			$('body').removeClass('scroll-bottom');
		} else {
			$('body').removeClass('scroll-top');
			$('body').addClass('scroll-bottom');
		}
		prevScrollpos = currentScrollPos;

		if(currentScrollPos <= 0) {
			$('body').removeClass('scroll-top').removeClass('scroll-bottom');
		}
	});

	/* ------------------------------ RESERVATION TOGGLE ------------------------------ */
	$('.cta-wrapper .button').on('click',function() {
		$('body').toggleClass('reservation-window-active');
	});

	/* ---------------------------------- Inview ---------------------------------- */
    inView('.inview').on('enter', function(e){
        e.classList.add('inview-on');
    }).on('exit', function(e){
        e.classList.remove('inview-on');
    });

	/* ---------------------------------- Popup ---------------------------------- */

	$('.close-popup').on('click', function(e){
		e.preventDefault();
		var _p = $(this).parent().parent();
		_p.addClass('d-none');
	});

	$('[data-campaign]').each(function(){
		_cid = $(this).data('campaign');
		if(!localStorage.getItem(_cid)){
			$(this).removeClass('d-none');
			_d = new Date().getTime();
			localStorage.setItem(_cid, _d);
		}
	});

	/* ---------------------------------- Swiper ---------------------------------- */
	var swiperGallery = new Swiper(".gallery-swiper", {
		slidesPerView: 1.4,
		centeredSlides: true,
		loop: true,
		spaceBetween: 10,
		breakpoints: {
			768: {
				slidesPerView: 2.2,
				spaceBetween: 20,
			}
		},
	});

	var swiperItems = new Swiper(".items-swiper", {
		slidesPerView: 1.15,
		spaceBetween: 30,
		allowTouchMove: true,
		navigation: {
			nextEl: ".swiper-button-next",
			prevEl: ".swiper-button-prev",
		},
		breakpoints: {
			768: {
				slidesPerView: 2.2,
				spaceBetween: 20,
			},
			992: {
				slidesPerView: 2.4,
				spaceBetween: 20,
			},
			1200: {
				slidesPerView: 3.4,
				spaceBetween: 30,
			},
			1400: {
				slidesPerView: 4.4,
				spaceBetween: 30,
			}
		},
	});

	$('.featured-swiper').each(function(i) {
		$(this).addClass('swiper-container-' + i);
		$('.swiper-button-next', this).addClass('swiper-button-next-'+i);
		$('.swiper-button-prev', this).addClass('swiper-button-prev-'+i);
		new Swiper('.swiper-container-' + i, {
			slidesPerView: 1.2,
			spaceBetween: 10,
			navigation: {
				nextEl: '.swiper-button-next-'+i,
				prevEl: '.swiper-button-prev-'+i,
			},
			breakpoints: {
				768: {
					slidesPerView: 2.2,
					spaceBetween: 20,
				},
				992: {
					slidesPerView: 3,
					spaceBetween: 20,
				}
			},
		});
	});

	var swiperVineyard = new Swiper(".vineyard-swiper", {
		slidesPerView: 1,
		effect: "fade",
		navigation: {
			nextEl: ".swiper-button-next",
			prevEl: ".swiper-button-prev",
		},
		on: {
			slideChange: function(){
				var _i = this.activeIndex;
				$('.vinogorje-wrapper [data-slide]').removeClass('active');
				$('.vinogorje-wrapper [data-slide="'+_i+'"]').addClass('active');
			}
		}

	});

	$('.vinogorje-wrapper [data-slide]').on('click', function(e){
		e.preventDefault();
		$('.vineyard-swiper').removeClass('d-none');
		var _slide = $(this).data('slide');
		$('.vinogorje-wrapper [data-slide]').removeClass('active');
		swiperVineyard.slideTo(_slide);
		$(this).addClass('active');
	});

	$('.vineyard-swiper').on('click', function(e){
		console.log(e.target.className);
		if (e.target === this || e.target.className == 'close-swiper' || e.target.classList.contains('swiper-slide-active')){
			$('.vineyard-swiper').addClass('d-none');
			$('.vinogorje-wrapper [data-slide]').removeClass('active');
		}
	}); 

}).on('keydown', function(event){
	if (event.key === "Escape"){
		if($('.vineyard-swiper').length) $('.vineyard-swiper .close-swiper').trigger('click');
		if($('[data-campaign]').length && $('.close-popup').length) $('.close-popup').trigger('click');
	}
});

/* ------------------------------ PARALLAX IMAGES ------------------------------ */
function throttle(fn, wait){
	let time = Date.now();
	return function() {
		if ((time + wait - Date.now()) < 0){
			fn();
			time = Date.now();
		}
	};
}

function updateDualParallax() {
	$('.two-image-parallax').each(function(){
		var $wrapper = $(this);
		var wrapperOffset = $wrapper.offset().top;
		var windowScroll = $(window).scrollTop();
		var windowHeight = $(window).height();
		var wrapperHeight = $wrapper.height();
		var startScroll = wrapperOffset - windowHeight;
		var endScroll = wrapperOffset + wrapperHeight;
		var progress = (windowScroll - startScroll) / (endScroll - startScroll);
		progress = Math.min(Math.max(progress, 0), 1);

		var img1 = $wrapper.find('img:first-child');
		var img2 = $wrapper.find('img:last-child');

		var img1Height = img1.height();
		var img2Height = img2.height();

		var img1StartTop = 0;
		var img1EndTop = wrapperHeight - img1Height;

		var img2StartTop = 0.10 * wrapperHeight;
		var img2EndTop = wrapperHeight - img2Height - 0.10 * wrapperHeight;

		var img1Top = img1StartTop + (img1EndTop - img1StartTop) * progress;
		var img2Top = img2StartTop + (img2EndTop - img2StartTop) * progress;

		img1.css('top', img1Top + 'px');
		img2.css('top', img2Top + 'px');
	});
}

function updateParallax() {
	$('.parallax').each(function(){
		var _element = $(this);
		var _image = _element.find('img');
		var start = _element.offset().top - $(window).height();
		var end = _element.offset().top + _element.outerHeight();
		var progress = ($(window).scrollTop() - start) / (end - start);
		var clampedProgress = Math.max(0, Math.min(1, progress));
		var newHeight = -1 * clampedProgress * (_image.outerHeight() - _element.outerHeight());
		_image.css('transform', 'translateY(' + newHeight + 'px)');
	});
}

function onScroll() {
	updateDualParallax();
	updateParallax();
}

let ticking = false;
function requestTick() {
	if (!ticking) {
		window.requestAnimationFrame(function() {
			onScroll();
			ticking = false;
		});
		ticking = true;
	}
}

$(window).on('scroll', requestTick);
$(window).on('resize', throttle(function() { onScroll(); }, 100));
$(window).on('load', onScroll);
// source --> https://www.kurija-mihalovic.hr/wp-content/themes/hotel-kurija-mihalovic/specific.js?ver=1.0.0 
$(document).ready(function(){

	/* ------------------------------ DROPDOWN TOGGLE ------------------------------ */
	$('.mobile-menu nav > ul > li:nth-child(2)').addClass('active');
	setTimeout(function(){ $('.site-hotel-kurija-mihalovic .mobile-menu nav > ul > li:nth-child(2) .dropdown').css('max-height', '100vh'); }, 300);

	/* ------------------------------ DATE PICKER ------------------------------ */
	(function () {
		Datepicker.locales.hr = {
			days: ["Nedjelja", "Ponedjeljak", "Utorak", "Srijeda", "Četvrtak", "Petak", "Subota"],
			daysShort: ["Ned", "Pon", "Uto", "Sri", "Čet", "Pet", "Sub"],
			daysMin: ["Ne", "Po", "Ut", "Sr", "Če", "Pe", "Su"],
			months: ["Siječanj", "Veljača", "Ožujak", "Travanj", "Svibanj", "Lipanj", "Srpanj", "Kolovoz", "Rujan", "Listopad", "Studeni", "Prosinac"],
			monthsShort: ["Sij", "Velj", "Ožu", "Tra", "Svi", "Lip", "Srp", "Kol", "Ruj", "Lis", "Stu", "Pro"],
			today: "Danas"
		};
	}());

	var today = new Date();
	for (let i = 1; i <= 2; i++) {
		const wrapper = $(`#datepicker-wrapper-${i}`);
		const inputStart = $(`#checkin-${i}`);
		const inputEnd = $(`#checkout-${i}`);
		if(wrapper.length){
			new DateRangePicker(wrapper[0], {
				inputs: [inputStart[0], inputEnd[0]],
				allowOneSidedRange: false,
				language: 'hr',
				autohide: false,
				format: 'dd.mm.yyyy',
				minDate: today,
				weekStart: 1
			});
		}
	}
});