// MENU
$(' #mainMenu nav > ul > li').click(function(){
    $(' #mainMenu nav > ul > li.active').removeClass('active');
    $(this).addClass('active');
});

// Language
$(' .top-language li.list-lang ').click(function(){
    $(' .top-language li.list-lang.active').removeClass('active');
    $(this).addClass('active');
});

function initNewProductsScrollZoom() {
    var section = document.querySelector('.sect-newproducts');
    if (!section) return;

    var slides = section.querySelectorAll('.slide-newproducts .item');
    if (!slides.length) return;

    var lastScrollY = window.pageYOffset || document.documentElement.scrollTop || 0;
    var zoomPercent = 100;
    var minZoom = 100;
    var maxZoom = 165;
    var wheelStrength = .12;
    var ticking = false;

    function applyBackgroundZoom() {
        var backgroundSize = zoomPercent <= 100 ? 'cover' : zoomPercent.toFixed(2) + '%';

        slides.forEach(function(slide) {
            slide.style.backgroundSize = backgroundSize;
        });
    }

    function updateBackgroundZoom() {
        var currentScrollY = window.pageYOffset || document.documentElement.scrollTop || 0;
        var rect = section.getBoundingClientRect();
        var viewportHeight = window.innerHeight || document.documentElement.clientHeight;
        var isScrollingDown = currentScrollY > lastScrollY;
        var isSectionVisible = rect.bottom > 0 && rect.top < viewportHeight;

        if (!isSectionVisible) {
            zoomPercent = minZoom;
        } else if (!isScrollingDown) {
            zoomPercent = Math.max(minZoom, zoomPercent - 6);
        }

        applyBackgroundZoom();

        lastScrollY = Math.max(currentScrollY, 0);
        ticking = false;
    }

    function requestUpdate() {
        if (ticking) return;

        ticking = true;
        window.requestAnimationFrame(updateBackgroundZoom);
    }

    function handleWheel(event) {
        var rect = section.getBoundingClientRect();
        var viewportHeight = window.innerHeight || document.documentElement.clientHeight;
        var isSectionVisible = rect.bottom > 0 && rect.top < viewportHeight;

        if (!isSectionVisible) return;

        if (event.deltaY > 0) {
            zoomPercent = Math.min(maxZoom, zoomPercent + Math.abs(event.deltaY) * wheelStrength);
        } else if (event.deltaY < 0) {
            zoomPercent = Math.max(minZoom, zoomPercent - Math.abs(event.deltaY) * wheelStrength);
        }

        applyBackgroundZoom();
    }

    window.addEventListener('scroll', requestUpdate, { passive: true });
    window.addEventListener('wheel', handleWheel, { passive: true });
    window.addEventListener('resize', requestUpdate);
    applyBackgroundZoom();
}

initNewProductsScrollZoom();

function initClipTiktokEmbeds() {
    var embeds = document.querySelectorAll('.sect-clip .tiktok-embed');
    if (!embeds.length) return;

    function renderEmbeds() {
        if (window.tiktokEmbed && window.tiktokEmbed.lib && window.tiktokEmbed.lib.render) {
            window.tiktokEmbed.lib.render(embeds);
        }
    }

    if (window.tiktokEmbed) {
        renderEmbeds();
        return;
    }

    var existingScript = document.querySelector('script[src="https://www.tiktok.com/embed.js"]');
    if (existingScript) {
        existingScript.addEventListener('load', renderEmbeds);
        return;
    }

    var script = document.createElement('script');
    script.src = 'https://www.tiktok.com/embed.js';
    script.async = true;
    script.onload = renderEmbeds;
    document.body.appendChild(script);
}

initClipTiktokEmbeds();

function initBgParallaxMovement() {
    var sections = Array.prototype.slice.call(document.querySelectorAll('[data-bg-parallax]'));
    if (!sections.length) return;

    var layers = [];
    var ticking = false;

    sections.forEach(function(section) {
        var container = section.querySelector('.parallax-container');
        if (!container) return;

        // Disable theme scrolly so it cannot overwrite bg-position
        if (window.jQuery) {
            var $container = window.jQuery(container);
            $container.removeData('plugin_scrolly');
            container.removeAttribute('data-velocity');
        }

        var bgUrl = section.getAttribute('data-bg-parallax') || container.getAttribute('data-bg') || '';
        var velocity = parseFloat(section.getAttribute('data-velocity') || '-0.28');

        container.classList.add('img-loaded', 'parallax-smooth');
        container.style.backgroundImage = bgUrl ? 'url("' + bgUrl + '")' : container.style.backgroundImage;
        container.style.backgroundPosition = 'center center';
        container.style.backgroundSize = 'cover';
        container.style.transform = 'translate3d(0,0,0)';
        container.style.willChange = 'transform';

        layers.push({
            section: section,
            container: container,
            velocity: velocity
        });
    });

    if (!layers.length) return;

    function updateParallax() {
        var viewport = window.innerHeight || document.documentElement.clientHeight;

        layers.forEach(function(layer) {
            var rect = layer.section.getBoundingClientRect();
            var sectionTop = layer.section.offsetTop;
            var scrollY = window.pageYOffset || document.documentElement.scrollTop;

            // Keep a continuous shift tied to section progress (no snap on stop)
            var progress = (scrollY + viewport * 0.5) - (sectionTop + layer.section.offsetHeight * 0.5);
            var shift = progress * layer.velocity;

            layer.container.style.transform = 'translate3d(0,' + shift.toFixed(2) + 'px,0)';
        });

        ticking = false;
    }

    function requestUpdate() {
        if (ticking) return;
        ticking = true;
        window.requestAnimationFrame(updateParallax);
    }

    window.addEventListener('scroll', requestUpdate, { passive: true });
    window.addEventListener('resize', requestUpdate);
    updateParallax();
}

$(window).on('load', function() {
    // Wait for theme to inject .parallax-container, then take over smoothly
    setTimeout(initBgParallaxMovement, 120);
});

