/**
 * Blog Over Ticker JS
 * EXACT copy of Bloglo ticker implementation using jQuery Marquee
 * With Customizer partial refresh support
 *
 * @package Blog_Over
 */

(function ($) {

    // Track manual pause state (when user clicks pause button)
    var isManuallyPaused = false;

    // Initialize or reinitialize the ticker marquee
    function initTicker() {
        let htc = $(".ticker-slider-items");

        if (htc.length) {
            // Reset manual pause state on init
            isManuallyPaused = false;

            // Destroy existing marquee if any (for reinit)
            let existingMarquee = htc.find(".ticker-slider-wrap");
            if (existingMarquee.data('marquee')) {
                existingMarquee.marquee('destroy');
            }

            // Get settings from data attributes
            let speed = htc.data('speed') || 50;
            let direction = htc.data('direction') || 'left';
            let pauseOnHover = htc.data('pauseonhover') === true || htc.data('pauseonhover') === 'true';

            // RTL support
            if ($("body").hasClass("rtl")) {
                direction = (direction === 'left') ? 'right' : 'left';
            }

            // Initialize marquee WITHOUT built-in pauseOnHover (we'll handle it manually)
            let htcM = htc.find(".ticker-slider-wrap").marquee({
                speed: speed,
                duration: 14000,
                gap: 0,
                delayBeforeStart: 0,
                direction: direction,
                duplicated: true,
                startVisible: true,
                pauseOnHover: false  // We handle hover manually to respect manual pause
            });

            // Remove previous handlers to avoid duplicates
            htc.off("click", ".ticker-slider-pause");
            htc.find(".ticker-slider-box").off("mouseenter mouseleave");

            // Pause button click handler
            htc.on("click", ".ticker-slider-pause", function () {
                isManuallyPaused = !isManuallyPaused;
                $(this).find("i").toggleClass("fa-pause fa-play");
                htcM.marquee("toggle");
            });

            // Custom hover handlers (only if pauseOnHover is enabled)
            if (pauseOnHover) {
                htc.find(".ticker-slider-box").on("mouseenter", function () {
                    // Only pause on hover if not already manually paused
                    if (!isManuallyPaused) {
                        htcM.marquee("pause");
                    }
                }).on("mouseleave", function () {
                    // Only resume on mouse leave if not manually paused
                    if (!isManuallyPaused) {
                        htcM.marquee("resume");
                    }
                });
            }
        }
    }

    // Initialize on document ready
    $(document).ready(function () {
        initTicker();

        // Re-initialize after customizer partial refresh
        if (typeof wp !== 'undefined' && wp.customize && wp.customize.selectiveRefresh) {
            wp.customize.selectiveRefresh.bind('partial-content-rendered', function (placement) {
                // Check if this is a ticker-related partial
                if (placement.partial.id.indexOf('ticker') !== -1 ||
                    (placement.container && placement.container.closest('.blog-over-ticker-slider').length)) {
                    // Slight delay to ensure DOM and styles are fully applied
                    setTimeout(function () {
                        initTicker();
                    }, 250);
                }
            });
        }
    });

    // Expose globally for manual refresh if needed
    window.novaInitTicker = initTicker;

})(jQuery);
