var lastScrollTop = 0;

function closeMegaMenu() {
	$(".megamenu").removeClass("active");
	$(".megamenu .inner li a, .megamenu .inner li button").attr("tabindex", "-1");
}

function openMegaMenu() {
	$(".megamenu").addClass("active");
	$(".megamenu .inner > li > .category-label").first().focus();
	$(".megamenu .inner li .category-label, .megamenu .inner li .child-category, .megamenu .breadcrumb a").attr("tabindex", 3);
}

function toggleMegaMenu() {
	if ($(".megamenu").hasClass("active")) {
		closeMegaMenu();
		return;
	}
	openMegaMenu();
}

$(window).on("scroll", function scrollingHero() {
	var currentScroll = $(this).scrollTop();
	if (currentScroll > lastScrollTop && $(window).scrollTop() > 300) {
		$("header").addClass("down");
	} else {
		$("header").removeClass("down");
	}
	lastScrollTop = currentScroll;
});

$("a[data-bs-target], button[data-bs-target]").on("keydown", function clickNoHref(event) {
	if (event.key === "Enter") {
		this.dispatchEvent(new MouseEvent("click", { bubbles: true }));
	}
});

$("#navbarSupportedContent").on("show.bs.collapse", function closeMenu() {
	$("body").css("overflow", "hidden");
});

$("#navbarSupportedContent").on("hidden.bs.collapse", function closeMenu() {
	$(".megamenu").removeClass("active");
	var $firstBreadcrumb = $(".outer #header_breadcrumb .breadcrumb .breadcrumb-item:nth-child(1) a");
	if ($firstBreadcrumb.length) {
		$firstBreadcrumb.first().trigger("click");
	}
	$("body").css("overflow", "");
});

if ($(".layout2.animations").length) {
	$(window).on("scroll", function scrollingImageHero() {
		var $layout = $(".layout2");
		if (!$layout.length) {
			return;
		}
		if ($(window).innerWidth() > 991) {
			if ($(window).scrollTop() < ($layout.offset().top + $layout.innerHeight())) {
				$(".layout2 .secondary img").css("transform", "translateY(" + $(window).scrollTop() * 0.2 + "px) rotate(" + $(window).scrollTop() * -0.01 + "deg)");
				$(".layout2 .primary img").css("transform", "translateY(" + $(window).scrollTop() * -0.2 + "px) rotate(" + $(window).scrollTop() * 0.01 + "deg)");
				$(".layout2 .tertiary img").css("transform", "translateY(" + $(window).scrollTop() * -0.2 + "px) rotate(" + $(window).scrollTop() * 0.002 + "deg)");
			}
		} else {
			$(".layout2 .secondary img").css("transform", "translateY(0px) rotate(0deg)");
			$(".layout2 .primary img").css("transform", "translateY(0px) rotate(0deg)");
			$(".layout2 .tertiary img").css("transform", "translateY(0px) rotate(0deg)");
		}
	});
}

$(".megamenu .toggler, .megamenu .overlay").on("click", function openMenu() {
	toggleMegaMenu();
});

$(".megamenu .toggler").on("keydown", function openMenuEnter(event) {
	if (event.key === "Enter") {
		event.preventDefault();
		toggleMegaMenu();
	}
});

$(".megamenu").on("keydown", function closeMegaMenuEsc(event) {
	if (event.key === "ArrowUp") {
		$(".megamenu .breadcrumb li:first-child a").focus();
	}
});

