jQuery(document).ready(function($) {
    // Form submission চেক
    if (team_mxt_data.form_submitted) {
        window.onload = function() {
            window.location.reload(true);
        }
    }
});

jQuery(document).ready(function($) {
    // Function to update the live preview and selected style
    function updateTeamMxtLivePreview(style) {
        var data = {
            'action': 'team_mxt_unique_live_preview',
            'style': style
        };

        $.post(team_mxt_data.ajax_url, data, function(response) {
            if (response.success) {
                $('#team-mxt-live-preview').html(response.data.html);
                loadStyleCss(style);

                localStorage.setItem('team_mxt_selected_style', style);
            } else {
                $('#team-mxt-live-preview').html('<p>Error loading preview</p>');
            }
        });
    }

    // Function to load the selected style CSS
    function loadStyleCss(style) {
        var cssLink = team_mxt_data.plugin_url + 'css/' + style.replace('_', '-') + '.css';
        $('#dynamic-style-css').remove();
        $('<link>', {
            id: 'dynamic-style-css',
            rel: 'stylesheet',
            type: 'text/css',
            href: cssLink
        }).appendTo('head');
    }

    var savedStyle = localStorage.getItem('team_mxt_selected_style');
    if (savedStyle) {
        $('input[name="team_mxt_member_style"][value="' + savedStyle + '"]').prop('checked', true);
        updateTeamMxtLivePreview(savedStyle);

        // Set the shortcode input value
        $('#team_mxt_shortcode').val('[team_mxt style="' + savedStyle + '"]');
        $('#team_mxt_members_without_slider_shortcode').val('[team_mxt_without_slider style="' +
            savedStyle + '"]');
    }

    // Handle radio button changes
    $('input[name="team_mxt_member_style"]').change(function() {
        var selectedStyle = $(this).val();
        updateTeamMxtLivePreview(selectedStyle);

        // Set the shortcode input value for both shortcodes
        $('#team_mxt_shortcode').val('[team_mxt style="' + selectedStyle + '"]');
        $('#team_mxt_members_without_slider_shortcode').val('[team_mxt_without_slider style="' +
            selectedStyle + '"]');

        // Save the selected style to local storage
        localStorage.setItem('team_mxt_selected_style', selectedStyle);
    });

    // Handle copy shortcode button click for the first shortcode
    $('#team_mxt_copy_shortcode').click(function() {
        var copyText = document.getElementById('team_mxt_shortcode');
        copyText.select();
        document.execCommand('copy');
    });

    // Handle copy shortcode button click for the second shortcode
    $('#team_mxt_copy_without_slider_shortcode').click(function() {
        var copyText = document.getElementById('team_mxt_members_without_slider_shortcode');
        copyText.select();
        document.execCommand('copy');
    });
});

document.addEventListener('DOMContentLoaded', function() {
    var categoryTitles = document.querySelectorAll('.category-title');

    categoryTitles.forEach(function(title) {
        title.addEventListener('click', function() {
            var stylesSection = this.nextElementSibling;
            if (stylesSection.style.display === 'none' || stylesSection.style.display === '') {
                stylesSection.style.display = 'flex';
                stylesSection.style.flexWrap = 'wrap';
            } else {
                stylesSection.style.display = 'none';
            }
        });
    });
});

// For Notification
document.addEventListener("DOMContentLoaded", function() {
    var notificationElement = document.querySelector('.notification');
    var closeButton = document.querySelector('.close-btn');

    closeButton.addEventListener('click', function() {
        notificationElement.style.display = 'none'; // Hide the notification
        window.removeEventListener('scroll', handleScroll); // Remove scroll event listener
    });

    function handleScroll() {
        var scrollPosition = window.pageYOffset || document.documentElement.scrollTop;
        if (scrollPosition > 400) {
            notificationElement.style.bottom = '20px';
        }
    }

    window.addEventListener('scroll', handleScroll);
});
// For Premium Warning on hover
document.addEventListener('DOMContentLoaded', function() {
  var submitButton = document.getElementById('submit');
  var modal = document.getElementById('modal');
  var isMouseOverModal = false;

  submitButton.addEventListener('mouseenter', function() {
    modal.style.display = 'block';
  });

  submitButton.addEventListener('mouseleave', function() {
    hideModalTimeout = setTimeout(function() {
      if (!isMouseOverModal) {
        modal.style.display = 'none';
      }
    }, 2000);
  });

  modal.addEventListener('mouseenter', function() {
    modal.style.display = 'block';
    isMouseOverModal = true;
  });

  modal.addEventListener('mouseleave', function() {
    modal.style.display = 'none';
    isMouseOverModal = false;
  });
});