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

    /**
     * Initialize Footer Newsletter Widget
     */
    function initFooterNewsletter($scope) {
        var $form = $scope.find('.unlock_addon_footer_newsletter_form');
        
        if ($form.length) {
            $form.on('submit', function(e) {
                e.preventDefault();
                
                var $form = $(this);
                var $input = $form.find('.unlock_addon_footer_newsletter_input');
                var $button = $form.find('.unlock_addon_footer_newsletter_button');
                var email = $input.val();
                var formAction = $form.attr('action');
                
                if (!email || !isValidEmail(email)) {
                    $input.addClass('error');
                    setTimeout(function() {
                        $input.removeClass('error');
                    }, 2000);
                    return false;
                }
                
                // Disable button during submission
                $button.prop('disabled', true).text('Subscribing...');
                
                // If form action is set and not '#', submit to external service
                if (formAction && formAction !== '#') {
                    // Create a hidden form and submit
                    var $hiddenForm = $('<form>', {
                        'method': 'post',
                        'action': formAction,
                        'target': '_blank',
                        'style': 'display: none;'
                    });
                    
                    var $hiddenInput = $('<input>', {
                        'type': 'hidden',
                        'name': 'EMAIL',
                        'value': email
                    });
                    
                    $hiddenForm.append($hiddenInput);
                    $('body').append($hiddenForm);
                    $hiddenForm.submit();
                    
                    // Show success message
                    setTimeout(function() {
                        $button.prop('disabled', false).text($button.data('original-text') || 'Subscribe');
                        showSuccessMessage($form, 'Thank you for subscribing!');
                    }, 1000);
                } else {
                    // Default success message
                    setTimeout(function() {
                        $button.prop('disabled', false).text($button.data('original-text') || 'Subscribe');
                        showSuccessMessage($form, 'Thank you for subscribing!');
                        $input.val('');
                    }, 1000);
                }
                
                return false;
            });
            
            // Store original button text
            $form.find('.unlock_addon_footer_newsletter_button').each(function() {
                if (!$(this).data('original-text')) {
                    $(this).data('original-text', $(this).text());
                }
            });
        }
    }
    
    /**
     * Validate email
     */
    function isValidEmail(email) {
        var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
        return emailRegex.test(email);
    }
    
    /**
     * Show success message
     */
    function showSuccessMessage($form, message) {
        var $message = $('<div>', {
            'class': 'unlockafe-newsletter-success',
            'text': message,
            'css': {
                'color': '#4caf50',
                'margin-top': '10px',
                'font-size': '14px'
            }
        });
        
        $form.after($message);
        
        setTimeout(function() {
            $message.fadeOut(function() {
                $message.remove();
            });
        }, 3000);
    }

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

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

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

})(jQuery);




