window.wp=window.wp||{};(function(){wp.html=_.extend(wp.html||{},{attrs:function(c){var a,b;if("/"===c[c.length-1]){c=c.slice(0,-1)}a=wp.shortcode.attrs(c);b=a.named;_.each(a.numeric,function(d){if(/\s/.test(d)){return}b[d]=""});return b},string:function(a){var c="<"+a.tag,b=a.content||"";_.each(a.attrs,function(e,d){c+=" "+d;if(""===e){return}if(_.isBoolean(e)){e=e?"true":"false"}c+='="'+e+'"'});if(a.single){return c+" />"}c+=">";c+=_.isObject(b)?wp.html.string(b):b;return c+""+a.tag+">"}})}());(function(b){var a={},c={};wp.mce=wp.mce||{};wp.mce.view={defaults:{pattern:{view:Backbone.View,text:function(d){return d.options.original},toView:function(e){if(!this.pattern){return}this.pattern.lastIndex=0;var d=this.pattern.exec(e);if(!d){return}return{index:d.index,content:d[0],options:{original:d[0],results:d}}}},shortcode:{view:Backbone.View,text:function(d){return d.options.shortcode.string()},toView:function(e){var d=wp.shortcode.next(this.shortcode,e);if(!d){return}return{index:d.index,content:d.content,options:{shortcode:d.shortcode}}}}},add:function(i,e){var g,d,h,f;if(e.extend){g=wp.mce.view.get(e.extend)}else{if(e.shortcode){g=wp.mce.view.defaults.shortcode}else{g=wp.mce.view.defaults.pattern}}_.defaults(e,g);e.id=i;f={remove:function(){delete c[this.el.id];this.$el.parent().remove();if(d){d.apply(this,arguments)}return this}};if(_.isFunction(e.view)){h=e.view}else{h=g.view;d=e.view.remove;_.defaults(f,e.view)}if(!d&&!h._mceview){d=h.prototype.remove}e.view=h.extend(f,{_mceview:true});a[i]=e},get:function(d){return a[d]},remove:function(d){delete a[d]},toViews:function(e){var d=[{content:e}],f;_.each(a,function(h,g){f=d.slice();d=[];_.each(f,function(k){var j=k.content,i;if(k.processed){d.push(k);return}while(j&&(i=h.toView(j))){if(i.index){d.push({content:j.substring(0,i.index)})}d.push({content:wp.mce.view.toView(g,i.options),processed:true});j=j.slice(i.index+i.content.length)}if(j){d.push({content:j})}})});return _.pluck(d,"content").join("")},toView:function(e,g){var f=wp.mce.view.get(e),d,h;if(!f){return""}d=new f.view(_.extend(g||{},{viewType:e}));h=d.el.id=d.el.id||_.uniqueId("__wpmce-");c[h]=d;d.$wrapper=b();return wp.html.string({tag:"span"===d.tagName?"span":"div",attrs:{"class":"wp-view-wrap wp-view-type-"+e,"data-wp-view":h,contenteditable:false}})},render:function(d){b(".wp-view-wrap",d).each(function(){var f=b(this),e=wp.mce.view.instance(this);if(!e){return}e.$wrapper=f;e.render();e.$el.detach();f.empty().append(e.el).append('')})},toText:function(d){return d.replace(/<(?:div|span)[^>]+data-wp-view="([^"]+)"[^>]*>.*?]+data-wp-view-end[^>]*><\/span><\/(?:div|span)>/g,function(g,h){var e=c[h],f;if(e){f=wp.mce.view.get(e.options.viewType)}return e&&f?f.text(e):""})},removeInternalAttrs:function(e){var d={};_.each(e,function(g,f){if(-1===f.indexOf("data-mce")){d[f]=g}});return d},attrs:function(d){return wp.mce.view.removeInternalAttrs(wp.html.attrs(d))},instance:function(d){var e=b(d).data("wp-view");if(e){return c[e]}},select:function(e){var d=b(e);if(d.hasClass("selected")){return}d.addClass("selected");b(e.firstChild).trigger("select")},deselect:function(e){var d=b(e);if(!d.hasClass("selected")){return}d.removeClass("selected");b(e.firstChild).trigger("deselect")},l10n:_.isUndefined(_wpMceViewL10n)?{}:_wpMceViewL10n}}(jQuery));(function(b){var a=wp.mce.view,c;c=function(g,e){var f=e.link,d;if("file"===f){d=g.get("url")}else{if("post"===f){d=g.get("link")}else{if("custom"===f){d=e.linkUrl}}}return d||""};wp.media.string={};wp.media.string.link=function(g,e){var f=getUserSetting("urlbutton","post"),d={tag:"a",content:g.get("title")||g.get("filename"),attrs:{rel:"attachment wp-att-"+g.id}};d.attrs.href=c(g,e);return wp.html.string(d)};wp.media.string.image=function(k,i){var h,d,e,g,j,f;i=_.defaults(i||{},{img:{},align:getUserSetting("align","none"),size:getUserSetting("imgsize","medium"),link:getUserSetting("urlbutton","post")});i.linkUrl=c(k,i);k=k.toJSON();d=_.clone(i.img);h=d["class"]?d["class"].split(/\s+/):[];g=k.sizes?k.sizes[i.size]:{};if(!g){delete i.size;g=k}d.width=g.width;d.height=g.height;d.src=g.url;if(i.align&&!k.caption){h.push("align"+i.align)}if(i.size){h.push("size-"+i.size)}h.push("wp-image-"+k.id);d["class"]=_.compact(h).join(" ");e={tag:"img",attrs:d,single:true};if(i.linkUrl){i.anchor=i.anchor||{};i.anchor.href=i.linkUrl}if(i.anchor){e={tag:"a",attrs:i.anchor,content:e}}f=wp.html.string(e);if(k.caption){j={id:"attachment_"+k.id,width:d.width};if(i.align){j.align="align"+i.align}f=wp.shortcode.string({tag:"caption",attrs:j,content:f+" "+k.caption})}return f};a.add("attachment",{pattern:new RegExp("(?:]*)>)?]*class=(?:\"[^\"]*|'[^']*)\\bwp-image-(\\d+)[^>]*)>(?:)?"),text:function(d){var e=_.pick(d,"align","size","link","img","anchor");return wp.media.string.image(d.model,e)},view:{className:"editor-attachment",template:media.template("editor-attachment"),events:{"click .close":"remove"},initialize:function(){var d=this,e=this.options.results,g=e[3],f;this.model=wp.media.model.Attachment.get(g);if(e[1]){this.anchor=a.attrs(e[1])}this.img=a.attrs(e[2]);f=this.img["class"];f=f.replace(/(?:^|\s)wp-image-\d+/,"");f=f.replace(/(?:^|\s)size-(\S+)/,function(h,i){d.size=i;return""});f=f.replace(/(?:^|\s)align(left|center|right|none)(?:\s|$)/,function(h,i){d.align=i;return""});this.img["class"]=f;this.$el.addClass("spinner");this.model.fetch().done(_.bind(this.render,this))},render:function(){var e=this.model.toJSON(),d;if(!e.url){return}if(this.align){this.$wrapper.addClass("align"+this.align)}d={url:"image"===e.type?e.url:e.icon,uploading:e.uploading};_.extend(d,wp.media.fit({width:e.width,height:e.height,maxWidth:a.l10n.contentWidth}));if(this.size&&e.sizes&&e.sizes[this.size]){_.extend(d,_.pick(e.sizes[this.size],"url","width","height"))}this.$el.html(this.template(d))}}});a.add("gallery",{shortcode:"gallery",gallery:(function(){var d={};return{attachments:function(j,i){var l=j.string(),e=d[l],g,f,k,h;delete d[l];if(e){return e}g=j.attrs.named;f=_.pick(g,"orderby","order");f.type="image";f.perPage=-1;if(g.ids){f.post__in=g.ids.split(",");f.orderby="post__in"}else{if(g.include){f.post__in=g.include.split(",")}}if(g.exclude){f.post__not_in=g.exclude.split(",")}if(!f.post__in){f.parent=g.id||i}h={};_.filter(g,function(n,m){if(_.isUndefined(f[m])){h[m]=n}});k=media.query(f);k.gallery=new Backbone.Model(h);return k},shortcode:function(e){var g=e.props.toJSON(),f=_.pick(g,"include","exclude","orderby","order"),h,i;if(e.gallery){_.extend(f,e.gallery.toJSON())}f.ids=e.pluck("id");if(f.ids&&"post__in"===f.orderby){delete f.orderby}h=new wp.shortcode({tag:"gallery",attrs:f,type:"single"});i=new wp.media.model.Attachments(e.models,{props:g});i.gallery=e.gallery;d[h.string()]=i;return h}}}()),view:{className:"editor-gallery",template:media.template("editor-gallery"),parent:b("#post_ID").val(),events:{"click .close":"remove","click .edit":"edit"},initialize:function(){this.update()},update:function(){var d=a.get("gallery");this.attachments=d.gallery.attachments(this.options.shortcode,this.parent);this.attachments.more().done(_.bind(this.render,this))},render:function(){var d,f,e;if(!this.attachments.length){return}f=this.attachments.first().toJSON();e=f.sizes&&f.sizes.thumbnail?f.sizes.thumbnail:f;d={url:e.url,orientation:e.orientation,count:this.attachments.length};this.$el.html(this.template(d))},edit:function(){var d;if(!wp.media.view||this.frame){return}d=new wp.media.model.Selection(this.attachments.models,{props:this.attachments.props.toJSON(),multiple:true});d.gallery=this.attachments.gallery;this.frame=wp.media({frame:"post",state:"gallery-edit",title:a.l10n.editGallery,editing:true,multiple:true,selection:d});this.frame.on("close",function(){if(this.frame){this.frame.detach()}delete this.frame},this);this.frame.get("gallery-edit").on("update",function(f){var e=a.get("gallery");this.options.shortcode=e.gallery.shortcode(f);this.update()},this)}}})}(jQuery));