; (function ($) {

    $(document).ready(function () {

        // Initialize frmx-nice-select on all bbPress/Forumax select elements
        if (typeof $.fn.frmxNiceSelect === 'function') {
            // Initialize on all forum form selects
            $('.bbp-form select, .bbp-topic-form select, .bbp-reply-form select, .bbp-forum-form select').frmxNiceSelect();

            // Initialize on specific select elements (only if they are actual select elements)
            $('select#bbp_forum_id, select#bbp_stick_topic_select, select#bbp_topic_status_select, select#sort-replies').frmxNiceSelect();
            $('select#bbp_destination_topic, select#bbp_forum_parent_id, select#bbp_forum_status, select#bbp_forum_visibility').frmxNiceSelect();

            // Initialize on admin dropdown menus (only select elements)
            $('select.bbp-admin-dropdown-menu').frmxNiceSelect();
        }

        // Toggle dropdown - using frmx- prefixed classes to avoid theme conflicts
        $('.forumax-sort-lists .frmx-dropdown-toggle').on('click', function (e) {
            e.preventDefault();
            e.stopPropagation(); // prevent document click

            const $currentMenu = $(this).siblings('.frmx-dropdown-menu');

            // Close all others
            $('.forumax-sort-lists .frmx-dropdown-menu').not($currentMenu).removeClass('frmx-show');

            // Toggle current
            $currentMenu.toggleClass('frmx-show');
            
            // Update aria-expanded attribute
            $(this).attr('aria-expanded', $currentMenu.hasClass('frmx-show'));
        });

        // Allow clicks inside dropdown (including links)
        $('.forumax-sort-lists .frmx-dropdown-menu').on('click', function (e) {
            // Only prevent propagation if not clicking a link
            if (!$(e.target).is('a')) {
                e.stopPropagation();
            }
        });

        // Click outside → close all dropdowns
        $(document).on('click', function () {
            $('.forumax-sort-lists .frmx-dropdown-menu').removeClass('frmx-show');
            $('.forumax-sort-lists .frmx-dropdown-toggle').attr('aria-expanded', 'false');
        });

        // Forumax Social Share Modal
        $(document).on('click', '.modal-toggle[data-id="forumax_share_modal"]', function (e) {
            e.preventDefault();
            $('#forumax_share_modal').fadeIn();
        });

        $(document).on('click', '#forumax_share_modal .forumax-close, #forumax_share_modal .forumax-modal-overlay', function () {
            $('#forumax_share_modal').fadeOut();
        });

        // Forumax Copy Link Logic
        $('#forumax_share_modal .share-this-doc').on('click', function () {
            var copyText = $(this).closest('.copy-url-wrap').find('input.word-wrap');
            var $sourceToast = $(this).siblings('.forumax-link-copied-wrap');

            // Remove any existing active toasts to prevent duplicates
            $('.forumax-link-copied-wrap.active-body-toast').remove();

            // Clone to body to escape modal stacking context
            var $toast = $sourceToast.clone();
            $toast.addClass('active-body-toast');
            $toast.appendTo('body');

            copyText.select();
            document.execCommand("copy");

            // Feedback: Show toast
            $toast.fadeIn().css('display', 'flex');

            // Hide after 3 seconds AND remove
            setTimeout(function () {
                $toast.fadeOut(function () {
                    $(this).remove();
                });
            }, 3000);
        });

        // Close Toast manually
        $('body').on('click', '.forumax-link-copied-wrap .close-notice', function () {
            $(this).closest('.forumax-link-copied-wrap').fadeOut(function () {
                $(this).remove();
            });
        });

        // Open specific social links in a popup window
        $('#forumax_share_modal .social-links a').on('click', function (e) {
            var href = $(this).attr('href');
            // If it's a mailto link, let it behave normally
            if (href.indexOf('mailto:') === 0) {
                return;
            }
            e.preventDefault();
            var width = 600;
            var height = 400;
            var left = (screen.width - width) / 2;
            var top = (screen.height - height) / 2;
            window.open(href, 'sharer', 'toolbar=0,status=0,width=' + width + ',height=' + height + ',top=' + top + ',left=' + left);
        });
    });

})(jQuery);