/**
 * Copyright (c) 2013 by InstantSuggest.com
 * Released under GPL2 License, see http://www.gnu.org/licenses/gpl-2.0.html
 * For contribution, see http://www.instantsuggest.com/contribution
 * For contacting, see http://www.instantsuggest.com/contact
 */
(function(){"undefined"==typeof InstantSuggest||"undefined"==typeof CKEDITOR||($IS.CKEditor4Plugin=function(){},$IS.CKEditor4Plugin.prototype.init=function(a,b){this.editor=a;this.url=b;a.config.toolbarGroups&&a.config.toolbarGroups.push?(a.config.toolbarGroups.push($IS.pluginName),this.toolbarGroupName=$IS.pluginName):a.config.toolbar&&a.config.toolbar.push?(a.config.toolbar.push([$IS.OptionsDialogModule.buttonName,$IS.StartSelectionModule.buttonName,$IS.KeywordMenuModule.buttonName,$IS.KeywordResearchModule.buttonName,
$IS.ArticleSearchModule.buttonName]),this.toolbarGroupName=$IS.pluginName):this.toolbarGroupName="basicstyles";this.addOptionsDialogModule();this.addStartSelectionModule();this.addEndSelectionModule();this.addKeywordMenuModule();this.addContentModule();this.addKeywordResearchModule();this.addArticleSearchModule()},$IS.CKEditor4Plugin.prototype.addOptionsDialogModule=function(){var a=this.editor;a.addCommand($IS.OptionsDialogModule.commandName,new CKEDITOR.dialogCommand($IS.OptionsDialogModule.dialogName));
CKEDITOR.dialog.add($IS.OptionsDialogModule.dialogName,this.url+"options.js?ver="+$IS.version);a.ui.addButton($IS.OptionsDialogModule.buttonName,{label:$IS.OptionsDialogModule.buttonTitle,icon:$IS.OptionsDialogModule.buttonIcon,command:$IS.OptionsDialogModule.commandName,toolbar:this.toolbarGroupName});a.setKeystroke(CKEDITOR.CTRL+CKEDITOR.SHIFT+$IS.Event.KEY.O,$IS.OptionsDialogModule.commandName)},$IS.CKEditor4Plugin.prototype.addStartSelectionModule=function(){var a=this,b=a.editor;b.addCommand($IS.StartSelectionModule.commandName,
{exec:function(b){var c=b.getSelection().getRanges();0<c.length&&(c=c[0],b=b.createRange(),b.setStart(c.startContainer,c.startOffset),a.selectionRange=b)}});b.ui.addButton($IS.StartSelectionModule.buttonName,{label:$IS.StartSelectionModule.buttonTitle,icon:$IS.StartSelectionModule.buttonIcon,command:$IS.StartSelectionModule.commandName,toolbar:a.toolbarGroupName});b.setKeystroke(CKEDITOR.CTRL+CKEDITOR.SHIFT+$IS.Event.KEY.S,$IS.StartSelectionModule.commandName)},$IS.CKEditor4Plugin.prototype.addEndSelectionModule=
function(){var a=this;a.editor.addCommand($IS.EndSelectionModule.commandName,{exec:function(b,e){var c=b.getSelection(),f=c.getRanges();if(0<f.length&&(f=f[f.length-1],a.selectionRange)){var h=a.selectionRange;h.setEnd(f.endContainer,f.endOffset);c.selectRanges([h]);e&&e.call(b)}}})},$IS.CKEditor4Plugin.prototype.addKeywordMenuModule=function(){var a=this,b=a.editor,e,c;a.menuGroupName=$IS.pluginName;b.addMenuGroup(a.menuGroupName,1);var f=function(){c=!0;e||(h.call(this),e=CKEDITOR.tools.setTimeout(h,
200,this))},h=function(){e=null;c&&(CKEDITOR.tools.setTimeout(m,0,this),c=!1)},p=function(a){var k=0,d=0,g=a.getRanges(!0),c;0<g.length&&(c=a.createBookmarks(),d=g[0].clone(),k=b.document.createElement("span"),k.setHtml("&#65279;"),d.insertNode(k),d=k.getClientRect(),k.remove(),k=d.left,d=d.bottom,a.selectBookmarks(c));return{x:k,y:d}},m=function(){var c=b.getSelection(),k=c.getSelectedText(),d=CKEDITOR.tools.trim(k);if(!(d.length<$IS.keywordToolPreference.getOptionValue("minKeywordLength")||d.length>
$IS.keywordToolPreference.getOptionValue("maxKeywordLength"))){var g=$IS.keywordProviderManager.getProvider(),f=p(c),e=null,h=null;g.getSuggestions({keyword:CKEDITOR.tools.trim(k)},{async:!1,beforeSend:function(){h=c.createBookmarks();e=b.document.createElement("div",{attributes:{contenteditable:!1},styles:{position:"fixed",top:0,left:0,"z-index":9999,"background-color":"white",opacity:0.5,filter:"Alpha(opacity=50)",display:"block",width:"100%",height:"100%",cursor:"wait"}});b.document.getBody().append(e)},
complete:function(){e&&(e.setStyle("cursor","text"),e.remove(),e=null);h&&c.selectBookmarks(h)},success:function(c){$IS.Api.log(g,"m",c);if(0!=c.length){var d=new CKEDITOR.menu(b),e={};d.addListener(function(){return e});var h=function(a){a=a.data[0];if(a===d._.panel){var c=a._.panel._.currentBlock;c.element.on("keyup",function(a){var e=c._.focusIndex,g="";(e=0<=e&&c.element.getElementsByTag("a").getItem(e))&&(g=CKEDITOR.tools.trim(e.getAttribute("title")));if(g)switch(a.data.getKeystroke()){case $IS.Event.KEY.S:a=
$IS.keywordProviderManager.getProvider().getSearchUrl({keyword:g});window.open(a,"_blank");d.hide();break;case $IS.Event.KEY.K:$IS.ContentDialogModule.params={type:"keyword",keyword:g};b.execCommand($IS.ContentDialogModule.commandName);d.hide();break;case $IS.Event.KEY.A:$IS.ContentDialogModule.params={type:"article",keyword:g},b.execCommand($IS.ContentDialogModule.commandName),d.hide()}})}};b.on("menuShow",h);d.onHide=function(){b.removeListener("menuShow",h);for(var a in e)b.removeMenuItem(a)};
for(var n=function(c,d){var g=$IS.pluginName+d;b.addMenuItem(g,{label:c,group:a.menuGroupName,onClick:function(){b.insertText($IS.Tools.formatSuggestion(k,c))}});e[g]=CKEDITOR.TRISTATE_ON},l=0;l<c.length;l++)n(c[l],l);c=b.document.getDocumentElement();d.show(c,null,f.x,f.y)}}})}};b.on("contentDom",function(){var a=b.editable(),c=a.isInline();a.attachListener(a,"keyup",function(a){a=a.data;var c=a.getKey();a.$.shiftKey&&(c==$IS.Event.KEY.LEFT||c==$IS.Event.KEY.RIGHT)&&$IS.keywordToolPreference.getOptionValue("enableKeySelectionMode")&&
f.call(b)});if(c?CKEDITOR.env.webkit||CKEDITOR.env.gecko:CKEDITOR.env.opera){var d;a.attachListener(a,"mousedown",function(a){1===$IS.Event.getMouseButton(a.data.$)&&(d=1)});a.attachListener(b.document.getDocumentElement(),"mouseup",function(a){d&&1===$IS.Event.getMouseButton(a.data.$)&&$IS.keywordToolPreference.getOptionValue("enableMouseSelectionMode")&&f.call(b);d=0})}else c=CKEDITOR.env.ie?a:b.document.getDocumentElement(),a.attachListener(c,"mouseup",function(a){1===$IS.Event.getMouseButton(a.data.$)&&
$IS.keywordToolPreference.getOptionValue("enableMouseSelectionMode")&&f.call(b)})});b.addCommand($IS.KeywordMenuModule.commandName,{exec:function(){b.execCommand($IS.EndSelectionModule.commandName,f)}});b.ui.addButton($IS.KeywordMenuModule.buttonName,{label:$IS.KeywordMenuModule.buttonTitle,icon:$IS.KeywordMenuModule.buttonIcon,command:$IS.KeywordMenuModule.commandName,toolbar:a.toolbarGroupName});b.setKeystroke(CKEDITOR.CTRL+CKEDITOR.SHIFT+$IS.Event.KEY.E,$IS.KeywordMenuModule.commandName)},$IS.CKEditor4Plugin.prototype.addContentModule=
function(){this.editor.addCommand($IS.ContentDialogModule.commandName,new CKEDITOR.dialogCommand($IS.ContentDialogModule.dialogName));CKEDITOR.dialog.add($IS.ContentDialogModule.dialogName,this.url+"content.js?ver="+$IS.version)},$IS.CKEditor4Plugin.prototype.addKeywordResearchModule=function(){var a=this.editor;a.addCommand($IS.KeywordResearchModule.commandName,{exec:function(){var b=CKEDITOR.tools.trim(a.getSelection().getSelectedText());b||(a.execCommand($IS.EndSelectionModule.commandName),b=CKEDITOR.tools.trim(a.getSelection().getSelectedText()));
$IS.ContentDialogModule.params={type:"keyword",keyword:b};a.execCommand($IS.ContentDialogModule.commandName)}});a.setKeystroke(CKEDITOR.CTRL+CKEDITOR.SHIFT+$IS.Event.KEY.K,$IS.KeywordResearchModule.commandName);a.ui.addButton($IS.KeywordResearchModule.buttonName,{label:$IS.KeywordResearchModule.buttonTitle,icon:$IS.KeywordResearchModule.buttonIcon,command:$IS.KeywordResearchModule.commandName,toolbar:this.toolbarGroupName})},$IS.CKEditor4Plugin.prototype.addArticleSearchModule=function(){var a=this.editor;
a.addCommand($IS.ArticleSearchModule.commandName,{exec:function(){var b=CKEDITOR.tools.trim(a.getSelection().getSelectedText());b||(a.execCommand($IS.EndSelectionModule.commandName),b=CKEDITOR.tools.trim(a.getSelection().getSelectedText()));$IS.ContentDialogModule.params={type:"article",keyword:b};a.execCommand($IS.ContentDialogModule.commandName)}});a.setKeystroke(CKEDITOR.CTRL+CKEDITOR.SHIFT+$IS.Event.KEY.A,$IS.ArticleSearchModule.commandName);a.ui.addButton($IS.ArticleSearchModule.buttonName,{label:$IS.ArticleSearchModule.buttonTitle,
icon:$IS.ArticleSearchModule.buttonIcon,command:$IS.ArticleSearchModule.commandName,toolbar:this.toolbarGroupName})},$IS.CKEditor4Plugin.prototype.register=function(){var a=this;CKEDITOR.plugins.add($IS.pluginName,{requires:"menu,dialog,toolbar",init:function(b){a.init(b,this.path)}})},(new $IS.CKEditor4Plugin).register())})();
