var wpLink;!function(a,b,c){function d(){return g||e.dom.getParent(e.selection.getNode(),"a[href]")}var e,f,g,h={},i="ontouchend"in document;wpLink={textarea:"",init:function(){h.wrap=a("#wp-link-wrap"),h.dialog=a("#wp-link"),h.backdrop=a("#wp-link-backdrop"),h.submit=a("#wp-link-submit"),h.close=a("#wp-link-close"),h.text=a("#wp-link-text"),h.url=a("#wp-link-url"),h.openInNewTab=a("#wp-link-target"),a.ui&&a.ui.autocomplete&&wpLink.setAutocomplete(),h.dialog.on("keydown",wpLink.keydown),h.submit.on("click",function(a){a.preventDefault(),wpLink.update()}),h.close.add(h.backdrop).add("#wp-link-cancel button").click(function(a){a.preventDefault(),wpLink.close()}),h.url.on("paste",function(){setTimeout(wpLink.correctURL,0)})},setAutocomplete:function(){var d,e,f=h.url;f.on("keydown",function(){f.removeAttr("aria-activedescendant")}).autocomplete({source:function(b,c){return e===b.term?void c(d):/^https?:/.test(b.term)||-1!==b.term.indexOf(".")?c():(a.post(window.ajaxurl,{action:"wp-link-ajax",page:1,search:b.term,_ajax_linking_nonce:a("#_ajax_linking_nonce").val()},function(a){d=a,c(a)},"json"),void(e=b.term))},focus:function(a,b){f.attr("aria-activedescendant","mce-wp-autocomplete-"+b.item.ID),a.preventDefault()},select:function(d,e){return f.val(e.item.permalink),h.wrap.hasClass("has-text-field")&&""===a.trim(h.text.val())&&h.text.val(e.item.title),c.a11y.speak(b.linkSelected),!1},open:function(){f.attr("aria-expanded","true")},close:function(){f.attr("aria-expanded","false")},minLength:2,position:{my:"left top+2"},messages:{noResults:"undefined"!=typeof window.uiAutocompleteL10n?window.uiAutocompleteL10n.noResults:"",results:function(a){return"undefined"!=typeof window.uiAutocompleteL10n?a>1?window.uiAutocompleteL10n.manyResults.replace("%d",a):window.uiAutocompleteL10n.oneResult:void 0}}}).autocomplete("instance")._renderItem=function(b,c){return a('
').append(''+c.title+' '+c.info+"").appendTo(b)},f.attr({"aria-owns":f.autocomplete("widget").attr("id")}).on("focus",function(){var a=f.val();a&&!/^https?:/.test(a)&&f.autocomplete("search")}).autocomplete("widget").addClass("wplink-autocomplete").attr("role","listbox").removeAttr("tabindex")},correctURL:function(){var b=a.trim(h.url.val());b&&f!==b&&!/^(?:[a-z]+:|#|\?|\.|\/)/.test(b)&&(h.url.val("http://"+b),f=b)},open:function(b,c,d,f){var i,j=a(document.body);j.addClass("modal-open"),g=f,wpLink.range=null,b&&(window.wpActiveEditor=b),window.wpActiveEditor&&(this.textarea=a("#"+window.wpActiveEditor).get(0),"undefined"!=typeof window.tinymce&&(j.append(h.backdrop,h.wrap),i=window.tinymce.get(window.wpActiveEditor),e=i&&!i.isHidden()?i:null,e&&window.tinymce.isIE&&(e.windowManager.wplinkBookmark=e.selection.getBookmark())),!wpLink.isMCE()&&document.selection&&(this.textarea.focus(),this.range=document.selection.createRange()),h.wrap.show(),h.backdrop.show(),wpLink.refresh(c,d),a(document).trigger("wplink-open",h.wrap))},isMCE:function(){return e&&!e.isHidden()},refresh:function(a,b){var c="";wpLink.isMCE()?wpLink.mceRefresh(a,b):(h.wrap.hasClass("has-text-field")||h.wrap.addClass("has-text-field"),document.selection?c=document.selection.createRange().text||b||"":"undefined"!=typeof this.textarea.selectionStart&&this.textarea.selectionStart!==this.textarea.selectionEnd&&(b=this.textarea.value.substring(this.textarea.selectionStart,this.textarea.selectionEnd)||b||""),h.text.val(b),wpLink.setDefaultValues()),i?h.url.focus().blur():window.setTimeout(function(){h.url.focus()[0].select()}),f=h.url.val().replace(/^http:\/\//,"")},hasSelectedText:function(a){var b,c,d,f=e.selection.getContent();if(/]+>[^<]+<\/a>$/.test(f)||-1===f.indexOf("href=")))return!1;if(a){if(c=a.childNodes,0===c.length)return!1;for(d=c.length-1;d>=0;d--)if(b=c[d],3!=b.nodeType&&!window.tinymce.dom.BookmarkManager.isBookmarkNode(b))return!1}return!0},mceRefresh:function(c,f){var g,i=d(),j=this.hasSelectedText(i);i?(g=i.innerText||i.textContent,a.trim(g)||(g=f||""),c=c||e.dom.getAttrib(i,"href"),"_wp_link_placeholder"!==c?(h.url.val(c),h.openInNewTab.prop("checked","_blank"===e.dom.getAttrib(i,"target")),h.submit.val(b.update)):this.setDefaultValues(g)):(g=e.selection.getContent({format:"text"})||f||"",this.setDefaultValues(g)),j?(h.text.val(g),h.wrap.addClass("has-text-field")):(h.text.val(""),h.wrap.removeClass("has-text-field"))},close:function(b){a(document.body).removeClass("modal-open"),"noReset"!==b&&(wpLink.isMCE()?(e.plugins.wplink&&e.plugins.wplink.close(),e.focus()):(wpLink.textarea.focus(),wpLink.range&&(wpLink.range.moveToBookmark(wpLink.range.getBookmark()),wpLink.range.select()))),h.backdrop.hide(),h.wrap.hide(),f=!1,a(document).trigger("wplink-close",h.wrap)},getAttrs:function(){return wpLink.correctURL(),{href:a.trim(h.url.val()),target:h.openInNewTab.prop("checked")?"_blank":""}},buildHtml:function(a){var b='"},update:function(){wpLink.isMCE()?wpLink.mceUpdate():wpLink.htmlUpdate()},htmlUpdate:function(){var a,d,e,f,g,i,j,k=wpLink.textarea;k&&(a=wpLink.getAttrs(),d=h.text.val(),a.href&&(e=wpLink.buildHtml(a),document.selection&&wpLink.range?(k.focus(),wpLink.range.text=e+(d||wpLink.range.text)+"",wpLink.range.moveToBookmark(wpLink.range.getBookmark()),wpLink.range.select(),wpLink.range=null):"undefined"!=typeof k.selectionStart&&(f=k.selectionStart,g=k.selectionEnd,j=d||k.value.substring(f,g),e=e+j+"",i=f+e.length,f!==g||j||(i-=4),k.value=k.value.substring(0,f)+e+k.value.substring(g,k.value.length),k.selectionStart=k.selectionEnd=i),wpLink.close(),k.focus(),c.a11y.speak(b.linkInserted)))},mceUpdate:function(){var a,f,g=wpLink.getAttrs();return e.focus(),window.tinymce.isIE&&e.windowManager.wplinkBookmark&&(e.selection.moveToBookmark(e.windowManager.wplinkBookmark),e.windowManager.wplinkBookmark=null),g.href?(a=d(),h.wrap.hasClass("has-text-field")&&(f=h.text.val()||g.href),a?(f&&("innerText"in a?a.innerText=f:a.textContent=f),g["data-wplink-edit"]=null,e.dom.setAttribs(a,g)):f?e.selection.setNode(e.dom.create("a",g,e.dom.encode(f))):e.execCommand("mceInsertLink",!1,g),wpLink.close("noReset"),e.focus(),e.nodeChanged(),void c.a11y.speak(b.linkInserted)):(e.execCommand("unlink"),void wpLink.close())},keydown:function(a){var b;27===a.keyCode?(wpLink.close(),a.stopImmediatePropagation()):9===a.keyCode&&(b=a.target.id,"wp-link-submit"!==b||a.shiftKey?"wp-link-close"===b&&a.shiftKey&&(h.submit.focus(),a.preventDefault()):(h.close.focus(),a.preventDefault()))},getUrlFromSelection:function(b){var c=/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i,d=/^(https?|ftp):\/\/[A-Z0-9.-]+\.[A-Z]{2,4}[^ "]*$/i;return b||(this.isMCE()?b=e.selection.getContent({format:"text"}):document.selection&&wpLink.range?b=wpLink.range.text:"undefined"!=typeof this.textarea.selectionStart&&(b=this.textarea.value.substring(this.textarea.selectionStart,this.textarea.selectionEnd))),b=a.trim(b),b&&c.test(b)?"mailto:"+b:b&&d.test(b)?b.replace(/&|?38;/gi,"&"):""},setDefaultValues:function(a){h.url.val(this.getUrlFromSelection(a)),h.submit.val(b.save)}},a(document).ready(wpLink.init)}(jQuery,window.wpLinkL10n,window.wp);