jQuery(function ($) {
	const topOffset = 250
	const fadeRange = 500

	let ticking = false

	function onScroll() {
		if ($(window).width() <= 767) {
			$('.services-tile').css('opacity', 1)
			return
		}

		if (!ticking) {
			window.requestAnimationFrame(() => {
				$('.services-tile').each(function () {
					const $el = $(this)
					const next = $el.next('.services-tile')

					if (!next.length) {
						$el.css('opacity', 1)
						return
					}

					const nextTop = next[0].getBoundingClientRect().top

					let opacity = (nextTop - topOffset) / fadeRange
					opacity = Math.max(0, Math.min(1, opacity))
					$el.css({
						opacity: opacity,
					})
				})
				ticking = false
			})
			ticking = true
		}
	}

	onScroll()

	$(window).on('scroll resize', onScroll)
})

document.addEventListener('DOMContentLoaded', function () {
    const section = document.querySelector('.elementor-element-1fc401f');
    const header  = document.querySelector('.elementor-element-371ea19');

    if (!section || !header) return;

    let triggerPoint = 0;

    // obliczamy moment, w którym nagłówek dojedzie do góry okna
    function recomputeTriggerPoint() {
        const hadClass = header.classList.contains('sticky-header');
        // zdejmujemy klasę na czas pomiaru, żeby sticky nie zaburzało położenia
        if (hadClass) header.classList.remove('sticky-header');

        const rect = header.getBoundingClientRect();
        triggerPoint = rect.top + window.pageYOffset;

        // przywróć klasę, jeśli była
        if (hadClass) header.classList.add('sticky-header');
    }

    function updateSticky() {
        const isMobileWidth = window.innerWidth < 1025;

        if (!isMobileWidth) {
            header.classList.remove('sticky-header');
            return;
        }

        const sectionRect = section.getBoundingClientRect();
        const sectionVisible = sectionRect.bottom > 0 && sectionRect.top < window.innerHeight;

        if (sectionVisible && window.pageYOffset >= triggerPoint) {
            // dodaj tylko jeśli jeszcze jej nie ma – bez zbędnych przełączeń
            if (!header.classList.contains('sticky-header')) {
                header.classList.add('sticky-header');
            }
        } else {
            if (header.classList.contains('sticky-header')) {
                header.classList.remove('sticky-header');
            }
        }
    }

    // nasłuchujemy scroll + resize
    window.addEventListener('scroll', updateSticky);
    window.addEventListener('resize', function () {
        recomputeTriggerPoint();
        updateSticky();
    });

    // start
    recomputeTriggerPoint();
    updateSticky();
});

// jQuery(function ($) {
//     let headerHeight = $('header.elementor-location-header').outerHeight();

//     $(window).on('scroll', function () {
//         let scroll = $(window).scrollTop();
//         if (scroll >= headerHeight) {
//             $('header.elementor-location-header').addClass('fixed');
//             $('body').css('padding-top', headerHeight);
//         } else {
//             $('header.elementor-location-header').removeClass('fixed');
//             $('body').css('padding-top', 0);
//         }
//     });
// });