(function(e){e(".wpzinc-media-library-selector").each(function(){var t=e(this).data("multiple");if(typeof t=="undefined"){t=false}if(!t){return}e("ul",e(this)).sortable()});e("#wpbody").on("click",".wpzinc-media-library-insert",function(t){t.preventDefault();var a=e(this).closest(".wpzinc-media-library-selector"),i=e(a).data("input-name"),n=e(a).data("output-size"),l=e(a).data("file-type"),r=e(a).data("multiple"),o=e(a).data("limit");if(typeof n=="undefined"){n="thumbnail"}if(typeof l=="undefined"){l="image"}if(typeof r=="undefined"){r=false}if(typeof o=="undefined"){o=99999}var c=[];e("input[type=hidden]",e(a)).each(function(){c.push(e(this).val())});if(s){s.open();return}var s=wp.media({title:"Choose Item",button:{text:"Select"},library:{type:l},multiple:r?"add":false});s.on("open",function(){var e=s.state().get("selection");c.forEach(function(t){attachment=wp.media.attachment(t);attachment.fetch();e.add(attachment?[attachment]:[])})});s.off("select");s.off("selection:toggle");s.on("selection:toggle",function(){var e=s.state().get("selection");if(e.length>o){e.remove(e.last())}});s.on("select",function(){var t=s.state().get("selection").map(function(e){e.toJSON();return e});e("ul",e(a)).html("");var l=t.length;for(var r=0;r<l;r++){var o=t[r],c=o.get("id"),f=o.get("url");var u='<li class="wpzinc-media-library-attachment">';u+='<a href="#" class="wpzinc-media-library-insert">';u+='<input type="hidden" name="'+i+'" value="'+c+'" />';switch(o.attributes.type){case"image":if(typeof o.attributes.sizes[n]!=="undefined"){console.log(o.attributes.sizes[n]);f=o.attributes.sizes[n].url}u+='<img src="'+f+'" />';break;default:u+=o.attributes.filename;break}u+='<a href="#" class="wpzinc-media-library-remove">Remove</a>';u+="</li>";e("ul",e(a)).append(u)}});s.open()});e("#wpbody").on("click",".wpzinc-media-library-remove",function(t){t.preventDefault();e(this).closest(".wpzinc-media-library-attachment").remove()})})(jQuery);