document.addEventListener("DOMContentLoaded", () => {
	// Handle high contrast toggle button click
	document.querySelector("#hc-toggle").addEventListener("click", function () {
		if (!document.body.classList.contains("hcm")) {
			document.body.classList.add("hcm");
			byf_set_cookie("byf_hc_mode", "1", 14);
		} else {
			document.body.classList.remove("hcm");
			byf_set_cookie("byf_hc_mode", "0", 14);
		}
		this.blur();
	});

	// Set high contrast mode if set in cookie
	function byf_set_hc_mode() {
		let byf_hc_mode = byf_get_cookie("byf_hc_mode");
		if (byf_hc_mode == 1) {
			document.body.classList.add("hcm");
		}
	}

	// Check for cookie on page load
	byf_set_hc_mode();

	// Generic get cookie
	function byf_get_cookie(cname) {
		var name = cname + "=";
		var decodedCookie = decodeURIComponent(document.cookie);
		var ca = decodedCookie.split(";");
		for (var i = 0; i < ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0) == " ") {
				c = c.substring(1);
			}
			if (c.indexOf(name) == 0) {
				return c.substring(name.length, c.length);
			}
		}
		return "";
	}

	// Generic set cookie
	function byf_set_cookie(cname, cvalue, exdays) {
		var d = new Date();
		d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
		var expires = "expires=" + d.toUTCString();
		document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
	}

	// Event listeners for updating 'aria-expanded' property for dropdowns when using keyboard navigation
	document.querySelectorAll(".nav-item").forEach((navlink) => {
		navlink.addEventListener("click", function (e) {
			if (navlink.parentNode.classList.contains("show")) {
				return;
			}
			byf_set_aria(navlink);
		});

		navlink.addEventListener("keyup", function (e) {
			if (e.keyCode != 9 || navlink.getAttribute("aria-expanded") == "true") {
				return;
			}
			byf_set_aria(navlink);
		});
	});

	// Helper function to update aria-expanded property
	function byf_set_aria(navlink) {
		document.querySelectorAll("button.nav-link").forEach((link) => {
			link.setAttribute("aria-expanded", "false");
		});

		if (navlink.tagName == "BUTTON") {
			navlink.setAttribute("aria-expanded", "true");
		}
	}

	// CAROUSEL SLIDE, FOCUS NEXT SLIDE
	let slides = document.querySelectorAll(".carousel-item");
	let slide_count = slides.length;
	let slide_index = 0;

	document.querySelectorAll(".carousel-control-next").forEach((next_btn) => {
		next_btn.addEventListener("click", () => {
			slide_index = slide_index + 1 == slide_count ? 0 : slide_index + 1;
			focus_slide();
		});
	});

	document.querySelectorAll(".carousel-control-prev").forEach((prev_btn) => {
		prev_btn.addEventListener("click", () => {
			slide_index = slide_index - 1 < 0 ? slide_count - 1 : slide_index - 1;
			focus_slide();
		});
	});

	function focus_slide() {
		// console.log(slide_index);
	}
});
