;(function($) {
    'use strict';

    /**
     * Initialize Promo Box Widget
     */
    function initPromoBox($scope) {
        const $wrapper = $scope.find('.unlockafe-promo-box-wrapper');
        
        if ($wrapper.length === 0) {
            return;
        }

        // Remove gray background from parent Elementor section
        const $parentSection = $scope.closest('.elementor-section');
        if ($parentSection.length) {
            const bgColor = $parentSection.css('background-color');
            const computedBgColor = window.getComputedStyle($parentSection[0]).backgroundColor;
            
            // Common gray background colors to detect and remove
            const grayColors = [
                'rgb(245, 245, 245)', '#f5f5f5', 'rgb(243, 243, 243)', '#f3f3f3',
                'rgb(240, 240, 240)', '#f0f0f0', 'rgb(229, 229, 229)', '#e5e5e5',
                'rgb(236, 236, 236)', '#ececec', 'rgb(239, 239, 239)', '#efefef',
                'rgb(224, 224, 224)', '#e0e0e0', 'rgb(211, 211, 211)', '#d3d3d3',
                'rgb(204, 204, 204)', '#cccccc'
            ];
            
            // Check if background color is gray
            const isGrayBackground = grayColors.some(grayColor => {
                return computedBgColor.toLowerCase().includes(grayColor.toLowerCase()) ||
                       bgColor.toLowerCase().includes(grayColor.toLowerCase());
            });
            
            if (isGrayBackground) {
                $parentSection.css({
                    'background': 'transparent !important',
                    'background-color': 'transparent !important'
                });
                $parentSection.addClass('unlockafe-promo-no-gray-bg');
            }
        }

        // Add hover effects
        $wrapper.on('mouseenter', function() {
            $(this).addClass('is-hovered');
        }).on('mouseleave', function() {
            $(this).removeClass('is-hovered');
        });

        // Parallax effect for background images
        const $bgImage = $wrapper.find('.unlockafe-promo-background-image');
        if ($bgImage.length) {
            $(window).on('scroll.promoBox', function() {
                const scrollTop = $(window).scrollTop();
                const elementTop = $wrapper.offset().top;
                const elementHeight = $wrapper.outerHeight();
                const windowHeight = $(window).height();
                
                if (elementTop + elementHeight > scrollTop && elementTop < scrollTop + windowHeight) {
                    const parallaxSpeed = 0.5;
                    const yPos = -(scrollTop - elementTop) * parallaxSpeed;
                    $bgImage.css('transform', 'translateY(' + yPos + 'px)');
                }
            });
        }
    }

    // Initialize on Elementor frontend
    $(window).on('elementor/frontend/init', function() {
        elementorFrontend.hooks.addAction('frontend/element_ready/unlockafe-promo-box.default', function($scope) {
            initPromoBox($scope);
        });
    });

    // Also initialize on document ready (for non-Elementor pages)
    $(document).ready(function() {
        $('.elementor-widget-unlockafe-promo-box').each(function() {
            var $scope = $(this);
            initPromoBox($scope);
        });
    });

    // Re-initialize on Elementor preview refresh
    if (typeof elementorFrontend !== 'undefined') {
        elementorFrontend.hooks.addAction('frontend/element_ready/global', function($scope) {
            if ($scope.find('.unlockafe-promo-box-wrapper').length) {
                setTimeout(function() {
                    initPromoBox($scope);
                }, 100);
            }
        });
    }

})(jQuery);





