CKEDITOR.plugins.add("ipsautolink",{init:function(c){c.widgets.add("ipsembedded",{inline:!1,upcast:function(a){if("div"==a.name&&a.hasClass("ipsEmbeddedVideo"))return!0}});new CKEDITOR.plugins.ipsautolink(c)}});
CKEDITOR.plugins.ipsautolink=function(c){this.urlRegex=/^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:localhost)|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?)(?::\d{2,5})?(?:[/?#]\S*)?$/i;this.handleKey=
function(a){13==a.data.keyCode?CKEDITOR.tools.setTimeout(function(){this._handleKey(a)},0,this):32==a.data.keyCode&&this._handleKey(a)};this._handleKey=function(a){for(var b=c.getSelection().getRanges(!0),d=0;d<b.length;d++)if(b[d].collapsed){var e=32==a.data.keyCode?b[d].getCommonAncestor(!0,!0):b[d].getCommonAncestor(!0,!0).getPrevious();e&&e instanceof CKEDITOR.dom.element&&this.replaceInElement(e)}};c.on("key",this.handleKey,this);this.handlePaste=function(a){if(a.data.dataTransfer.getTransferType(c)!=
CKEDITOR.DATA_TRANSFER_INTERNAL&&!(-1<a.data.dataValue.indexOf("<"))){for(var b=c.getSelection().getRanges(!0),d=0;d<b.length;d++)if(b[d].startOffset){var e=b[d].getPreviousEditableNode();if(e&&(e=e.getText(),"[url="==e.substr(-5)||"[img]"==e.substr(-5)||"[code]"==e.substr(-6)))return}if(b=this.replaceTextWithLink(a.data.dataValue))a.data.dataValue=b.getOuterHtml()}};c.on("paste",this.handlePaste,this,{},11);c.on("contentDom",function(){var a=this;$("."+c.id).closest("form").on("submit",function(){for(var b=
c.editable().getChildren(),d=0;d<b.count();d++)b.getItem(d)&&b.getItem(d)instanceof CKEDITOR.dom.element&&a.replaceInElement(b.getItem(d));c.updateElement()})},this);this.replaceInElement=function(a){if(!("a"==a.getName()||"pre"==a.getName()||a.getAttribute("ipsnoautolink"))){for(var b=a.getChildren(),d=0;d<b.count();d++){var e=b.getItem(d);if(e instanceof CKEDITOR.dom.text){var c=[],f="",h=!1,g=e.getText().split(" ");for(word in g){var j=this.replaceWord(g[word].trim());j?(f.length&&(c.push(new CKEDITOR.dom.text(f)),
f=""),c.push(j),h=!0):f+=g[word];word<g.length-1&&(f+=" ")}f.length&&c.push(new CKEDITOR.dom.text(f));if(1<c.length||h){for(f=0;f<c.length;f++)c[f].insertBefore(e);e.remove()}}else e&&e instanceof CKEDITOR.dom.element&&this.replaceInElement(e)}"p"==a.getName()&&this.replaceParagraphWithEmbed(a)}};this.replaceWord=function(a){var b=this.replaceTextWithLink(a);return b?b:(a=this.replaceTextWithEmoticon(a))?a:null};this.replaceTextWithLink=function(a){if(XRegExp.exec(a,this.urlRegex)){var b=new CKEDITOR.dom.element("a");
b.setAttribute("href",$("<textarea />").html(a).text());b.appendHtml(a);return b}return null};this.replaceTextWithEmoticon=function(a){for(key in c.config.ipsEmoticons){var b=RegExp("^"+key.replace(/([.?*+^$[\]\\(){}|-])/g,"\\$1")+"$","i");if(a.match(b))return a=new CKEDITOR.dom.element("img"),a.setAttribute("src",c.config.ipsEmoticons[key].image),c.config.ipsEmoticons[key].image_2x&&a.setAttribute("srcset",c.config.ipsEmoticons[key].image_2x),c.config.ipsEmoticons[key].width&&c.config.ipsEmoticons[key].height&&
(a.setAttribute("width",c.config.ipsEmoticons[key].width),a.setAttribute("height",c.config.ipsEmoticons[key].height)),a.setAttribute("data-emoticon","true"),a.setAttribute("alt",key),a.setAttribute("title",key),a}return null};this.replaceParagraphWithEmbed=function(a){for(var b=0,d=null,e=a.getChildren(),i=0;i<e.count();i++){var f=e.getItem(i);if(!(f instanceof CKEDITOR.dom.element)||"br"!=f.getName()&&(b++,1<b))return;if("a"==f.getName()&&1==f.getChildCount()&&!a.getAttribute("ipsNoEmbed"))for(var h=
f.getChildren(),g=0;g<e.count();g++)h.getItem(g)instanceof CKEDITOR.dom.text&&(d=f.getAttribute("href"))}d&&1==b&&ips.getAjax()(ips.getSetting("baseURL")+"?app=core&module=system&controller=editor&do=validateLink",{data:{url:d.replace(/&amp;/g,"&")},type:"post"}).done(function(b){b.embed?(b=CKEDITOR.dom.element.createFromHtml(b.preview),b.replace(a),"img"!=b.getName()&&(c.widgets.initOn(b,"ipsembedded"),$(document).trigger("contentChange",[$("body")]))):a.setAttribute("ipsNoEmbed","true")})}};