// Begin: Hero Carousel
let currentIndex = 0;
function showSlide(index) {
	const slides = document.querySelectorAll('.hero-carousel-item');
    if (slides.length === 0) {
        return; // Exit the function if no slides are found
    }
	const totalSlides = slides.length;
	currentIndex = (index + totalSlides) % totalSlides;
	const offset = -currentIndex * 100;
	document.getElementById('hero-carouselInner').style.transform = `translate3d(${offset}%, 0, 0)`;
	document.querySelectorAll('.carousel-indicators li').forEach((indicator, idx) => {
		indicator.classList.toggle('active', idx === currentIndex);
	});
}
function nextSlide() {
	showSlide(currentIndex + 1);
}
function prevSlide() {
	showSlide(currentIndex - 1);
}
function goToSlide(index) {
	showSlide(index);
}
function createIndicators() {
	const slides = document.querySelectorAll('.hero-carousel-item');
    if (slides.length === 0) {
        return; // Exit the function if no slides are found
    }
	const indicatorsContainer = document.createElement('ol');
	indicatorsContainer.className = 'carousel-indicators';
	slides.forEach((_, index) => {
		const li = document.createElement('li');
		li.setAttribute('data-slide-to', index);
		li.onclick = () => goToSlide(index);
		if (index === 0) li.classList.add('active');
		indicatorsContainer.appendChild(li);
	});
	document.querySelector('.hero-carousel').appendChild(indicatorsContainer);
}
createIndicators();
showSlide(currentIndex);	
// End: Hero Carousel
// Begin: Toggle 12-24 Switch
function toggleMos(m12Id, m24Id, chkBoxId) {
	var m12 = document.getElementById(m12Id);
	var m24 = document.getElementById(m24Id);
	var chkBox = document.getElementById(chkBoxId);

	if (chkBox) {
		if (chkBox.checked) {
			m12.className = "font-weight-normal";
			m24.className = "font-weight-bold";
		} else {
			m12.className = "font-weight-bold";
			m24.className = "font-weight-normal";
		}
	}
}
// End: Toggle 12-24 Switch
document.addEventListener("DOMContentLoaded", function() {
	
	// Begin: Equal Height Div
	const rows = document.querySelectorAll('.de_pricing-tables');
	rows.forEach(row => {
		const boxes = row.querySelectorAll('.equal-div');

		if (boxes.length > 0) {
			let maxHeight = 0;

			boxes.forEach(box => {
				const boxHeight = box.offsetHeight;
				if (boxHeight > maxHeight) {
					maxHeight = boxHeight;
				}
			});

			boxes.forEach(box => {
				box.style.height = `${maxHeight}px`;
			});
		}
	});
	// End: Equal Height Div
	
	// Begin: For tab GA events
	var tabFunctions = {
		"h1-tab": typeof trackSecurityGA === "function" ? trackSecurityGA : null,
		"h2-tab": typeof trackIdentityGA === "function" ? trackIdentityGA : null,
		"h3-tab": typeof trackScamGA === "function" ? trackScamGA : null
	};

	Object.keys(tabFunctions).forEach(function(id) {
		var el = document.getElementById(id);
		var fn = tabFunctions[id];

		if (el && typeof fn === "function") {
			el.addEventListener("click", fn);
		}
	});
	// End: For tab GA events
	
	// Begin: Video Modal
    const videoModals = document.querySelectorAll('.video-modal');

    videoModals.forEach(modal => {
        modal.addEventListener('show.bs.modal', function () {
            modal.removeAttribute('inert'); // Allow interaction with modal
        });

        modal.addEventListener('hidden.bs.modal', function () {
            const iframe = modal.querySelector('iframe');
            const src = iframe.src; // Get the current src of the iframe
            iframe.src = ''; // Temporarily remove the src to pause the video
            iframe.src = src; // Restore the src to reset the video
            modal.setAttribute('inert', ''); // Add inert when the modal is hidden
        });
    });
	// End: Video Modal
	
});

// Coupon copy-to-clipboard
function copyToClipboard(element) {
	var couponText = element.querySelector(".code").innerText;
	var tempInput = document.createElement("input");
	tempInput.value = couponText;
	document.body.appendChild(tempInput);
	tempInput.select();
	document.execCommand("copy");
	document.body.removeChild(tempInput);

	var couponCodeElement = element.querySelector(".cp-l");
    couponCodeElement.innerText = "COPIED!";
	element.classList.add("active");
    element.querySelector(".cp-l").classList.add("active");
    element.querySelector(".cp-i").classList.add("active");
	setTimeout(function() {
        couponCodeElement.innerText = 'Coupon Code';
        element.classList.remove("active");
        element.querySelector(".cp-l").classList.remove("active");
        element.querySelector(".cp-i").classList.remove("active");
    }, 1500);
}

window.addEventListener('scroll', function () {
	const topbar = document.getElementById('topbar');
	if (topbar) {
		if (window.scrollY > 0) {
			topbar.classList.add('fixed');
		} else {
			topbar.classList.remove('fixed');
		}
	}
});