jQuery(function($){
var frame,
selection,
multiple_image=true,
metaBox=$('#atbdp_gallery'),
addImgLink=metaBox.find('#directorist-listing-gallery-btn'),
delImgLink=metaBox.find('#directorist-gallery-remove'),
imgContainer=metaBox.find('.directorist-listing-gallery-container'),
active_mi_ext=atbdp_font_data.active_mi_ext;
if(1===active_mi_ext){ multiple_image=true }
addImgLink.on('click', function(event){
event.preventDefault();
if(frame){
frame.open();
return;
}
frame=wp.media({
title: atbdp_font_data.i18n_text.upload_image,
button: {
text: atbdp_font_data.i18n_text.choose_image
},
library:{ type:'image'},
multiple: multiple_image
});
frame.on('select', function(){
if(multiple_image){
selection=frame.state().get('selection').toJSON();
}else{
selection=frame.state().get('selection').first().toJSON();
}
var data='';
if($('.directorist-listing-gallery-single').length===0){
imgContainer.html('');
}
if(multiple_image){
$(selection).each(function (){
if('image'===this.type){
data +='<div class="directorist-listing-gallery-single">';
data +='<input class="directorist-listing-gallery-single__attatchment" name="gallery_img[]" type="hidden" value="'+this.id+'">';
data +='<img style="width: 100%; height: 100%;" src="'+this.url+'" alt="Listing Image" /> <span class="directorist-listing-gallery-single__remove fa fa-times" title="Remove it"></span></div>';
}});
}else{
if('image'===selection.type){
data +='<div class="directorist-listing-gallery-single">';
data +='<input class="directorist-listing-gallery-single__attatchment" name="gallery_img[]" type="hidden" value="'+selection.id+'">';
data +='<img style="width: 100%; height: 100%;" src="' + selection.url + '" alt="Listing Image" /> <span class="directorist-listing-gallery-single__remove  fa fa-times" title="Remove it"></span></div>';
}}
if(multiple_image){
imgContainer.append(data);
}else{
imgContainer.html(data);
}
delImgLink.removeClass('hidden');
});
frame.open();
});
delImgLink.on('click', function(event){
event.preventDefault();
imgContainer.html('<img src="' + atbdp_font_data.AdminAssetPath + 'images/no-image.png" alt="Listing Image" />');
delImgLink.addClass('hidden');
});
$(document).on('click', '.directorist-listing-gallery-single__remove', function (e){
e.preventDefault();
$(this).parent().remove();
if($('.directorist-listing-gallery-single').length===0){
imgContainer.html('<img src="'+atbdp_font_data.AdminAssetPath+'images/no-image.png" alt="Listing Image" /><p>No images</p> ' +
'<small>(allowed formats jpeg. png. gif)</small>');
delImgLink.addClass('hidden');
}});
if($(".directorist-gallery-grid-two .directorist-grid-item").length){
$(".directorist-gallery-grid-two .directorist-grid-item").magnificPopup({
type: "image",
delegate: "a",
gallery: {
enabled: true
}});
}});