"use strict";
/* Namespace ------------------------------------------------------- */
var UnitedBlog = UnitedBlog || {};
/* Preloader ------------------------------------------------------- */
UnitedBlog.PreLoader = {
    init: function () {
        const preloader = document.querySelector("#united-preloader");
        if (preloader) {
            document.body.classList.add("page-loaded");
        }
    },
};
/* Background Image ------------------------------------------------ */
UnitedBlog.setBackgroundImage = {
    init: function () {
        // Set background images based on data-bg attribute
        const pageSections = document.querySelectorAll(".data-bg");
        pageSections.forEach((section) => {
            const background = section.getAttribute("data-background");
            if (background) {
                section.style.backgroundImage = `url(${background})`;
            }
        });
    },
};
/* Custom Cursor --------------------------------------------------- */
UnitedBlog.CustomCursor = {
    init: function () {
        const myCursor = document.querySelector('.theme-custom-cursor');
        if (myCursor) {
            const body = document.body;
            const cursorPrimary = document.querySelector('.theme-cursor-primary');
            const cursorSecondary = document.querySelector('.theme-cursor-secondary');
            let mouseX = 0, mouseY = 0, isHovering = false;
            // Update cursor positions on mouse movement
            window.addEventListener('mousemove', (event) => {
                if (!isHovering) {
                    cursorPrimary.style.transform = `translate(${event.clientX}px, ${event.clientY}px)`;
                }
                cursorSecondary.style.transform = `translate(${event.clientX}px, ${event.clientY}px)`;
                mouseX = event.clientX;
                mouseY = event.clientY;
            });
            // Add hover effect on specified elements
            body.addEventListener('mouseenter', (event) => {
                if (event.target.matches('a, button, input[type="submit"], .united-swiper-button, .cursor-pointer')) {
                    cursorPrimary.classList.add('cursor-hover');
                    cursorSecondary.classList.add('cursor-hover');
                    isHovering = true;
                }
            }, true);
            // Remove hover effect when leaving specified elements
            body.addEventListener('mouseleave', (event) => {
                if (event.target.matches('a, button, input[type="submit"], .united-swiper-button, .cursor-pointer')) {
                    cursorPrimary.classList.remove('cursor-hover');
                    cursorSecondary.classList.remove('cursor-hover');
                    isHovering = false;
                }
            }, true);
            // Make the cursors visible
            cursorPrimary.style.visibility = "visible";
            cursorSecondary.style.visibility = "visible";
        }
    },
};
/* Slider
 **-----------------------------------------------------*/
UnitedBlog.swiper = {
    init: function () {
        this.tickerSlider();
        this.bannerSlider();
        this.widgetSlider();
    },
    tickerSlider: function () {
        let tickerWrapper = document.querySelector(".united-ticker-init");
        if (tickerWrapper) {
            let tickerDefaultOptions = {
                scrollbar: false,
                direction: 'horizontal',
                slidesPerView: 'auto',
                initialSlide: 0,
                loop: true,
                parallax: false,
                spaceBetween: 0,
                allowTouchMove: false,
                waitForTransition: false,
                watchSlidesProgress: false,
                autoplay: {delay: 0, disableOnInteraction: false},
            };
            let tickerDataOptions = {};
            let tickerData = tickerWrapper.getAttribute("data-ticker") || "";
            if (tickerData) {
                tickerDataOptions = JSON.parse(tickerData);
            }
            let tickerOptions = {
                ...tickerDefaultOptions,
                ...tickerDataOptions,
            };
            let swiper = new Swiper(tickerWrapper, tickerOptions);
        }
    },
    bannerSlider: function () {
        let sliderWrapper = document.querySelector(".united-banner-init");
        if (sliderWrapper) {
            let bannerDefaultOptions = {
                loop: true,
            };
            let bannerDataOptions = {};
            let bannerData = sliderWrapper.getAttribute("data-banner") || "";
            if (bannerData) {
                bannerDataOptions = JSON.parse(bannerData);
            }
            let sliderOptions = {
                ...bannerDefaultOptions,
                ...bannerDataOptions,
            };
            let swiper = new Swiper(sliderWrapper, sliderOptions);
        }
    },
    widgetSlider: function () {
        let sliderWrappers = document.querySelectorAll(".united-swiper-js .swiper");
        if (sliderWrappers) {
            sliderWrappers.forEach(function (item) {
                let parentWrapper = item.parentNode;
                let navNext = parentWrapper.querySelector(".swiper-button-next");
                let navPrev = parentWrapper.querySelector(".swiper-button-prev");
                let paginate = parentWrapper.querySelector(".swiper-pagination");
                let defaultOptions = {
                    loop: true,
                    slidesPerView: 1,
                    lazy: true, // Correct Swiper's lazy loading option
                    navigation: {
                        nextEl: navNext,
                        prevEl: navPrev,
                    },
                    pagination: {
                        el: paginate,
                        clickable: true,
                    },
                };
                let dataOptions = {};
                let data = item.getAttribute("data-slider") || "";
                if (data) {
                    dataOptions = JSON.parse(data);
                }
                let sliderOptions = {
                    ...defaultOptions,
                    ...dataOptions,
                };
                let swiper = new Swiper(item, sliderOptions);
                // Adjust slidesPerView for smaller screens
                let containerWidth = item.clientWidth;
                if (containerWidth < 500) {
                    swiper.params.slidesPerView = 1;
                    swiper.update();
                }
            });
        }
    },
};
/* Tabs
 **-----------------------------------------------------*/
