jQuery(document).ready(function($) {
    // Add body class for voices page
    $('body').addClass('voices-page-active');
    
    // If API key is connected, load voices
    if ($('.mementor-tts-api-status').hasClass('connected')) {
        mementorTTSLoadVoices();
    }

    // Add Refresh Voices (clear cache) AJAX handler
    $('#clear-voice-cache').on('click', function() {
        var $btn = $(this);
        $btn.prop('disabled', true).html('<span class="dashicons dashicons-update spin"></span> Refreshing...');
        $.ajax({
            url: ajaxurl,
            method: 'POST',
            data: {
                action: 'mementor_tts_clear_voice_cache',
                nonce: (typeof mementorTTS !== 'undefined' && mementorTTS.nonce) ? mementorTTS.nonce : ''
            },
            success: function(response) {
                // Reload the page to fetch fresh voices
                location.reload();
            },
            error: function() {
                $btn.prop('disabled', false).html('<span class="dashicons dashicons-update"></span> Refresh Voices');
                alert('Failed to refresh voices. Please try again.');
            }
        });
    });
}); 