jQuery(document).ready(function($) {
    var mediaUploader;
    
    $('#upload_cover_image_button').click(function(e) {
        e.preventDefault();
        if (mediaUploader) {
            mediaUploader.open();
            return;
        }
        mediaUploader = wp.media.frames.file_frame = wp.media({
            title: 'Choose Cover Image',
            button: {
                text: 'Choose Image'
            },
            multiple: false
        });
        mediaUploader.on('select', function() {
            var attachment = mediaUploader.state().get('selection').first().toJSON();
            $('#cover_image').val(attachment.url);
            $('#cover_image_preview').attr('src', attachment.url);
        });
        mediaUploader.open();
    });
});
