(function ($) {
	'use strict';

	var frame;

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

		if (frame) {
			frame.open();
			return;
		}

		frame = wp.media({
			title: fmexrAdminSettings.mediaTitle,
			button: { text: fmexrAdminSettings.mediaButton },
			library: { type: 'image' },
			multiple: false,
		});

		frame.on('select', function () {
			var attachment = frame.state().get('selection').first().toJSON();
			$('#fmexr_seller_logo').val(attachment.url);
			$('#fmexr_logo_preview').attr('src', attachment.url).show();
			$('#fmexr_logo_remove').show();
		});

		frame.open();
	});

	$(document).on('click', '#fmexr_logo_remove', function (e) {
		e.preventDefault();
		$('#fmexr_seller_logo').val('');
		$('#fmexr_logo_preview').attr('src', '').hide();
		$(this).hide();
	});

})(jQuery);
