tinymce.PluginManager.add("link",function(b){function t(e){return function(){var t=b.settings.link_list;"string"==typeof t?tinymce.util.XHR.send({url:t,success:function(t){e(tinymce.util.JSON.parse(t))}}):"function"==typeof t?t(e):e(t)}}function p(t,l,e){return function n(t,i){return i=i||[],tinymce.each(t,function(t){var e={text:t.text||t.title};t.menu?e.menu=n(t.menu):(e.value=t.value,l&&l(e)),i.push(e)}),i}(t,e||[])}function e(t){var e,l,a,n,s,i,o,r,u,c,d,f,g={},h=b.selection,v=b.dom;function x(t){var e=n.find("#text");(!e.value()||t.lastControl&&e.value()==t.lastControl.text())&&e.value(t.control.text()),n.find("#href").value(t.control.value())}function m(){!a&&0===g.text.length&&s&&this.parent().parent().find("#text")[0].value(this.value())}e=h.getNode(),l=v.getParent(e,"a[href]"),s=function(t){var e=h.getContent();if(/</.test(e)&&(!/^<a [^>]+>[^<]+<\/a>$/.test(e)||-1==e.indexOf("href=")))return!1;if(t){var n,i=t.childNodes;if(0===i.length)return!1;for(n=i.length-1;0<=n;n--)if(3!=i[n].nodeType)return!1}return!0}(),g.text=a=l?l.innerText||l.textContent:h.getContent({format:"text"}),g.href=l?v.getAttrib(l,"href"):"",l?g.target=v.getAttrib(l,"target"):b.settings.default_link_target&&(g.target=b.settings.default_link_target),(f=v.getAttrib(l,"rel"))&&(g.rel=f),(f=v.getAttrib(l,"class"))&&(g.class=f),(f=v.getAttrib(l,"title"))&&(g.title=f),s&&(i={name:"text",type:"textbox",size:40,label:"Text to display",onchange:function(){g.text=this.value()}}),t&&(o={type:"listbox",label:"Link list",values:p(t,function(t){t.value=b.convertURL(t.value||t.url,"href")},[{text:"None",value:""}]),onselect:x,value:b.convertURL(g.href,"href"),onPostRender:function(){o=this}}),!1!==b.settings.target_list&&(b.settings.target_list||(b.settings.target_list=[{text:"None",value:""},{text:"New window",value:"_blank"}]),u={name:"target",type:"listbox",label:"Target",values:p(b.settings.target_list)}),b.settings.rel_list&&(r={name:"rel",type:"listbox",label:"Rel",values:p(b.settings.rel_list)}),b.settings.link_class_list&&(c={name:"class",type:"listbox",label:"Class",values:p(b.settings.link_class_list,function(t){t.value&&(t.textStyle=function(){return b.formatter.getCssText({inline:"a",classes:[t.value]})})})}),!1!==b.settings.link_title&&(d={name:"title",type:"textbox",label:"Title",value:g.title}),n=b.windowManager.open({title:"Insert link",data:g,body:[{name:"href",type:"filepicker",filetype:"file",size:40,autofocus:!0,label:"Url",onchange:function(t){var e=t.meta||{};o&&o.value(b.convertURL(this.value(),"href")),tinymce.each(t.meta,function(t,e){n.find("#"+e).value(t)}),e.text||m.call(this)},onkeyup:m},i,d,function(n){var i=[];if(tinymce.each(b.dom.select("a:not([href])"),function(t){var e=t.name||t.id;e&&i.push({text:e,value:"#"+e,selected:-1!=n.indexOf("#"+e)})}),i.length)return i.unshift({text:"None",value:""}),{name:"anchor",type:"listbox",label:"Anchors",values:i,onselect:x}}(g.href),o,r,u,c],onSubmit:function(t){var e;function n(t,e){var n=b.selection.getRng();tinymce.util.Delay.setEditorTimeout(b,function(){b.windowManager.confirm(t,function(t){b.selection.setRng(n),e(t)})})}function i(){var t={href:e,target:g.target?g.target:null,rel:g.rel?g.rel:null,class:g.class?g.class:null,title:g.title?g.title:null};l?(b.focus(),s&&g.text!=a&&("innerText"in l?l.innerText=g.text:l.textContent=g.text),v.setAttribs(l,t),h.select(l),b.undoManager.add()):s?b.insertContent(v.createHTML("a",t,v.encode(g.text))):b.execCommand("mceInsertLink",!1,t)}g=tinymce.extend(g,t.data),(e=g.href)?0<e.indexOf("@")&&-1==e.indexOf("//")&&-1==e.indexOf("mailto:")?n("The URL you entered seems to be an email address. Do you want to add the required mailto: prefix?",function(t){t&&(e="mailto:"+e),i()}):b.settings.link_assume_external_targets&&!/^\w+:/i.test(e)||!b.settings.link_assume_external_targets&&/^\s*www[\.|\d\.]/i.test(e)?n("The URL you entered seems to be an external link. Do you want to add the required http:// prefix?",function(t){t&&(e="http://"+e),i()}):i():b.execCommand("unlink")}})}b.addButton("link",{icon:"link",tooltip:"Insert/edit link",shortcut:"Meta+K",onclick:t(e),onPostRender:function(){var i=this,l=b.plugins.b2evo_shorttags;l?b.on("NodeChange",function(t){var e=b.selection.getNode(),n=l.selected();n?i.disabled(!0):i.disabled(!1),i.active(!n&&b.dom.is(e,"a[href]"))}):this.stateSelector="a[href]"}}),b.addButton("unlink",{icon:"unlink",tooltip:"Remove link",cmd:"unlink",onPostRender:function(){var i=this,l=b.plugins.b2evo_shorttags;l?b.on("NodeChange",function(t){var e=b.selection.getNode(),n=l.selected();n?i.disabled(!0):i.disabled(!1),i.active(!n&&b.dom.is(e,"a[href]"))}):this.stateSelector="a[href]"}}),b.addShortcut("Meta+K","",t(e)),b.addCommand("mceLink",t(e)),this.showDialog=e,b.addMenuItem("link",{icon:"link",text:"Insert/edit link",shortcut:"Meta+K",onclick:t(e),context:"insert",prependToContext:!0,onPostRender:function(){var i=this,l=b.plugins.b2evo_shorttags;l?b.on("NodeChange",function(t){var e=b.selection.getNode(),n=l.selected();n?i.disabled(!0):i.disabled(!1),i.active(!n&&b.dom.is(e,"a[href]"))}):this.stateSelector="a[href]"}})});