(function () {
    "use strict";

    function ready(callback) {
        if (document.readyState === "loading") {
            document.addEventListener("DOMContentLoaded", callback, { once: true });
            return;
        }
        callback();
    }

    function mobileMenuIsActive() {
        return window.matchMedia && window.matchMedia("(max-width: 860px)").matches;
    }

    function dropdownTrigger(item) {
        return Array.prototype.find.call(item.children, function (child) {
            return child.matches && child.matches("a[aria-expanded]");
        }) || null;
    }

    function closeDropdowns(header) {
        header.querySelectorAll(".h4-global-header__menu-item--dropdown.is-open").forEach(function (item) {
            var link = dropdownTrigger(item);
            item.classList.remove("is-open");
            if (link) {
                link.setAttribute("aria-expanded", "false");
            }
        });
    }

    function closeHeader(header, restoreFocus) {
        var toggle = header.querySelector(".h4-global-header__menu-toggle");
        header.classList.remove("is-open");
        if (toggle) {
            toggle.setAttribute("aria-expanded", "false");
            if (restoreFocus) {
                toggle.focus();
            }
        }
        closeDropdowns(header);
    }

    function initHeader(header, index) {
        var toggle = header.querySelector(".h4-global-header__menu-toggle");
        var nav = header.querySelector(".h4-global-header__nav");
        if (! toggle || ! nav || header.dataset.h4bsSiteNavReady === "1") {
            return;
        }

        header.dataset.h4bsSiteNavReady = "1";
        if (! nav.id) {
            nav.id = "h4-global-header-menu-" + index;
        }
        toggle.setAttribute("aria-controls", nav.id);
        toggle.setAttribute("aria-expanded", "false");

        toggle.addEventListener("click", function () {
            var open = ! header.classList.contains("is-open");
            header.classList.toggle("is-open", open);
            toggle.setAttribute("aria-expanded", open ? "true" : "false");
            if (! open) {
                closeDropdowns(header);
            }
        });

        header.querySelectorAll(".h4-global-header__menu-item--dropdown").forEach(function (item) {
            var link = dropdownTrigger(item);
            if (! link) {
                return;
            }
            link.addEventListener("click", function (event) {
                if (! mobileMenuIsActive() || item.classList.contains("is-open")) {
                    return;
                }
                event.preventDefault();
                closeDropdowns(header);
                item.classList.add("is-open");
                link.setAttribute("aria-expanded", "true");
            });
        });
    }

    ready(function () {
        var mobileMenuQuery = window.matchMedia ? window.matchMedia("(max-width: 860px)") : null;

        function syncDesktopState() {
            if (mobileMenuQuery ? mobileMenuQuery.matches : mobileMenuIsActive()) {
                return;
            }
            document.querySelectorAll(".h4-global-header.is-open").forEach(function (header) {
                closeHeader(header, false);
            });
        }

        document.querySelectorAll(".h4-global-header").forEach(initHeader);

        document.addEventListener("click", function (event) {
            document.querySelectorAll(".h4-global-header.is-open").forEach(function (header) {
                if (! header.contains(event.target)) {
                    closeHeader(header, false);
                }
            });
        });

        document.addEventListener("keydown", function (event) {
            if (event.key !== "Escape") {
                return;
            }
            document.querySelectorAll(".h4-global-header.is-open").forEach(function (header) {
                closeHeader(header, true);
            });
        });

        window.addEventListener("resize", syncDesktopState);
        if (mobileMenuQuery && mobileMenuQuery.addEventListener) {
            mobileMenuQuery.addEventListener("change", syncDesktopState);
        }
    });
}());
