
/*
English:

JS framework framework.averin.pro 
Version: 7.0.0
Prefix: ""
Author: Artemiy Averin (ya@averin.pro)
Company: Design studio Artemy Averin (https://averin.pro/) 

Docs & Examples: https://framework.averin.pro/ 

Русский: 

JS фреймворк framework.averin.pro
Версия: 7.0.0
Префикс: ""
Студия Артемия Аверина (ya@averin.pro)
 
Документая и примеры: httpы://framework.averin.pro/
*/

	/*** Start animator component ***/
/*
AverinAnimator — JQuery js plugin Version: 2.1
*/
$(function(){
    // Функция throttle для оптимизации производительности
    function throttle(func, limit) {
        let inThrottle;
        return function() {
            const args = arguments;
            const context = this;
            if (!inThrottle) {
                func.apply(context, args);
                inThrottle = true;
                setTimeout(() => inThrottle = false, limit);
            }
        }
    }

    function isElementInViewport(el, offset = 100) {
        var rect = el.getBoundingClientRect();
        var windowHeight = window.innerHeight || document.documentElement.clientHeight;
        var windowWidth = window.innerWidth || document.documentElement.clientWidth;

        return (
            rect.top <= windowHeight - offset &&
            rect.bottom >= offset
        );
    }

    $("*[data-effect]").each(function(){
        var $this = $(this);
        var effect = $this.attr("data-effect");
        var data_hold = $this.attr("data-hold");
        var miliseconds_hold = data_hold ? parseFloat(data_hold)/1000 : 0;

        var data_holdsee = $this.attr("data-holdsee");
        var holdsee = data_holdsee || 0;

        var animate_start = $this.attr("data-duration");
        var miliseconds = animate_start ? parseFloat(animate_start)/1000 : 1;

        // Делаем элемент невидимым изначально
        $this.css({
            "opacity": "0",
            "visibility": "hidden" // Для лучшей поддержки браузеров
        });

        // Задаём скорость анимации элементу
        $this.css({
            "-webkit-animation-duration": miliseconds + "s",
            "-moz-animation-duration": miliseconds + "s",
            "-o-animation-duration": miliseconds + "s",
            "animation-duration": miliseconds + "s",
            "-webkit-animation-delay": miliseconds_hold + "s",
            "-moz-animation-delay": miliseconds_hold + "s",
            "-o-animation-delay": miliseconds_hold + "s",
            "animation-delay": miliseconds_hold + "s",
            "visibility": "visible" // Убираем visibility hidden после задания анимации
        });

        if (holdsee == 0) {
            // Анимация сразу - делаем элемент видимым и запускаем анимацию
            $this.css("opacity", "1");
            $this.addClass("animated " + effect);
        } else {
            var element = this; // Используем DOM элемент для getBoundingClientRect
            var animatedClass = "animated " + effect;
            var animationAdded = false;

            // Функция проверки видимости и запуска анимации
            function checkAndAnimate() {
                if (!animationAdded && isElementInViewport(element, 50)) {
                    // Делаем элемент видимым перед анимацией
                    $(element).css("opacity", "1");
                    $(element).addClass(animatedClass);
                    animationAdded = true;

                    // Если анимация добавлена, можно отписаться от событий для оптимизации
                    if (animationAdded) {
                        $(window).off('scroll', throttledCheck);
                        $(window).off('resize', throttledCheck);
                    }
                }
            }

            // Создаем throttled версию функции
            var throttledCheck = throttle(checkAndAnimate, 100);

            // Проверяем сразу после загрузки страницы
            $(window).on('load', function() {
                setTimeout(checkAndAnimate, 200); // Небольшая задержка после полной загрузки
            });

            // Проверяем после загрузки DOM
            setTimeout(checkAndAnimate, 300);

            // Проверяем при скролле
            $(window).on('scroll', throttledCheck);

            // Проверяем при изменении размера окна
            $(window).on('resize', throttledCheck);

            // Дополнительные проверки с задержкой
            setTimeout(function() {
                checkAndAnimate();
            }, 500);

            setTimeout(function() {
                checkAndAnimate();
            }, 1000);
        }
    });
});
/*** End animator component ***/
	
	