$(function () {
	var $breadcrumb = $("#header_breadcrumb ol");
	var $container = $(".megamenu .inner");

	function showMenuLevel($targetUl) {
		if ($targetUl && $targetUl.length) {
			var $catHref = $targetUl.find("a.category-label[href]");
			$targetUl.addClass("active");
			$targetUl.parents("li").addClass("grape");
			$targetUl.parents("ul").addClass("chosen");
			$catHref.attr("data-href", $catHref.attr("href")).removeAttr("href");
		}
	}

	function toggleMenuCategory(event, element) {
		var $element = $(element);

		if (!$element.attr("href")) {
			event.preventDefault();
			event.stopPropagation();

			var name = $element.data("name");
			var $parentLi = $element.closest("li");
			var $parentUl = $element.closest("ul");
			var $nextMenu = $parentLi.children(".menu-level");

			if ($nextMenu.length || $element.is("a, button")) {
				$breadcrumb.find(".breadcrumb-item").removeClass("active");
				var $last = $breadcrumb.find(".breadcrumb-item").last();

				if ($last.length && !$last.find("a").length) {
					var text = $last.text();
					$last.html('<a href="#">' + text + "</a>");
				}

				$breadcrumb.append('<li class="breadcrumb-item active" aria-current="page"><a href="' + $element.data("href") + '" tabindex="3">' + name + "</a></li>");
				showMenuLevel($nextMenu.length ? $nextMenu : null);
				$parentUl.addClass("chosen");
				$parentLi.addClass("grape");
				$element.attr("href", $element.data("href")).removeAttr("data-href");
			}
		}
	}

	$(".megamenu").on("click", ".category-label", function onCategoryClick(event) {
		toggleMenuCategory(event, this);
	});

	$(".category-label").on("keydown", function onCategoryKeydown(event) {
		if (event.key === "Enter") {
			toggleMenuCategory(event, this);
			$(".menu-level.active > li.has_children:first-child .category-label").first().focus();
		}
	});

	$breadcrumb.on("click", ".breadcrumb-item:not(.active) a", function onBreadcrumbClick(event) {
		event.preventDefault();

		$container.find(".menu-level").removeClass("active");
		$(".megamenu .inner").find(".grape").removeClass("grape");
		$(".megamenu .inner").find(".chosen").removeClass("chosen");

		var $clickedLi = $(this).closest("li");
		var index = $breadcrumb.find(".breadcrumb-item").index($clickedLi);
		var $items = $breadcrumb.find(".breadcrumb-item").slice(0, index + 1);

		$breadcrumb.empty().append($items);

		var $targetUl = null;

		$(".megamenu .inner a.category-label[href]").each(function allAttrs() {
			$(this).attr("data-href", $(this).attr("href")).removeAttr("href");
		});

		if (index === 0) {
			$targetUl = $container;
			$(".megamenu .inner").removeClass("chosen");
		} else {
			var names = $breadcrumb.find(".breadcrumb-item").slice(1, index + 1).map(function getName() {
				return $(this).text().trim();
			}).get();
			var $currentUl = $container;
			names.forEach(function iterateName(name) {
				var $currentLabel = $currentUl.find(".category-label").filter(function filterLabel() {
					return $(this).data("name") === name;
				}).first();
				var $nextMenu = $currentLabel.closest("li").children(".menu-level");
				$currentLabel.attr("data-href", $currentLabel.attr("href")).removeAttr("href");
				if ($nextMenu.length) {
					$currentUl = $nextMenu;
				}
			});
			$targetUl = $currentUl;
		}

		$(this).attr("href", $(this).data("href")).removeAttr("data-href");
		showMenuLevel($targetUl);
	});
});

$(".big_header .dropdown a.toggle").on("keydown", function activateDropdownMode(event) {
	if (event.key === "Enter") {
		event.preventDefault();
		$(".dropdown").removeClass("hover");
		$(this).closest(".dropdown").addClass("hover");
	}
});

$("body").on("keydown", function closeAllInstants(event) {
	if (event.key === "Escape") {
		$(".dropdown").removeClass("hover");
		$(".search-wrapper").removeClass("open");
		closeMegaMenu();
	}
});

$(".search-toggler").on("click", function toggleSearch() {
	$(".search-wrapper").toggleClass("open");
	var $searchInput = $(".search-wrapper .search-input-wrapper input").first();
	if ($searchInput.length) {
		$searchInput.trigger("focus");
	}
});

$(".dropdown.basket-dropdown").on("show.bs.dropdown", function closeSearch() {
	$(".search-wrapper").removeClass("open");
});

$(".search-toggler").on("keydown", function toggleSearchKeydown(event) {
	if (event.key === "Enter") {
		event.preventDefault();
		$(".search-wrapper").addClass("open");
		var $searchInput = $(".search-wrapper .search-input-wrapper input").first();
		if ($searchInput.length) {
			$searchInput.trigger("focus");
		}
	}
});

document.addEventListener("DOMContentLoaded", function onDomReady() {
	var dropdownItem = document.querySelector(".pages-dropdown");
	var toggleLink = dropdownItem ? dropdownItem.querySelector(".nav-link.toggle") : null;

	if (dropdownItem && toggleLink) {
		dropdownItem.addEventListener("mouseenter", function onMouseEnter() {
			toggleLink.setAttribute("aria-expanded", "true");
		});

		dropdownItem.addEventListener("mouseleave", function onMouseLeave() {
			toggleLink.setAttribute("aria-expanded", "false");
		});
	}

	var prevButtons = document.querySelectorAll('button[data-controls="prev"]');
	var nextButtons = document.querySelectorAll('button[data-controls="next"]');

	prevButtons.forEach(function setPrevLabel(button) {
		button.setAttribute("aria-label", "Previous");
	});

	nextButtons.forEach(function setNextLabel(button) {
		button.setAttribute("aria-label", "Next");
	});
});
