/**
 * 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
 */
ContentDialog={id:0,relationId:0,loading:!1,currentType:"keyword",init:function(){for(var a=tinyMCEPopup.getWindowArg("type","keyword"),b=["keyword","article"],c=0;c<b.length;c++)if(a!=b[c]){var d=this.getLastResearchKeyword(b[c]);d&&(document.getElementById(b[c]+"_query").value=d,this.getContent(b[c],d))}(b=tinyMCEPopup.getWindowArg("keyword",""))||(b=this.getLastResearchKeyword(a));this.openTab(a,b)},openOptionsDialog:function(a){global.$IS.OptionsDialogModule.params.type=a;tinyMCEPopup.editor.execCommand(global.$IS.OptionsDialogModule.commandName)},
showElement:function(a,b){b?tinyMCEPopup.dom.setStyle(a,"display",""):tinyMCEPopup.dom.setStyle(a,"display","none")},isVisible:function(a){a=tinyMCEPopup.dom.getStyle(a,"display");return"none"!=a&&"hidden"!=a},getLastResearchKeyword:function(a){return global.$IS.ContentDialogModule.researchKeywords[a]?global.$IS.ContentDialogModule.researchKeywords[a]:""},saveResearchKeyword:function(a,b){global.$IS.ContentDialogModule.researchKeywords[a]=b},onQueryKeyDown:function(a){a=a||window.event;13==global.$IS.Event.getKey(a)&&
(a=(a=global.$IS.Event.getTarget(a))?a.value:"")&&this.getContent(this.currentType,a)},openTab:function(a,b){this.currentType=a;mcTabs.displayTab(a+"_tab",a+"_panel");b&&(document.getElementById(a+"_query").value=b,this.getContent(a,b));setTimeout(function(){document.getElementById(a+"_query").select();document.getElementById(a+"_query").focus()},0)},checkAllKeywords:function(a){global.tinymce.each(tinyMCEPopup.dom.select("#keyword_list input.value"),function(b){b.checked=a});this.showElement("keyword_selected",
!1);this.showElement("keyword_list",!0)},showSelectedKeywords:function(){if(this.isVisible("keyword_selected"))this.showElement("keyword_selected",!1),this.showElement("keyword_list",!0);else{var a="";global.tinymce.each(tinyMCEPopup.dom.select("#keyword_list input.value"),function(b){b.checked&&(a+="<li>"+unescape(b.value)+"</li>")});tinyMCEPopup.dom.setHTML("keyword_selected",a);this.showElement("keyword_selected",!0);this.showElement("keyword_list",!1)}},showRecommendation:function(a,b,c){if(c==
this.relationId&&(c=b.h?b.h:"<h3>Recommended</h3>","object"===typeof b&&("object"===typeof b.c&&b.c.length)&&(b=b.c),"object"===typeof b&&b.length)){for(var d=[],k=!1,l=0;l<b.length;l++)k=!0,d.push('<div class="recommended">'),d.push('<h4 class="title"><a style="font-weight:bold;text-decoration:underline;cursor:pointer;white-space:normal;" href="'+b[l].u+'" target="_blank">'+b[l].t+"</a></h4>"),d.push('<div class="content" style="white-space:normal;">'+b[l].c+"</div>"),d.push("</div>");k&&(a+="_recommendation",
tinyMCEPopup.dom.setHTML(a,c+d.join("")),this.showElement(a,!0))}},getContent:function(a,b,c,d,k){if(!this.loading)switch(this.loading=!0,this.relationId++,d=d?d:a+"_suggestions",c=c?c:1,1==c&&tinyMCEPopup.dom.setHTML(tinyMCEPopup.dom.select("#"+a+"_panel .action"),""),tinyMCEPopup.dom.setHTML(d,""),tinyMCEPopup.dom.setHTML(a+"_recommendation",""),this.showElement(a+"_recommendation",!1),(b=b?tinymce.trim(b):"")||(b=tinymce.trim(document.getElementById(a+"_query").value)),(1==c||"keyword"!=a)&&this.saveResearchKeyword(a,
b),a){case "keyword":this.getKeywords(b,c,d,k);break;case "article":this.getArticles(b,c,d)}},getKeywords:function(a,b,c,d){if(!(a.length<global.$IS.keywordToolPreference.getOptionValue("minKeywordLength")||a.length>global.$IS.keywordToolPreference.getOptionValue("maxKeywordLength"))){var k=this,l=k.relationId,h=global.$IS.keywordProviderManager.getProvider();h.getSuggestions({keyword:a},{async:!0,beforeSend:function(){tinyMCEPopup.dom.setHTML(c,'<img class="loading" src="'+global.$IS.basePath+'icons/loading.gif" />')},
complete:function(){k.loading=!1},error:function(){tinyMCEPopup.dom.setHTML(c,"")},success:function(e){d&&tinyMCEPopup.dom.setOuterHTML(d,global.$IS.Tools.getTextContent(d));e=e.length?e:e.suggestions?e.suggestions:[];global.$IS.Api.log(h,"d",e,function(a){k.showRecommendation("keyword",a,l)});if(!e||!e.length)tinyMCEPopup.dom.setHTML(c,"");else{var f=[],g="";if(1==b){g=' class="root"';f.push('<ul class="root" id="keyword_selected" style="display:none;"></ul>');var m=[];m.push('<input type="button" value="Check All" onclick="ContentDialog.checkAllKeywords(true);" />&nbsp;');
m.push('<input type="button" value="Uncheck All" onclick="ContentDialog.checkAllKeywords(false);" />&nbsp;');m.push('<input type="button" value="Show/Hide Selected Keywords" onclick="ContentDialog.showSelectedKeywords();" />');tinyMCEPopup.dom.setHTML(tinyMCEPopup.dom.select("#keyword_panel .action"),m.join(""))}1==b&&f.push('<ul id="keyword_list"'+g+">");for(g=0;g<e.length;g++){var m=++k.id,n=0;f.push("<li>");f.push('<input type="checkbox" class="value" value="'+escape(e[g])+'" />');e[g].toLowerCase()==
a.toLowerCase()?f.push(e[g]):(n=b+1,f.push('<a id="keyword_link_'+m+'" href="javascript:;" onclick="ContentDialog.getContent(\'keyword\', \''+e[g]+"', "+n+", 'keyword_list_"+m+"', this);\">"),f.push(e[g]+"</a>"));f.push('&nbsp;<input type="button" value="A" onclick="ContentDialog.openTab(\'article\', \''+e[g]+"');\" />");0<n&&f.push('<ul id="keyword_list_'+m+'"></ul>');f.push("</li>")}1==b&&f.push("</ul>");tinyMCEPopup.dom.setHTML(c,f.join(""))}}})}},getArticles:function(a,b,c){if(a){var d=this,k=
this.relationId,l=global.$IS.articleProviderManager.getProvider();l.getSuggestions({keyword:a,page:b},{async:!0,beforeSend:function(){tinyMCEPopup.dom.setHTML(c,'<img class="loading" src="'+global.$IS.basePath+'icons/loading.gif" />')},complete:function(){d.loading=!1},error:function(){tinyMCEPopup.dom.setHTML(c,"")},success:function(h){var e=h.length?h:h.suggestions?h.suggestions:[];global.$IS.Api.log(l,"d",e,function(a){d.showRecommendation("article",a,k)});if(!e||!e.length)tinyMCEPopup.dom.setHTML(c,
"");else{var f=[],g=[];h=h.maxPage?h.maxPage:0;1<b&&g.push('<input type="button" value="Previous" onclick="ContentDialog.getContent(\'article\', \''+a+"', "+(b-1)+", '"+c+"');\" />&nbsp;");b<h?g.push('<input type="button" value="Next" onclick="ContentDialog.getContent(\'article\', \''+a+"', "+(b+1)+", '"+c+"');\" />&nbsp;"):g.push('<input type="button" value="More" onclick="window.open(\''+l.getSearchUrl({keyword:a})+"', '_blank');\" />");tinyMCEPopup.dom.setHTML(tinyMCEPopup.dom.select("#article_panel .action"),
g.join(""));for(g=0;g<e.length;g++)h=e[g],f.push('<div class="article">'),f.push('<h3 class="title"><a href="'+h.url+'" target="_blank">'+h.title+"</a></h3>"),f.push('<div class="content">'+h.content+"</div>"),f.push('<div class="meta">'),f.push('Published by <a href="'+h.url+'" target="_blank">'+h.source+"</a> on "+h.date),f.push("</div>"),f.push("</div>");tinyMCEPopup.dom.setHTML(c,f.join(""))}}})}}};
