(function($) {
    // Function to initialize a single Didak Vis Mere instance
    function initDidakVisMere($wrapper) {
        var $content = $wrapper.find('.didak-vis-mere-content');
        var $button = $wrapper.find('.didak-vis-mere-knap');
        var defaultHeight = $content.data('default-height');
        var numericDefaultHeight = parseInt(defaultHeight);
        var isInitialized = false;

        // Function to check content height and update UI
        function updateUI(force = false) {
            // If not forced and already initialized, return
            if (!force && isInitialized) return;

            var contentHeight = $content[0].scrollHeight;
            
            // Always start with content hidden
            $content.css('max-height', defaultHeight);
            $button.text($button.data('text-more'));

            // Check if content needs 'vis mere' functionality
            if (contentHeight > numericDefaultHeight) {
                $button.show();
            } else {
                $content.css('max-height', '');
                $button.hide();
            }

            isInitialized = true;
        }

        // Initial UI update
        updateUI(true);

        // Remove any existing click handlers to prevent duplicates
        $button.off('click.didakVisMere');

        // Add click handler
        $button.on('click.didakVisMere', function() {
            var textMore = $button.data('text-more');
            var textLess = $button.data('text-less');
            
            if ($content.hasClass('expanded')) {
                $content.removeClass('expanded').css('max-height', defaultHeight);
                $button.text(textMore);
            } else {
                $content.addClass('expanded').css('max-height', '');
                $button.text(textLess);
            }
        });

        // Store the updateUI function on the wrapper for later use
        $wrapper.data('updateUI', updateUI);
    }

    // Function to initialize all Didak Vis Mere instances
    function initAllDidakVisMere() {
        $('.didak-vis-mere-wrapper').each(function() {
            initDidakVisMere($(this));
        });
    }

    // Initialize all Didak Vis Mere instances on document ready
    $(document).ready(function() {
        initAllDidakVisMere();
    });

    // Re-initialize on window resize
    $(window).on('resize', function() {
        $('.didak-vis-mere-wrapper').each(function() {
            var updateUI = $(this).data('updateUI');
            if (typeof updateUI === 'function') {
                updateUI(true);
            }
        });
    });

    // Hook for dynamically added Didak Vis Mere instances
    $(document).on('didakVisMereAdded', function(e, $newContent) {
        if ($newContent) {
            $newContent.find('.didak-vis-mere-wrapper').each(function() {
                initDidakVisMere($(this));
            });
        } else {
            initAllDidakVisMere();
        }
    });

    // Integration with Didak Skyder
    function initDidakSkyderIntegration() {
        $('.didak-skyder').each(function() {
            var $skyder = $(this);
            var $contents = $skyder.find('> .didak-skyder-left-content > .didak-skyder-content-wrapper > .didak-skyder-content');
            
            $contents.on('addClass', function() {
                if ($(this).hasClass('active')) {
                    $(this).find('.didak-vis-mere-wrapper').each(function() {
                        var updateUI = $(this).data('updateUI');
                        if (typeof updateUI === 'function') {
                            updateUI(true);
                        }
                    });
                }
            });
        });
    }

    // Integration with Didak Tabs
    function initDidakTabsIntegration() {
        $('.didak-tabs').each(function() {
            var $tabSet = $(this);
            var $tabsNav = $tabSet.find('.didak-tabs-nav');
            
            $tabsNav.on('click', 'a', function() {
                var target = $(this).attr('href');
                setTimeout(function() {
                    $(target).find('.didak-vis-mere-wrapper').each(function() {
                        var updateUI = $(this).data('updateUI');
                        if (typeof updateUI === 'function') {
                            updateUI(true);
                        }
                    });
                }, 0);
            });
        });
    }

    // Initialize Didak Skyder and Didak Tabs integration
    $(document).ready(function() {
        initDidakSkyderIntegration();
        initDidakTabsIntegration();
    });

})(jQuery);