// main.js
jQuery(document).ready(function($) {
    $('.rzcps-container').each(function () {
        const container = $(this);
        const marquee = container.find('.rzcps');
        const isVertical = container.hasClass('vertical');
        const isMarqee = container.hasClass('marquee');
        const isRTL = container.hasClass('rtl');

        if (!isMarqee) return;

        const items = marquee.find('.item');
        const speed = parseInt(container.data('speed')) || 50;
        const visible = isVertical ? parseInt(container.data('visible')) || 3 : 1;

        // Calculate and set height dynamically (vertical + fade or vertical + marquee)
        if (isVertical) {
            let totalHeight = 0;
            items.slice(0, visible).css({ display: 'block', position: 'absolute', visibility: 'hidden' });
            items.slice(0, visible).each(function () {
                totalHeight += $(this).outerHeight(true) + 10;
            });
            items.css({ display: '', position: '', visibility: '' }); // reset
            container.css('height', totalHeight + 'px');
        }

        if (!isVertical) {
            items.each(function () {
                $(this).css({
                    flexShrink: 0,
                    width: 'auto'
                });
            });
        }        

        if(isMarqee){
            // Marquee Scroll Mode
            let normalizedSpeed = Math.min(Math.max(50 / speed, 0.2), 3);
            let scrollAmount = 0;
            let isPaused = false;

            marquee.append(marquee.html());

            function scrollUpdate() {
                if (!isPaused) {
                    scrollAmount += isRTL && !isVertical ? normalizedSpeed : -normalizedSpeed;
                    if (isVertical) {
                        marquee.css('transform', 'translateY(' + scrollAmount + 'px)');
                        if (Math.abs(scrollAmount) >= marquee[0].scrollHeight / 2) scrollAmount = 0;
                    } else {
                        marquee.css('transform', 'translateX(' + scrollAmount + 'px)');
                        if (Math.abs(scrollAmount) >= marquee[0].scrollWidth / 2) scrollAmount = 0;
                    }
                }
                requestAnimationFrame(scrollUpdate);
            }

            container.hover(() => isPaused = true, () => isPaused = false);
            scrollUpdate();
        }

    });
});
