(function($,_){var workflows={};wp.media.coerce=function(attrs,key){if(_.isUndefined(attrs[key])&&!_.isUndefined(this.defaults[key])){attrs[key]=this.defaults[key];}else if('true'===attrs[key]){attrs[key]=true;}else if('false'===attrs[key]){attrs[key]=false;}
return attrs[key];};wp.media.string={props:function(props,attachment){var link,linkUrl,size,sizes,defaultProps=wp.media.view.settings.defaultProps;props=props?_.clone(props):{};if(attachment&&attachment.type){props.type=attachment.type;}
if('image'===props.type){props=_.defaults(props||{},{align:defaultProps.align||getUserSetting('align','none'),size:defaultProps.size||getUserSetting('imgsize','medium'),url:'',classes:[]});}
if(!attachment){return props;}
props.title=props.title||attachment.title;link=props.link||defaultProps.link||getUserSetting('urlbutton','file');if('file'===link||'embed'===link){linkUrl=attachment.url;}else if('post'===link){linkUrl=attachment.link;}else if('custom'===link){linkUrl=props.linkUrl;}
props.linkUrl=linkUrl||'';if('image'===attachment.type){props.classes.push('wp-image-'+attachment.id);sizes=attachment.sizes;size=sizes&&sizes[props.size]?sizes[props.size]:attachment;_.extend(props,_.pick(attachment,'align','caption','alt'),{width:size.width,height:size.height,src:size.url,captionId:'attachment_'+attachment.id});}else if('video'===attachment.type||'audio'===attachment.type){_.extend(props,_.pick(attachment,'title','type','icon','mime'));}else{props.title=props.title||attachment.filename;props.rel=props.rel||'attachment wp-att-'+attachment.id;}
return props;},link:function(props,attachment){var options;props=wp.media.string.props(props,attachment);options={tag:'a',content:props.title,attrs:{href:props.linkUrl}};if(props.rel){options.attrs.rel=props.rel;}
return wp.html.string(options);},audio:function(props,attachment){return wp.media.string._audioVideo('audio',props,attachment);},video:function(props,attachment){return wp.media.string._audioVideo('video',props,attachment);},_audioVideo:function(type,props,attachment){var shortcode,html,extension;props=wp.media.string.props(props,attachment);if(props.link!=='embed'){return wp.media.string.link(props);}
shortcode={};if('video'===type){if(attachment.image&&-1===attachment.image.src.indexOf(attachment.icon)){shortcode.poster=attachment.image.src;}
if(attachment.width){shortcode.width=attachment.width;}
if(attachment.height){shortcode.height=attachment.height;}}
extension=attachment.filename.split('.').pop();if(_.contains(wp.media.view.settings.embedExts,extension)){shortcode[extension]=attachment.url;}else{return wp.media.string.link(props);}
html=wp.shortcode.string({tag:type,attrs:shortcode});return html;},image:function(props,attachment){var img={},options,classes,shortcode,html;props.type='image';props=wp.media.string.props(props,attachment);classes=props.classes||[];img.src=!_.isUndefined(attachment)?attachment.url:props.url;_.extend(img,_.pick(props,'width','height','alt'));if(props.align&&!props.caption){classes.push('align'+props.align);}
if(props.size){classes.push('size-'+props.size);}
img['class']=_.compact(classes).join(' ');options={tag:'img',attrs:img,single:true};if(props.linkUrl){options={tag:'a',attrs:{href:props.linkUrl},content:options};}
html=wp.html.string(options);if(props.caption){shortcode={};if(img.width){shortcode.width=img.width;}
if(props.captionId){shortcode.id=props.captionId;}
if(props.align){shortcode.align='align'+props.align;}
html=wp.shortcode.string({tag:'caption',attrs:shortcode,content:html+' '+props.caption});}
return html;}};wp.media.embed={coerce:wp.media.coerce,defaults:{url:'',width:'',height:''},edit:function(data,isURL){var frame,props={},shortcode;if(isURL){props.url=data.replace(/<[^>]+>/g,'');}else{shortcode=wp.shortcode.next('embed',data).shortcode;props=_.defaults(shortcode.attrs.named,this.defaults);if(shortcode.content){props.url=shortcode.content;}}
frame=wp.media({frame:'post',state:'embed',metadata:props});return frame;},shortcode:function(model){var self=this,content;_.each(this.defaults,function(value,key){model[key]=self.coerce(model,key);if(value===model[key]){delete model[key];}});content=model.url;delete model.url;return new wp.shortcode({tag:'embed',attrs:model,content:content});}};wp.media.collection=function(attributes){var collections={};return _.extend({coerce:wp.media.coerce,attachments:function(shortcode){var shortcodeString=shortcode.string(),result=collections[shortcodeString],attrs,args,query,others,self=this;delete collections[shortcodeString];if(result){return result;}
attrs=_.defaults(shortcode.attrs.named,this.defaults);args=_.pick(attrs,'orderby','order');args.type=this.type;args.perPage=-1;if(undefined!==attrs.orderby){attrs._orderByField=attrs.orderby;}
if('rand'===attrs.orderby){attrs._orderbyRandom=true;}
if(!attrs.orderby||/^menu_order(?: ID)?$/i.test(attrs.orderby)){args.orderby='menuOrder';}
if(attrs.ids){args.post__in=attrs.ids.split(',');args.orderby='post__in';}else if(attrs.include){args.post__in=attrs.include.split(',');}
if(attrs.exclude){args.post__not_in=attrs.exclude.split(',');}
if(!args.post__in){args.uploadedTo=attrs.id;}
others=_.omit(attrs,'id','ids','include','exclude','orderby','order');_.each(this.defaults,function(value,key){others[key]=self.coerce(others,key);});query=wp.media.query(args);query[this.tag]=new Backbone.Model(others);return query;},shortcode:function(attachments){var props=attachments.props.toJSON(),attrs=_.pick(props,'orderby','order'),shortcode,clone;if(attachments.type){attrs.type=attachments.type;delete attachments.type;}
if(attachments[this.tag]){_.extend(attrs,attachments[this.tag].toJSON());}
attrs.ids=attachments.pluck('id');if(props.uploadedTo){attrs.id=props.uploadedTo;}
delete attrs.orderby;if(attrs._orderbyRandom){attrs.orderby='rand';}else if(attrs._orderByField&&'rand'!==attrs._orderByField){attrs.orderby=attrs._orderByField;}
delete attrs._orderbyRandom;delete attrs._orderByField;if(attrs.ids&&'post__in'===attrs.orderby){delete attrs.orderby;}
attrs=this.setDefaults(attrs);shortcode=new wp.shortcode({tag:this.tag,attrs:attrs,type:'single'});clone=new wp.media.model.Attachments(attachments.models,{props:props});clone[this.tag]=attachments[this.tag];collections[shortcode.string()]=clone;return shortcode;},edit:function(content){var shortcode=wp.shortcode.next(this.tag,content),defaultPostId=this.defaults.id,attachments,selection,state;if(!shortcode||shortcode.content!==content){return;}
shortcode=shortcode.shortcode;if(_.isUndefined(shortcode.get('id'))&&!_.isUndefined(defaultPostId)){shortcode.set('id',defaultPostId);}
attachments=this.attachments(shortcode);selection=new wp.media.model.Selection(attachments.models,{props:attachments.props.toJSON(),multiple:true});selection[this.tag]=attachments[this.tag];selection.more().done(function(){selection.props.set({query:false});selection.unmirror();selection.props.unset('orderby');});if(this.frame){this.frame.dispose();}
if(shortcode.attrs.named.type&&'video'===shortcode.attrs.named.type){state='video-'+this.tag+'-edit';}else{state=this.tag+'-edit';}
this.frame=wp.media({frame:'post',state:state,title:this.editTitle,editing:true,multiple:true,selection:selection}).open();return this.frame;},setDefaults:function(attrs){var self=this;_.each(this.defaults,function(value,key){attrs[key]=self.coerce(attrs,key);if(value===attrs[key]){delete attrs[key];}});return attrs;}},attributes);};wp.media._galleryDefaults={itemtag:'dl',icontag:'dt',captiontag:'dd',columns:'3',link:'post',size:'thumbnail',order:'ASC',id:wp.media.view.settings.post&&wp.media.view.settings.post.id,orderby:'menu_order ID'};if(wp.media.view.settings.galleryDefaults){wp.media.galleryDefaults=_.extend({},wp.media._galleryDefaults,wp.media.view.settings.galleryDefaults);}else{wp.media.galleryDefaults=wp.media._galleryDefaults;}
wp.media.gallery=new wp.media.collection({tag:'gallery',type:'image',editTitle:wp.media.view.l10n.editGalleryTitle,defaults:wp.media.galleryDefaults,setDefaults:function(attrs){var self=this,changed=!_.isEqual(wp.media.galleryDefaults,wp.media._galleryDefaults);_.each(this.defaults,function(value,key){attrs[key]=self.coerce(attrs,key);if(value===attrs[key]&&(!changed||value===wp.media._galleryDefaults[key])){delete attrs[key];}});return attrs;}});wp.media.featuredImage={get:function(){return wp.media.view.settings.post.featuredImageId;},set:function(id){var settings=wp.media.view.settings;settings.post.featuredImageId=id;wp.media.post('get-post-thumbnail-html',{post_id:settings.post.id,thumbnail_id:settings.post.featuredImageId,_wpnonce:settings.post.nonce}).done(function(html){if('0'===html){window.alert(wp.i18n.__('Could not set that as the thumbnail image. Try a different attachment.'));return;}
$('.inside','#postimagediv').html(html);});},remove:function(){wp.media.featuredImage.set(-1);},frame:function(){if(this._frame){wp.media.frame=this._frame;return this._frame;}
this._frame=wp.media({state:'featured-image',states:[new wp.media.controller.FeaturedImage(),new wp.media.controller.EditImage()]});this._frame.on('toolbar:create:featured-image',function(toolbar){this.createSelectToolbar(toolbar,{text:wp.media.view.l10n.setFeaturedImage});},this._frame);this._frame.on('content:render:edit-image',function(){var selection=this.state('featured-image').get('selection'),view=new wp.media.view.EditImage({model:selection.single(),controller:this}).render();this.content.set(view);view.loadEditor();},this._frame);this._frame.state('featured-image').on('select',this.select);return this._frame;},select:function(){var selection=this.get('selection').single();if(!wp.media.view.settings.post.featuredImageId){return;}
wp.media.featuredImage.set(selection?selection.id:-1);},init:function(){$('#postimagediv').on('click keyup keydown','#set-post-thumbnail',function(event){if((event.type==='keyup'&&event.key===' ')||(event.type==='keydown'&&event.key==='Enter')||event.type==='click'){event.preventDefault();event.stopPropagation();wp.media.featuredImage.frame().open();}}).on('click keyup keydown','#remove-post-thumbnail',function(event){if((event.type==='keyup'&&event.key===' ')||(event.type==='keydown'&&event.key==='Enter')||event.type==='click'){wp.media.featuredImage.remove();return false;}});}};$(wp.media.featuredImage.init);wp.media.editor={insert:function(html){var editor,wpActiveEditor,hasTinymce=!_.isUndefined(window.tinymce),hasQuicktags=!_.isUndefined(window.QTags);if(this.activeEditor){wpActiveEditor=window.wpActiveEditor=this.activeEditor;}else{wpActiveEditor=window.wpActiveEditor;}
if(window.send_to_editor){return window.send_to_editor.apply(this,arguments);}
if(!wpActiveEditor){if(hasTinymce&&tinymce.activeEditor){editor=tinymce.activeEditor;wpActiveEditor=window.wpActiveEditor=editor.id;}else if(!hasQuicktags){return false;}}else if(hasTinymce){editor=tinymce.get(wpActiveEditor);}
if(editor&&!editor.isHidden()){editor.execCommand('mceInsertContent',false,html);}else if(hasQuicktags){QTags.insertContent(html);}else{document.getElementById(wpActiveEditor).value+=html;}
if(window.tb_remove){try{window.tb_remove();}catch(e){}}},add:function(id,options){var workflow=this.get(id);if(workflow){return workflow;}
workflow=workflows[id]=wp.media(_.defaults(options||{},{frame:'post',state:'insert',title:wp.media.view.l10n.addMedia,multiple:true}));workflow.on('insert',function(selection){var state=workflow.state();selection=selection||state.get('selection');if(!selection){return;}
$.when.apply($,selection.map(function(attachment){var display=state.display(attachment).toJSON();return this.send.attachment(display,attachment.toJSON());},this)).done(function(){wp.media.editor.insert(_.toArray(arguments).join('\n\n'));});},this);workflow.state('gallery-edit').on('update',function(selection){this.insert(wp.media.gallery.shortcode(selection).string());},this);workflow.state('playlist-edit').on('update',function(selection){this.insert(wp.media.playlist.shortcode(selection).string());},this);workflow.state('video-playlist-edit').on('update',function(selection){this.insert(wp.media.playlist.shortcode(selection).string());},this);workflow.state('embed').on('select',function(){var state=workflow.state(),type=state.get('type'),embed=state.props.toJSON();embed.url=embed.url||'';if('link'===type){_.defaults(embed,{linkText:embed.url,linkUrl:embed.url});this.send.link(embed).done(function(resp){wp.media.editor.insert(resp);});}else if('image'===type){_.defaults(embed,{title:embed.url,linkUrl:'',align:'none',link:'none'});if('none'===embed.link){embed.linkUrl='';}else if('file'===embed.link){embed.linkUrl=embed.url;}
this.insert(wp.media.string.image(embed));}},this);workflow.state('featured-image').on('select',wp.media.featuredImage.select);workflow.setState(workflow.options.state);return workflow;},id:function(id){if(id){return id;}
id=window.wpActiveEditor;if(!id&&!_.isUndefined(window.tinymce)&&tinymce.activeEditor){id=tinymce.activeEditor.id;}
id=id||'';return id;},get:function(id){id=this.id(id);return workflows[id];},remove:function(id){id=this.id(id);delete workflows[id];},send:{attachment:function(props,attachment){var caption=attachment.caption,options,html;if(!wp.media.view.settings.captions){delete attachment.caption;}
props=wp.media.string.props(props,attachment);options={id:attachment.id,post_content:attachment.description,post_excerpt:caption};if(props.linkUrl){options.url=props.linkUrl;}
if('image'===attachment.type){html=wp.media.string.image(props);_.each({align:'align',size:'image-size',alt:'image_alt'},function(option,prop){if(props[prop]){options[option]=props[prop];}});}else if('video'===attachment.type){html=wp.media.string.video(props,attachment);}else if('audio'===attachment.type){html=wp.media.string.audio(props,attachment);}else{html=wp.media.string.link(props);options.post_title=props.title;}
return wp.media.post('send-attachment-to-editor',{nonce:wp.media.view.settings.nonce.sendToEditor,attachment:options,html:html,post_id:wp.media.view.settings.post.id});},link:function(embed){return wp.media.post('send-link-to-editor',{nonce:wp.media.view.settings.nonce.sendToEditor,src:embed.linkUrl,link_text:embed.linkText,html:wp.media.string.link(embed),post_id:wp.media.view.settings.post.id});}},open:function(id,options){var workflow;options=options||{};id=this.id(id);this.activeEditor=id;workflow=this.get(id);if(!workflow||(workflow.options&&options.state!==workflow.options.state)){workflow=this.add(id,options);}
wp.media.frame=workflow;return workflow.open();},init:function(){$(document.body).on('click.add-media-button','.insert-media',function(event){var elem=$(event.currentTarget),editor=elem.data('editor'),options={frame:'post',state:'insert',title:wp.media.view.l10n.addMedia,multiple:true};event.preventDefault();if(elem.hasClass('gallery')){options.state='gallery';options.title=wp.media.view.l10n.createGalleryTitle;}
wp.media.editor.open(editor,options);});new wp.media.view.EditorUploader().render();}};_.bindAll(wp.media.editor,'open');$(wp.media.editor.init);}(jQuery,_));