(function () {
	'use strict';

	// FAQ toggle
	document.querySelectorAll('.virza-toggle').forEach(function (toggleBtn) {
		toggleBtn.addEventListener('click', function () {
			toggleBtn.parentNode.classList.toggle('virza-active');
		});
	});

	// Mobile nav submenu expand/collapse via arrow area click
	function virza_initNavigationToggles() {
		var navItems = document.querySelectorAll('.wp-block-navigation-item.has-child > .wp-block-navigation-item__content');
		navItems.forEach(function (item) {
			item.addEventListener('click', function (e) {
				var parentItem = item.closest('.wp-block-navigation-item.has-child');
				if (!parentItem) return;
				var arrowWidth = 20;
				var parentRect = parentItem.getBoundingClientRect();
				var clickPosition = e.pageX - (parentRect.left + window.pageXOffset);
				var totalWidth = item.getBoundingClientRect().width;
				if (totalWidth - clickPosition <= arrowWidth) {
					e.preventDefault();
					parentItem.classList.toggle('open');
				}
			});
		});
	}
	document.addEventListener('DOMContentLoaded', virza_initNavigationToggles);

	// Sticky header menu
	document.addEventListener('DOMContentLoaded', function () {
		var menu = document.querySelector('.opmhs-menu');
		if (!menu) return;
		var start = menu.offsetTop;
		window.addEventListener('scroll', function () {
			if (window.scrollY > start) {
				menu.classList.add('is-stuck');
			} else {
				menu.classList.remove('is-stuck');
			}
		}, { passive: true });
	});

	// Scroll reveal — fires when element enters viewport
	document.addEventListener('DOMContentLoaded', function () {
		var revealEls = document.querySelectorAll('.tse-section');
		if (!revealEls.length) return;
		var revealObserver = new IntersectionObserver(function (entries) {
			entries.forEach(function (entry) {
				if (entry.isIntersecting) {
					entry.target.classList.add('virza-revealed');
					revealObserver.unobserve(entry.target);
				}
			});
		}, {
			threshold: 0.12,
			rootMargin: '0px 0px -40px 0px'
		});
		revealEls.forEach(function (el, index) {
			el.style.transitionDelay = (index * 80) + 'ms';
			revealObserver.observe(el);
		});
	});

	// Lazy-load Google Maps iframe
	function virza_loadMap(container) {
		if (!container) return;
		container.innerHTML =
			'<iframe ' +
			'src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d56198.70009981208!2d-0.15394403868720177!3d51.501198856251484!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x47d8a00baf21de75%3A0x52963a5addd52a99!2sLondon%2C%20UK!5e0!3m2!1sen!2sbd!4v1768313911692!5m2!1sen!2sbd" ' +
			'width="100%" height="480" style="border:0;" ' +
			'allowfullscreen="" loading="lazy" ' +
			'referrerpolicy="no-referrer-when-downgrade">' +
			'</iframe>';
	}

	document.addEventListener('DOMContentLoaded', function () {
		var mapEl = document.getElementById('map-lazy-container');
		if (!mapEl) return;

		// Manual trigger via button
		var mapBtn = mapEl.querySelector('.map-load-btn');
		if (mapBtn) {
			mapBtn.addEventListener('click', function () {
				virza_loadMap(mapEl);
			});
		}

		// Auto-trigger when user scrolls near the map section
		var obs = new IntersectionObserver(function (entries) {
			entries.forEach(function (entry) {
				if (entry.isIntersecting) {
					virza_loadMap(mapEl);
					obs.disconnect();
				}
			});
		}, { rootMargin: '200px' });
		obs.observe(mapEl);
	});

	// Stats counter animation (count-up)
	function virza_countUp(el, target, duration, prefix, suffix) {
		prefix = prefix || '';
		suffix = suffix !== undefined ? suffix : '+';
		var start = 0;
		var stepTime = Math.floor(duration / target);
		var timer = setInterval(function () {
			start++;
			el.textContent = prefix + start + suffix;
			if (start >= target) {
				el.textContent = prefix + target + suffix;
				clearInterval(timer);
			}
		}, stepTime);
	}

	document.addEventListener('DOMContentLoaded', function () {
		var statsSection = document.querySelector('.virza-stats-section');
		if (!statsSection) return;
		var counters = statsSection.querySelectorAll('.virza-stat-number');
		var hasRun = false;
		var statsObserver = new IntersectionObserver(function (entries) {
			entries.forEach(function (entry) {
				if (entry.isIntersecting && !hasRun) {
					hasRun = true;
					counters.forEach(function (counter) {
						var target = parseInt(counter.dataset.target, 10);
						var prefix = counter.dataset.prefix || '';
						var suffix = counter.dataset.suffix !== undefined
							? counter.dataset.suffix
							: '+';
						virza_countUp(counter, target, 1200, prefix, suffix);
					});
					statsObserver.disconnect();
				}
			});
		}, { threshold: 0.3 });
		statsObserver.observe(statsSection);
	});

})();
