jQuery(function ($) {
    $(document).on('click', '#upload_mp3_button', function (e) {
        e.preventDefault();

        var frame = wp.media({
            title: mp3vplayer_admin.select_title,
            button: {
                text: mp3vplayer_admin.button_text
            },
            multiple: false,
            library: {
                type: 'audio'
            }
        });

        frame.on('select', function () {
            var attachment = frame.state().get('selection').first().toJSON();
            $('#mp3_url').val(attachment.url);

            if (!$('#mp3_title').val()) {
                var currentPlaceholder = $('#mp3_title').attr('placeholder') || '';
                $('#mp3_title').attr('placeholder', attachment.title || currentPlaceholder);
            }
        });

        frame.open();
    });
});