UnitedBlog.tabs = {
    init: function () {
        let tabLinks = document.querySelectorAll("[data-toggle='united-data-tab']");
        if (tabLinks) {
            tabLinks.forEach(function (tabLink) {
                tabLink.addEventListener("click", function (e) {
                    e.preventDefault();
                    let tabHeadings = [...tabLink.parentNode.children];
                    let tabContents = [
                        ...tabLink.parentNode.nextElementSibling.children,
                    ];
                    tabHeadings.forEach((tabLink) => {
                        tabLink.classList.remove("active");
                        tabLink.setAttribute("aria-selected", "false");
                    });
                    tabContents.forEach((tabContent) => {
                        tabContent.classList.remove("active");
                    });
                    let selectedTabId = tabLink.getAttribute("aria-controls");
                    let selectedContentTab =
                        document.getElementById(selectedTabId);
                    tabLink.classList.add("active");
                    tabLink.setAttribute("aria-selected", "true");
                    selectedContentTab.classList.add("active");
                });
            });
        }
    },
};
/* Scroll to Top --------------------------------------------------- */
UnitedBlog.ScrollToTop = {
    init: function () {
        const scrollUpButton = document.querySelector('.scroll-up');
        // Show or hide the button based on scroll position
        window.addEventListener('scroll', function () {
            if (window.scrollY > 100) {
                scrollUpButton.style.display = 'block';
                scrollUpButton.style.opacity = '1';
            } else {
                scrollUpButton.style.opacity = '0';
                scrollUpButton.style.display = 'none';
            }
        });
        // Scroll to top when the button is clicked
        scrollUpButton.addEventListener('click', function (event) {
            event.preventDefault();
            window.scrollTo({
                top: 0,
                behavior: 'smooth' // Smooth scrolling effect
            });
        });
    }
};
UnitedBlog.ProgressBar = {
    init: function () {
        const progressBar = document.getElementById('united-progressbar');
        // Check if progressBar exists before proceeding
        if (progressBar) {
            const totalHeight = document.body.scrollHeight - window.innerHeight;
            const scrollPosition = window.scrollY;
            const scrollPercentage = (scrollPosition / totalHeight) * 100;
            progressBar.style.width = scrollPercentage + '%';
            // Update progress on scroll
            window.addEventListener('scroll', function () {
                const scrollPosition = window.scrollY;
                const scrollPercentage = (scrollPosition / totalHeight) * 100;
                progressBar.style.width = scrollPercentage + '%';
            });
        }
    },
};
UnitedBlog.stickyHeader = {
    init: function () {
        // Select the element with the class 'united-single-affix'
        var affixElement = document.querySelector('.united-single-affix');
        // Check if the element exists
        if (affixElement) {
            // Function to toggle the 'stick-it' class based on scroll position
            function toggleStickClass() {
                if (window.scrollY > 80) {
                    affixElement.classList.add('stick-it');
                } else {
                    affixElement.classList.remove('stick-it');
                }
            }

            // Initial check when the page loads
            toggleStickClass();
            // Add scroll event listener to the window
            window.addEventListener('scroll', toggleStickClass);
        }
    },
};
UnitedBlog.stickySocial = {
    init: function () {
        const shareButton = document.querySelector('.social-share-title');
        const shareContent = document.querySelector('.social-share-content');
        if (shareButton && shareContent) {
            shareButton.addEventListener('click', function () {
                shareContent.classList.toggle('active');
            });
            document.addEventListener('click', function (event) {
                if (!shareButton.contains(event.target) && !shareContent.contains(event.target)) {
                    shareContent.classList.remove('active');
                }
            });
        }
    },
};

/* DOM Ready Handler ----------------------------------------------- */
function UnitedBlogDomReady(fn) {
    if (typeof fn !== "function") {
        return;
    }
    if (document.readyState === "interactive" || document.readyState === "complete") {
        return fn();
    }
    document.addEventListener("DOMContentLoaded", fn);
}

/* Initialize Functions -------------------------------------------- */
UnitedBlogDomReady(function () {
    UnitedBlog.setBackgroundImage.init();
    UnitedBlog.CustomCursor.init();
    UnitedBlog.swiper.init();
    UnitedBlog.tabs.init();
    UnitedBlog.ScrollToTop.init();
    UnitedBlog.stickyHeader.init();
    UnitedBlog.stickySocial.init();
});
window.addEventListener("load", function () {
    UnitedBlog.PreLoader.init();
});
// Scroll event for other features
window.addEventListener("scroll", function () {
    UnitedBlog.ProgressBar.init();
});