/* Help4 Builder Suite 1.3.316 | build e0bf22c8bac2c863f033078c97ae2049d42f0eab2d66bf4421fc207d92da6717 */
(function () {
    "use strict";

    var mobileContainmentQuery = window.matchMedia("(max-width: 782px)");
    var containmentTimer = 0;

    function isObservableNode(node) {
        return Boolean(node && typeof node.nodeType === "number");
    }

    function isVisibleElement(element) {
        var style;
        var rect;
        if (!(element instanceof HTMLElement) && !(element instanceof SVGElement)) {
            return false;
        }
        style = window.getComputedStyle(element);
        if (style.display === "none" || style.visibility === "hidden") {
            return false;
        }
        rect = element.getBoundingClientRect();
        return rect.width > 0 && rect.height > 0;
    }

    function clearMobileContainment(page) {
        page.querySelectorAll(".h4bs-mobile-contain-size, .h4bs-mobile-contain-shift").forEach(function (element) {
            element.classList.remove("h4bs-mobile-contain-size", "h4bs-mobile-contain-shift");
            element.style.removeProperty("--h4bs-mobile-contain-shift");
            element.removeAttribute("data-h4bs-mobile-contained");
        });
    }

    function hasOverflowOptOut(element, page) {
        var current = element;
        while (current && current !== page) {
            if (
                current.hasAttribute("data-h4bs-allow-mobile-overflow") ||
                current.hasAttribute("data-h4bs-mobile-scroll") ||
                current.classList.contains("h4bs-mobile-overflow-allowed")
            ) {
                return true;
            }
            current = current.parentElement;
        }
        return page.hasAttribute("data-h4bs-allow-mobile-overflow");
    }

    function hasContainingOverflowAncestor(element, page) {
        var current = element.parentElement;
        var overflowX;
        while (current && current !== page) {
            overflowX = window.getComputedStyle(current).overflowX;
            if (
                overflowX === "auto" ||
                overflowX === "scroll" ||
                overflowX === "hidden" ||
                overflowX === "clip"
            ) {
                return true;
            }
            current = current.parentElement;
        }
        return false;
    }

    function elementOverflowsBounds(element, leftBound, rightBound) {
        var rect = element.getBoundingClientRect();
        return rect.left < leftBound - 1 || rect.right > rightBound + 1 || rect.width > rightBound - leftBound + 1;
    }

    function repairMobileContainment(page) {
        var pageRect;
        var pageStyle;
        var leftBound;
        var rightBound;
        var candidates;
        var repaired = [];

        clearMobileContainment(page);
        if (! mobileContainmentQuery.matches || page.hasAttribute("data-h4bs-disable-mobile-containment")) {
            return;
        }

        pageRect = page.getBoundingClientRect();
        pageStyle = window.getComputedStyle(page);
        leftBound = Math.max(
            0,
            pageRect.left + parseFloat(pageStyle.paddingLeft || "0") + parseFloat(pageStyle.borderLeftWidth || "0")
        );
        rightBound = Math.min(
            window.innerWidth,
            pageRect.right - parseFloat(pageStyle.paddingRight || "0") - parseFloat(pageStyle.borderRightWidth || "0")
        );
        if (rightBound - leftBound < 1) {
            return;
        }

        candidates = Array.prototype.slice.call(page.querySelectorAll("*"));
        candidates.forEach(function (element) {
            var hasRepairedAncestor = repaired.some(function (ancestor) {
                return ancestor.contains(element);
            });
            var position;
            if (
                hasRepairedAncestor ||
                ! isVisibleElement(element) ||
                hasOverflowOptOut(element, page) ||
                hasContainingOverflowAncestor(element, page)
            ) {
                return;
            }

            position = window.getComputedStyle(element).position;
            if (position === "fixed" || ! elementOverflowsBounds(element, leftBound, rightBound)) {
                return;
            }

            element.classList.add("h4bs-mobile-contain-size");
            element.setAttribute("data-h4bs-mobile-contained", "size");
            repaired.push(element);
        });

        repaired.forEach(function (element) {
            var rect = element.getBoundingClientRect();
            var shift = 0;
            var state = "size";
            if (rect.right > rightBound + 1) {
                shift -= rect.right - rightBound;
            }
            if (rect.left + shift < leftBound - 1) {
                shift += leftBound - (rect.left + shift);
            }
            if (Math.abs(shift) >= 1) {
                element.style.setProperty("--h4bs-mobile-contain-shift", shift.toFixed(2) + "px");
                element.classList.add("h4bs-mobile-contain-shift");
                state += " shift";
            }
            element.setAttribute("data-h4bs-mobile-contained", state);
        });
    }

    function repairAllMobileContainment() {
        mobileContainmentRoots().forEach(repairMobileContainment);
    }

    function mobileContainmentRoots() {
        var roots = Array.prototype.slice.call(document.querySelectorAll(".h4bs-builder-page"));
        var fallback;
        if (roots.length === 0 && document.body.classList.contains("h4bs-builder-active")) {
            fallback = document.querySelector(".entry-content, .wp-block-post-content, article, main");
            if (fallback) {
                roots.push(fallback);
            }
        }
        roots.forEach(function (root) {
            root.classList.add("h4bs-mobile-containment-root");
        });
        return roots;
    }

    function scheduleMobileContainment() {
        window.clearTimeout(containmentTimer);
        containmentTimer = window.setTimeout(repairAllMobileContainment, 80);
    }

    function initMobileContainment() {
        var observer;
        repairAllMobileContainment();
        window.addEventListener("load", scheduleMobileContainment);
        window.addEventListener("resize", scheduleMobileContainment, { passive: true });
        document.addEventListener("h4bs:layout-change", scheduleMobileContainment);
        document.addEventListener("load", function (event) {
            if (event.target instanceof HTMLImageElement && event.target.closest(".h4bs-builder-page")) {
                scheduleMobileContainment();
            }
        }, true);
        if (typeof mobileContainmentQuery.addEventListener === "function") {
            mobileContainmentQuery.addEventListener("change", scheduleMobileContainment);
        } else if (typeof mobileContainmentQuery.addListener === "function") {
            mobileContainmentQuery.addListener(scheduleMobileContainment);
        }
        if (typeof MutationObserver === "function") {
            observer = new MutationObserver(scheduleMobileContainment);
            mobileContainmentRoots().forEach(function (page) {
                if (isObservableNode(page)) {
                    observer.observe(page, { childList: true, subtree: true });
                }
            });
        }
    }

    function closeMenu(nav, button, panel) {
        nav.classList.remove("is-menu-open");
        button.setAttribute("aria-expanded", "false");
        if (panel) {
            panel.hidden = true;
        }
    }

    function openMenu(nav, button, panel) {
        nav.classList.add("is-menu-open");
        button.setAttribute("aria-expanded", "true");
        if (panel) {
            panel.hidden = false;
        }
    }

    function initMenu(nav) {
        var button = nav.querySelector("[data-h4bs-menu-toggle]");
        var panel = nav.querySelector("[data-h4bs-menu-panel]");
        var mobileQuery = window.matchMedia("(max-width: 782px)");
        if (!button || !panel) {
            return;
        }

        function syncForViewport() {
            if (mobileQuery.matches) {
                closeMenu(nav, button, panel);
                return;
            }
            openMenu(nav, button, panel);
        }

        syncForViewport();
        if (typeof mobileQuery.addEventListener === "function") {
            mobileQuery.addEventListener("change", syncForViewport);
        } else if (typeof mobileQuery.addListener === "function") {
            mobileQuery.addListener(syncForViewport);
        }
        button.addEventListener("click", function () {
            if (nav.classList.contains("is-menu-open")) {
                closeMenu(nav, button, panel);
                return;
            }
            openMenu(nav, button, panel);
        });

        document.addEventListener("click", function (event) {
            if (!mobileQuery.matches) {
                return;
            }
            if (nav.contains(event.target)) {
                return;
            }
            closeMenu(nav, button, panel);
        });

        nav.addEventListener("keydown", function (event) {
            if (mobileQuery.matches && event.key === "Escape") {
                closeMenu(nav, button, panel);
                button.focus();
            }
        });
    }

    function activateMotion(element) {
        element.classList.add("is-h4bs-motion-active");
    }

    function initMotionTriggers() {
        var inViewElements = Array.prototype.slice.call(
            document.querySelectorAll(".h4bs-motion-trigger-in-view")
        );

        document
            .querySelectorAll(".h4bs-motion-trigger-interaction")
            .forEach(function (element) {
                var activate = function () {
                    activateMotion(element);
                    element.removeEventListener("click", activate);
                    element.removeEventListener("focusin", activate);
                    element.removeEventListener("pointerenter", activate);
                };
                element.addEventListener("click", activate);
                element.addEventListener("focusin", activate);
                element.addEventListener("pointerenter", activate);
            });

        if (inViewElements.length === 0) {
            return;
        }
        if (typeof IntersectionObserver !== "function") {
            inViewElements.forEach(activateMotion);
            return;
        }

        var observer = new IntersectionObserver(function (entries) {
            entries.forEach(function (entry) {
                if (! entry.isIntersecting) {
                    return;
                }
                activateMotion(entry.target);
                observer.unobserve(entry.target);
            });
        }, { threshold: 0.12 });
        inViewElements.forEach(function (element) {
            if (isObservableNode(element)) {
                observer.observe(element);
            }
        });
    }

    function normalizedCommerceCount(value, source) {
        var numeric;
        var items;
        var countKeys;
        var index;
        var key;
        if (typeof value === "number" || typeof value === "string") {
            numeric = Number(value);
            return Number.isFinite(numeric) ? Math.max(0, Math.floor(numeric)) : null;
        }
        if (Array.isArray(value)) {
            if (source === "notifications") {
                return value.filter(function (item) {
                    return !item || (item.read !== true && item.unread !== false);
                }).length;
            }
            return value.reduce(function (total, item) {
                var quantity = item && typeof item === "object"
                    ? Number(item.quantity || item.qty || 1)
                    : 1;
                return total + (Number.isFinite(quantity) ? Math.max(0, Math.floor(quantity)) : 1);
            }, 0);
        }
        if (!value || typeof value !== "object") {
            return null;
        }
        countKeys = ["count", "quantity", "total", "unread", "unread_count"];
        for (index = 0; index < countKeys.length; index += 1) {
            key = countKeys[index];
            if (Object.prototype.hasOwnProperty.call(value, key)) {
                numeric = Number(value[key]);
                if (Number.isFinite(numeric)) {
                    return Math.max(0, Math.floor(numeric));
                }
            }
        }
        items = value.items || value.entries || value.notifications;
        return typeof items !== "undefined"
            ? normalizedCommerceCount(items, source)
            : null;
    }

    function commerceIndicatorStoredCount(indicator) {
        var key = String(indicator.dataset.h4bsStorageKey || "");
        var raw;
        if (!key) {
            return null;
        }
        try {
            raw = window.localStorage.getItem(key);
            if (raw === null) {
                return null;
            }
            try {
                return normalizedCommerceCount(JSON.parse(raw), String(indicator.dataset.h4bsCountSource || ""));
            } catch (error) {
                return normalizedCommerceCount(raw, String(indicator.dataset.h4bsCountSource || ""));
            }
        } catch (error) {
            return null;
        }
    }

    function updateCommerceIndicator(indicator, rawCount) {
        var count = normalizedCommerceCount(rawCount, String(indicator.dataset.h4bsCountSource || ""));
        var showZero = String(indicator.dataset.h4bsShowZero || "0") === "1";
        var showQuantity = String(indicator.dataset.h4bsShowQuantity || "1") !== "0";
        var maxCount = Math.max(1, Number(indicator.dataset.h4bsMaxCount || 99) || 99);
        var label = String(indicator.dataset.h4bsLabel || "Commerce");
        var singular = String(indicator.dataset.h4bsUnitSingular || "item");
        var plural = String(indicator.dataset.h4bsUnitPlural || "items");
        var badge = indicator.querySelector("[data-h4bs-commerce-indicator-count]");
        var wrapper = indicator.closest(".h4bs-block-commerce_indicator");
        var visible;
        if (count === null) {
            count = normalizedCommerceCount(indicator.dataset.h4bsCount || 0, "");
        }
        count = count === null ? 0 : count;
        visible = count > 0 || showZero;
        indicator.dataset.h4bsCount = String(count);
        indicator.hidden = !visible;
        if (wrapper) {
            wrapper.classList.toggle("is-h4bs-count-hidden", !visible);
        }
        if (badge) {
            badge.hidden = !showQuantity;
            badge.textContent = count > maxCount ? String(maxCount) + "+" : String(count);
        }
        indicator.setAttribute(
            "aria-label",
            label + ": " + String(count) + " " + (count === 1 ? singular : plural)
        );
    }

    function updateCommerceIndicatorSource(source, count) {
        document.querySelectorAll("[data-h4bs-commerce-indicator]").forEach(function (indicator) {
            if (String(indicator.dataset.h4bsCountSource || "") === String(source || "")) {
                updateCommerceIndicator(indicator, count);
            }
        });
    }

    function initCommerceIndicators() {
        var indicators = Array.prototype.slice.call(
            document.querySelectorAll("[data-h4bs-commerce-indicator]")
        );
        if (indicators.length === 0) {
            return;
        }
        indicators.forEach(function (indicator) {
            var storedCount = commerceIndicatorStoredCount(indicator);
            updateCommerceIndicator(
                indicator,
                storedCount === null ? indicator.dataset.h4bsCount || 0 : storedCount
            );
        });
        document.addEventListener("h4bs:commerce-count", function (event) {
            var detail = event && event.detail && typeof event.detail === "object" ? event.detail : {};
            updateCommerceIndicatorSource(detail.source, detail.count);
        });
        document.addEventListener("h4bs:commerce-count-change", function (event) {
            var detail = event && event.detail && typeof event.detail === "object" ? event.detail : {};
            updateCommerceIndicatorSource(detail.source, detail.count);
        });
        window.addEventListener("storage", function (event) {
            indicators.forEach(function (indicator) {
                if (String(indicator.dataset.h4bsStorageKey || "") !== String(event.key || "")) {
                    return;
                }
                updateCommerceIndicator(
                    indicator,
                    commerceIndicatorStoredCount(indicator)
                );
            });
        });
        window.h4bsCommerceIndicators = {
            update: updateCommerceIndicatorSource,
            refresh: function () {
                indicators.forEach(function (indicator) {
                    var storedCount = commerceIndicatorStoredCount(indicator);
                    updateCommerceIndicator(
                        indicator,
                        storedCount === null ? indicator.dataset.h4bsCount || 0 : storedCount
                    );
                });
            },
        };
    }

    function initCollection(collection) {
        var items = Array.prototype.slice.call(collection.querySelectorAll("[data-h4bs-collection-item]"));
        var search = collection.querySelector("[data-h4bs-collection-search]");
        var filters = Array.prototype.slice.call(collection.querySelectorAll("[data-h4bs-collection-filter]"));
        var empty = collection.querySelector("[data-h4bs-collection-empty]");
        var status = collection.querySelector("[data-h4bs-collection-status]");
        var views = Array.prototype.slice.call(collection.querySelectorAll("[data-h4bs-collection-view]"));
        function update() {
            var query = search ? String(search.value || "").trim().toLowerCase() : "";
            var visible = 0;
            items.forEach(function (item) {
                var matches = query === "" || String(item.dataset.search || "").indexOf(query) >= 0;
                filters.forEach(function (filter) {
                    var key = String(filter.dataset.h4bsCollectionFilter || "");
                    var wanted = String(filter.value || "").toLowerCase();
                    var actual = String(item.getAttribute("data-filter-" + key) || "").toLowerCase();
                    if (wanted !== "" && actual !== wanted) matches = false;
                });
                item.hidden = !matches;
                if (matches) visible += 1;
            });
            if (empty) empty.hidden = visible !== 0;
            if (status) status.textContent = visible + (visible === 1 ? " item shown" : " items shown");
        }
        if (search) search.addEventListener("input", update);
        filters.forEach(function (filter) { filter.addEventListener("change", update); });
        views.forEach(function (button) {
            button.addEventListener("click", function () {
                var view = String(button.dataset.h4bsCollectionView || "grid");
                collection.classList.toggle("is-grid", view === "grid");
                collection.classList.toggle("is-list", view === "list");
                views.forEach(function (other) { other.setAttribute("aria-pressed", other === button ? "true" : "false"); });
            });
        });
        update();
    }

    function init() {
        document
            .querySelectorAll("[data-h4bs-builder-menu].has-mobile-hamburger")
            .forEach(initMenu);
        initCommerceIndicators();
        initMotionTriggers();
        initMobileContainment();
        document.querySelectorAll("[data-h4bs-collection]").forEach(initCollection);
    }

    if (document.readyState === "loading") {
        document.addEventListener("DOMContentLoaded", init, { once: true });
    } else {
        init();
    }
})();
