jQuery(document).ready(function($) {
    // Initialize audio player
    const playerContainer = document.querySelector('.mementor-tts-preview-player');
    if (playerContainer) {
        new MementorAudioPlayer(playerContainer);
    }

    // Notification styles are now loaded via admin/css/mementor-tts-notifications.css
    // The mementorTTSShowNotification function in mementor-tts-notifications.js will handle showing/hiding.

    // Helper function to format time
    function formatTime(seconds) {
        var minutes = Math.floor(seconds / 60);
        seconds = Math.floor(seconds % 60);
        return minutes + ':' + (seconds < 10 ? '0' : '') + seconds;
    }

    // Initialize label visibility on page load
    if (!$('#mementor_tts_show_player_label').is(':checked')) {
        $('.mementor-tts-preview-player .mementor-tts-player-label').hide();
    }

    // Reset player settings handler
    $('#mementor-reset-player-settings').on('click', function(e) {
        e.preventDefault();
        e.stopPropagation();
        
        // Use simple browser confirm - most reliable approach
        if (confirm("Are you sure you want to reset all player settings to default values?")) {
            const $resetButton = $(this);
            
            // Disable the button to prevent multiple clicks
            $resetButton.prop('disabled', true);
            
            // Send AJAX request to reset settings
            $.ajax({
                url: mementorTTS.ajaxurl,
                type: 'POST',
                data: {
                    action: 'mementor_tts_reset_player_settings',
                    nonce: mementorTTS.nonce
                },
                success: function(response) {
                    if (typeof window.mementorTTSShowNotification === 'function') {
                        window.mementorTTSShowNotification('Player settings have been reset.', 'success');
                    }
                    
                    // Reload the page after a short delay
                    setTimeout(function() {
                        window.location.reload();
                    }, 1000);
                },
                error: function() {
                    // Re-enable the button
                    $resetButton.prop('disabled', false);
                    
                    if (typeof window.mementorTTSShowNotification === 'function') {
                        window.mementorTTSShowNotification('An error occurred while resetting settings.', 'error');
                    }
                }
            });
        }
    });
}); 