/**
 * 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||CKEDITOR.dialog.add($IS.ContentDialogModule.dialogName,function(y){var f=this;f.id=0;f.uiElements={};f.relationId=0;f.styleLoaded=!1;f.loading=!1;var p=function(a){if(f.uiElements[a.id])return f.uiElements[a.id];var c=[],b=CKEDITOR.dialog.getCurrent();CKEDITOR.dialog._.uiElementBuilders.button.build(b,a,c);c=c.join("");return f.uiElements[a.id]=c},h=function(a,c){var b=CKEDITOR.dialog.getCurrent(),e=null;c&&(e=(e=b.getContentElement(c,
a))?e.getElement():null);e||(e=b.getElement().getDocument().getById(a));return e},s=function(a){for(var c=h("keyword_list").getElementsByTag("input"),b=0;b<c.count();b++){var e=c.getItem(b);e.hasClass("value")&&(e.$.checked=a)}h("keyword_selected").setStyle("display","none");h("keyword_list").setStyle("display","")},t=function(a,c,b){if(b==f.relationId&&(b=c.h?c.h:'<h3 class="header">Recommended</h3>',"object"===typeof c&&("object"===typeof c.c&&c.c.length)&&(c=c.c),"object"===typeof c&&c.length)){for(var e=
[],m=!1,l=0;l<c.length;l++)m=!0,e.push('<div class="recommended">'),e.push('<h4 class="title"><a href="'+c[l].u+'" target="_blank">'+c[l].t+"</a></h4>"),e.push('<div class="content">'+c[l].c+"</div>"),e.push("</div>");m&&(a=h(a+"_recommendation",a+"_tab"),a.setHtml(b+e.join("")),a.getParent().setStyle("display",""))}},u=function(a,c){var b=CKEDITOR.dialog.getCurrent();b.selectPage(a+"_tab");c&&(b.setValueOf(a+"_tab",a+"_query",c),n(a,c))},n=function(a,c,b,e,m){if(!f.loading){f.loading=!0;e=e?e:a+
"_suggestions";b=b?b:1;f.relationId++;var l=a+"_tab",d=h(e,l),q=CKEDITOR.dialog.getCurrent();1==b&&(h(a+"_action_top",l).setHtml(""),h(a+"_action_bottom",l).setHtml(""));d.setHtml("");h(a+"_recommendation",l).setHtml("");h(a+"_recommendation",l).getParent().setStyle("display","none");(c=c?CKEDITOR.tools.trim(c):"")||(c=CKEDITOR.tools.trim(q.getValueOf(l,a+"_query")));if(1==b||"keyword"!=a)$IS.ContentDialogModule.researchKeywords[a]=c;switch(a){case "keyword":w(c,b,e,m);break;case "article":x(c,b)}}},
w=function(a,c,b,e){if(!(a.length<$IS.keywordToolPreference.getOptionValue("minKeywordLength")||a.length>$IS.keywordToolPreference.getOptionValue("maxKeywordLength"))){var m=f.relationId,l=CKEDITOR.dialog.getCurrent(),d=1==c?h(b,"keyword_tab"):h(b),q=$IS.keywordProviderManager.getProvider();q.getSuggestions({keyword:a},{async:!0,beforeSend:function(){d.setHtml('<img class="loading" src="'+$IS.basePath+'icons/loading.gif" />');d.setStyle("display","")},complete:function(){f.loading=!1},error:function(a){d.setHtml("");
d.setStyle("display","none")},success:function(b){e&&CKEDITOR.dom.element.get(e).remove(!0);b=b.length?b:b.suggestions?b.suggestions:[];$IS.Api.log(q,"d",b,function(a){t("keyword",a,m)});if(!b||!b.length)d.setHtml(""),d.setStyle("display","none");else{if(1==c){var g=function(a){var c=[];c.push(p({type:"button",id:a+"_select_all",label:"Select All",title:"Select all keywords",style:"",className:"select_all"}));c.push("&nbsp;");c.push(p({type:"button",id:a+"_deselect_all",label:"Deselect All",title:"Deselect all keywords",
style:"",className:"deselect_all"}));c.push("&nbsp;");c.push(p({type:"button",id:a+"_show_selected",label:"Show/Hide Selected Keywords",title:"Show/Hide selected keywords",style:"",className:"show_selected"}));a=l.getContentElement("keyword_tab",a).getElement();a.setHtml(c.join(""));c=a.getElementsByTag("a");for(a=0;a<c.count();a++){var b=c.getItem(a);if(b.hasClass("select_all"))b.on("click",function(){s(!0)});else if(b.hasClass("deselect_all"))b.on("click",function(){s(!1)});else if(b.hasClass("show_selected"))b.on("click",
function(){var a=h("keyword_selected",void 0).getStyle("display");if("none"!=a&&"hidden"!=a)h("keyword_selected").setStyle("display","none"),h("keyword_list").setStyle("display","");else{for(var a="",c=h("keyword_list").getElementsByTag("input"),b=0;b<c.count();b++){var e=c.getItem(b);e.hasClass("value")&&e.$.checked&&(a+="<li>"+unescape(e.$.value)+"</li>")}h("keyword_selected").setHtml(a);h("keyword_selected").setStyle("display","");h("keyword_list").setStyle("display","none")}})}};g("keyword_action_top");
g("keyword_action_bottom")}g=[];1==c&&(g.push('<ul id="keyword_selected" style="display:none;"></ul>'),g.push('<ul id="keyword_list">'));for(var k=0;k<b.length;k++){var v=++f.id,r=0;g.push("<li>");g.push('<input type="checkbox" class="value" value="'+escape(b[k])+'" />&nbsp;');b[k].toLowerCase()==a.toLowerCase()?g.push(b[k]):(r=c+1,g.push('<a page="'+r+'" container="keyword_list_'+v+'" class="keyword_link" href="javascript:void(0);">'+b[k]+"</a>"));g.push("&nbsp;");g.push(p({type:"button",id:"article"+
k+"_button",label:"A",title:"Search for articles",style:"",className:"article_link"}));0<r&&g.push('<ul class="keyword_list_children" id="keyword_list_'+v+'" style="display:none;"></ul>');g.push("</li>")}1==c&&g.push("</ul>");d.setHtml(g.join(""));d.setStyle("display","");b=d.getElementsByTag("a");for(g=0;g<b.count();g++)if(k=b.getItem(g),k.hasClass("keyword_link"))k.on("click",function(){n("keyword",this.getText(),this.getAttribute("page"),this.getAttribute("container"),this.$)});else if(k.hasClass("article_link"))k.on("click",
function(){var a=this.getParent().getFirst();u("article",unescape(a.$.value))})}}})}},x=function(a,c){if(a){var b=f.relationId,e=CKEDITOR.dialog.getCurrent(),m=h("article_suggestions","article_tab"),l=$IS.articleProviderManager.getProvider();l.getSuggestions({keyword:a,page:c},{async:!0,beforeSend:function(){m.setHtml('<img class="loading" src="'+$IS.basePath+'icons/loading.gif" />')},complete:function(){f.loading=!1},error:function(){m.setHtml("")},success:function(d){var f=d.length?d:d.suggestions?
d.suggestions:[],h=d.maxPage?d.maxPage:0;$IS.Api.log(l,"d",f,function(a){t("article",a,b)});if(!f||!f.length)m.setHtml("");else{d=function(b){var d=[];1<c&&(d.push(p({type:"button",id:b+"_previous",label:"Previous",title:"Show previous page",style:"",className:"previous"})),d.push("&nbsp;"));c<h?d.push(p({type:"button",id:b+"_next",label:"Next",title:"Show next page",style:"",className:"next"})):d.push(p({type:"button",id:b+"_more",label:"More",title:"Search for more articles",style:"",className:"more"}));
b=e.getContentElement("article_tab",b).getElement();b.setHtml(d.join(""));d=b.getElementsByTag("a");for(b=0;b<d.count();b++){var f=d.getItem(b);if(f.hasClass("previous"))f.on("click",function(){n("article",a,c-1)});else if(f.hasClass("next"))f.on("click",function(){n("article",a,c+1)});else if(f.hasClass("more"))f.on("click",function(){window.open(l.getSearchUrl({keyword:a}),"_blank")})}};d("article_action_top");d("article_action_bottom");d=[];for(var g=0;g<f.length;g++){var k=f[g];d.push('<div class="article"'+
(g<f.length-1?' style="margin-bottom:10px;"':"")+">");d.push('<h3 class="title"><a href="'+k.url+'" target="_blank">'+k.title+"</a></h3>");d.push('<div class="content">'+k.content+"</div>");d.push('<div class="meta">');d.push('Published by <a href="'+k.url+'" target="_blank">'+k.source+"</a> on "+k.date);d.push("</div>");d.push("</div>")}m.setHtml(d.join(""))}}})}};return{title:$IS.ContentDialogModule.dialogTitle,width:800,height:525,resizable:CKEDITOR.DIALOG_RESIZE_NONE,contents:[{id:"keyword_tab",
label:"Keyword Research",style:"overflow:auto;width:100%;height:100%;",elements:[{type:"hbox",children:[{type:"hbox",widths:[720,30,30],children:[{type:"text",id:"keyword_query",isChanged:!1,setup:function(){this.getInputElement().on("keyup",function(a){13==a.data.getKeystroke()&&n("keyword")})}},{type:"button",id:"keyword_button",label:"Suggest",title:"Suggest keywords",onClick:function(){n("keyword")}},{type:"button",id:"keyword_options_button",label:"Options",title:"Open options dialog",onClick:function(){var a=
this.getDialog();$IS.OptionsDialogModule.params.type="keyword";a._.editor.execCommand($IS.OptionsDialogModule.commandName)}}]}]},{type:"vbox",id:"keyword_action_top",children:[]},{type:"hbox",id:"keyword_output",widths:["75%","25%"],children:[{type:"vbox",id:"keyword_suggestions",className:"suggestions",children:[]},{type:"vbox",id:"keyword_recommendation",className:"recommendation",children:[],setup:function(){this.getElement().getParent().setStyle("display","none")}}]},{type:"vbox",id:"keyword_action_bottom",
children:[]}]},{id:"article_tab",label:"Search Articles",style:"overflow:auto;width:100%;height:100%;",elements:[{type:"hbox",children:[{type:"hbox",widths:[720,30,30],children:[{type:"text",id:"article_query",isChanged:!1,setup:function(){this.getInputElement().on("keyup",function(a){13==a.data.getKeystroke()&&n("article")})}},{type:"button",id:"article_button",label:"Search",title:"Search for articles",onClick:function(){n("article")}},{type:"button",id:"article_options_button",label:"Options",
title:"Open options dialog",onClick:function(){var a=this.getDialog();$IS.OptionsDialogModule.params.type="article";a._.editor.execCommand($IS.OptionsDialogModule.commandName)}}]}]},{type:"vbox",id:"article_action_top",children:[]},{type:"hbox",id:"article_output",widths:["75%","25%"],children:[{type:"vbox",id:"article_suggestions",className:"suggestions",children:[]},{type:"vbox",id:"article_recommendation",className:"recommendation",children:[],setup:function(){this.getElement().getParent().setStyle("display",
"none")}}]},{type:"vbox",id:"article_action_bottom",children:[]}]}],buttons:[{type:"button",id:"close",label:"Close",title:"Close Toolbox",onClick:function(){this.getDialog().hide()}},{type:"button",id:"help",label:"Help",title:"Go to help on $IS.com",onClick:function(){window.open($IS.helpLink,"_blank")}}],onShow:function(){if(!1==f.styleLoaded){var a=[];this.getElement().addClass("instantsuggest_content");a.push(".cke_reset_all.instantsuggest_content, .cke_reset_all.instantsuggest_content * {white-space:normal;}");
a.push(".cke_reset_all .suggestions ul {list-style-type:none;}");a.push(".cke_reset_all .recommendation .header {font-weight:bold;}");a.push(".cke_reset_all .recommended a {font-weight:bold;text-decoration:underline;cursor:pointer;}");a.push(".cke_reset_all .keyword_link {text-decoration:underline;cursor:pointer;}");a.push(".cke_reset_all .keyword_list_children {margin-left:15px;}");a.push(".cke_reset_all .article .title a {font-weight:bold;}");a.push(".cke_reset_all .article .meta {font-style:italic;}");
a.push(".cke_reset_all .article .meta a {text-decoration:underline;cursor:pointer;font-style:italic;}");this.getElement().getDocument().appendStyleText(a.join(""));f.styleLoaded=!0}this.setupContent();for(var a=$IS.ContentDialogModule.params.type,c=["keyword","article"],b=0;b<c.length;b++)if(a!=c[b]){var e=$IS.ContentDialogModule.researchKeywords[c[b]]?$IS.ContentDialogModule.researchKeywords[c[b]]:"";e&&(this.setValueOf(c[b]+"_tab",c[b]+"_query",e),n(c[b],e))}(c=$IS.ContentDialogModule.params.keyword)||
(c=$IS.ContentDialogModule.researchKeywords[a]?$IS.ContentDialogModule.researchKeywords[a]:"");u(a,c)}}})})();
