(function($) {
    "use strict";

    // Open search overlay
    $('.search-toggle').on('click', function(e) {
        e.preventDefault();
        $('.bexplore-search-overlay').addClass('active');
        $('.bexplore-search-overlay .search-field').val('').focus();
        $('body').css('overflow', 'hidden');
    });

    // Close search overlay
    $('.search-close').on('click', function(e) {
        e.preventDefault();
        closeSearchOverlay();
    });

    // Close on overlay click (outside search form)
    $('.bexplore-search-overlay').on('click', function(e) {
        if ($(e.target).hasClass('bexplore-search-overlay')) {
            closeSearchOverlay();
        }
    });

    // Close on ESC key
    $(document).on('keydown', function(e) {
        if (e.keyCode === 27) {
            closeSearchOverlay();
        }
    });

    function closeSearchOverlay() {
        $('.bexplore-search-overlay').removeClass('active');
        $('body').css('overflow', '');
    }

    // Preloader
    $(window).on('load', function() {
        $("#loader").delay(1000).fadeOut(500);
    });

    // Hero slideshow
    var swiper = new Swiper('.tbSwiper', {
        spaceBetween: 30,
        effect: "fade",
        pagination: false,
        loop: true,
        autoplay: false,
        paginationClickable: true,
        navigation: {
            nextEl: ".slideshow-navigation-button.next",
            prevEl: ".slideshow-navigation-button.prev",
        },
    });

    // ── Masonry 3-column grid ─────────────────────────────────
    function initMasonry() {
        var $grid = $('.bexplore-masonry-grid');

        if ( ! $grid.length ) {
            return;
        }

        // Wait for all images to load so Masonry gets correct item heights
        $grid.imagesLoaded( function() {
            $grid.masonry({
                itemSelector : '.bexplore-masonry-item',
                columnWidth  : '.bexplore-masonry-sizer',
                percentPosition: true,
                gutter       : '.bexplore-masonry-gutter',
            });
        });
    }

    // Init on DOM ready
    initMasonry();

    // Re-layout after images inside lazy-loaded or dynamically injected posts finish loading
    $(document).on('post-load', function() {
        initMasonry();
    });

    // ── Copy Link button ──────────────────────────────────────
    $(document).on('click', '.js-copy-link', function() {
        var $btn   = $(this);
        var url    = $btn.data('url');

        // Use Clipboard API if available, fall back to execCommand
        if (navigator.clipboard && window.isSecureContext) {
            navigator.clipboard.writeText(url).then(function() {
                showCopied($btn);
            });
        } else {
            var $temp = $('<input>');
            $('body').append($temp);
            $temp.val(url).select();
            document.execCommand('copy');
            $temp.remove();
            showCopied($btn);
        }
    });

    function showCopied($btn) {
        var $iconCopy  = $btn.find('.icon-copy');
        var $iconCheck = $btn.find('.icon-check');
        var $label     = $btn.find('.copy-label');

        $btn.addClass('copied');
        $iconCopy.hide();
        $iconCheck.show();
        $label.text('Copied!');

        setTimeout(function() {
            $btn.removeClass('copied');
            $iconCopy.show();
            $iconCheck.hide();
            $label.text('Copy Link');
        }, 2200);
    }

})(jQuery);