jQuery(document).ready(function($) {
    function initFaqAccordions() {
        $('.qcld-faq-accordion-wrapper').each(function() {
            var $wrapper = $(this);
            
            // Avoid double initialization
            if ($wrapper.data('initialized')) return;
            $wrapper.data('initialized', true);

            $wrapper.on('click', '.qcld-faq-accordion-header', function() {
                var $item = $(this).parent();
                var $content = $item.find('.qcld-faq-accordion-content');
                var isActive = $item.hasClass('is-active');
                
                // Close all other items in this specific accordion
                $wrapper.find('.qcld-faq-accordion-item').removeClass('is-active');
                $wrapper.find('.qcld-faq-accordion-content').slideUp(300);
                
                if (!isActive) {
                    $item.addClass('is-active');
                    $content.stop(true, true).slideDown(300);
                }
            });

            // Accessibility support
            $wrapper.on('keydown', '.qcld-faq-accordion-header', function(e) {
                if (e.key === 'Enter' || e.key === ' ') {
                    e.preventDefault();
                    $(this).trigger('click');
                }
            });
        });
    }

    initFaqAccordions();
});
