var SearchEverything=function(a){var g={months:"Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split(" "),resizeSearchInput:function(){var b=a("#se-metabox-text");b.css({width:b.closest("div").outerWidth(!0)-b.next("a").outerWidth(!0)})},handleWindowResize:function(){a(window).resize(g.resizeSearchInput)},handleMetaboxActions:function(){a(".meta-box-sortables").on("sortstop",function(a,e){e.item&&(e.item.length&&"se-metabox"===e.item[0].id)&&g.resizeSearchInput()});a(".postbox h3, .postbox .handlediv").on("click",
function(){var b=a(this).closest(".postbox");setTimeout(function(){b.hasClass("closed")||g.resizeSearchInput()},1)})},displayOwnResults:function(b,e){var d=0;a.each(e,function(e,c){var f=a('<li><div title="Click to insert link into post."><h6></h6><a href="" target="_blank"></a><p></p></div></li>');4<e||(d+=1,f.data(c),f.find("h6").text(c.post_title||"Title missing"),f.find("p").text(g.extractText(f,"post_content")),f.find("a").text(g.urlDomain(c.guid)).prop("title",c.title||"Title missing").prop("href",
c.guid),b.append(f))});return d},extractText:function(b,e){var d=a("<div>"+b.data(e)+"</div>").text();!d||0===d.length?d="No Excerpt":100<d.length&&(d=d.substring(0,100)+"\u2026");return d},displayExternalResults:function(b,e){var d=0;a.each(e,function(e,c){var f=a('<li><div title="Click to insert link into post."><h6></h6><a href="" target="_blank"></a><p></p></div></li>');4<e||(d+=1,f.data(c),f.find("h6").text(c.title||"Title missing"),f.find("p").text(g.extractText(f,"text_preview")),f.find("a").text(g.urlDomain(c.url)).prop("title",
c.title||"Title missing").prop("href",c.url),b.append(f))});return d},performSearch:function(){var b=a("#se-metabox-text"),e=a('<div id="se-metabox-results"><div id="se-metabox-external-results" class="se-metabox-results-list se-hidden"><h4>Results from around the web</h4><p class="se-instructions">Click to insert link into post.</p><ul></ul></div><div id="se-metabox-own-results" class="se-metabox-results-list"><h4>Results from your blog</h4><p class="se-instructions">Click to insert link into post.</p><ul></ul></div><div class="se-spinner"></div></div>'),
d=0;a("#se-metabox-results").remove();b.closest("div").after(e);a.ajax({url:b.data("ajaxurl"),method:"get",dataType:"json",data:{action:"search_everything",text:tinyMCE&&tinyMCE.activeEditor.getContent()||"",s:b.prop("value")||""},success:function(e){var c=a("#se-metabox-own-results"),f=c.find("ul"),h=a("#se-metabox-external-results"),i=h.find("ul");a(".se-spinner, .se-no-results").remove();window.externalSearchEnabled?0===e.external.length?a("#se-metabox-results").append('<p class="se-no-results">We haven\'t found any external resources for you.</p>'):
(h.show(),d=g.displayExternalResults(i,e.external),h.find("h4").text(h.find("h4").text()+" ("+d+")")):(c.before('<div id="se-metabox-own-powersearch" class="se-metabox-results-list"><h4>Results from around the web</h4><p>If you want to use external search, you need to enable it in your <a class="se-settings-link" href="options-general.php?page=extend_search" target="_blank"><strong>settings</strong></strong></a>.</p></div>'),a("#se-metabox-own-powersearch").show());0===e.own.length?(a("#se-metabox-results").append('<p class="se-no-results">It seems we haven\'t found any results for search term <strong>'+
b.prop("value")+" on your blog</strong>.</p>"),h.removeClass("se-hidden")):(c.show(),d=g.displayOwnResults(f,e.own),c.find("h4").text(c.find("h4").text()+" ("+d+")"))},error:function(){a(".se-spinner, .se-no-results").remove();a("#se-metabox-results").append('<p class="se-no-results">There was something wrong with the search. Please try again. If this happens a lot, please check out our <a href="http://wordpress.org/support/plugin/search-everything" target="_blank">Support forum</a>.</p>')}});e.on("click",
".se-settings-link",function(){a(this).parent().text("Thanks. Please refresh this page.")})},urlDomain:function(a){var e=document.createElement("a");e.href=a;return e.hostname},handleSearch:function(){var b=a("#se-metabox-text");b.on("keypress",function(e){13===e.which&&(e.preventDefault(),""!==a.trim(b.prop("value"))&&g.performSearch())});a("#se-metabox-search").on("click",function(e){e.preventDefault();""!==a.trim(b.prop("value"))&&g.performSearch()})},initResultBehaviour:function(){var b=a("#se-metabox");
b.on("click","#se-metabox-own-results li",function(e){var d=a('<div id="se-just-a-wrapper"><p><a target="_blank" class="se-box se-article"><span class="se-box-heading"><span class="se-box-heading-title"></span></span><span class="se-box-text"></span><span class="se-box-date"></span><span class="se-box-domain"></span></a></p></div>'),b=a(this),c;c=b.data("post_date").split(" ")[0].split("-");c=new Date(c[0],c[1]-1,c[2]);c=g.months[c.getMonth()]+" "+c.getDate()+" "+c.getFullYear();var f=a('a.se-box[href="'+
b.data("guid")+'"]',tinyMCE&&tinyMCE.activeEditor.contentWindow.document||document);"A"!==a(e.target).prop("tagName")&&(f.length?"P"===f.parent().prop("tagName")?f.closest("p").remove():f.remove():(d.find(".se-box-heading-title").text(b.data("post_title")||"Title missing".substring(0,50)),d.find(".se-box-heading-domain").text("("+g.urlDomain(b.data("guid"))+")"),d.find(".se-box-text").text(g.extractText(b,"post_content")),d.find(".se-box-date").text(c),d.find(".se-box-domain").text(g.urlDomain(b.data("guid"))),
d.find(".se-box").attr("href",b.data("guid")),send_to_editor&&send_to_editor(d.html())))});b.on("click","#se-metabox-external-results li",function(e){var d=a('<div id="se-just-a-wrapper"><p><a target="_blank" class="se-box se-article"><span class="se-box-heading"><span class="se-box-heading-title"></span></span><span class="se-box-text"></span><span class="se-box-date"></span><span class="se-box-domain"></span></a></p></div>'),b=a(this),c;c=b.data("published_datetime").split("T")[0].split("-");c=
new Date(c[0],parseInt(c[1],10)-1,parseInt(c[2],10));c=g.months[c.getMonth()]+" "+c.getDate()+" "+c.getFullYear();var f=a('a.se-box[href="'+b.data("url")+'"]',tinyMCE&&tinyMCE.activeEditor.contentWindow.document||document);"A"!==a(e.target).prop("tagName")&&(f.length?"P"===f.parent().prop("tagName")?f.closest("p").remove():f.remove():(d.find(".se-box-heading-title").text(b.data("title")||"Title missing".substring(0,50)),d.find(".se-box-heading-domain").text("("+g.urlDomain(b.data("article_id"))+")"),
d.find(".se-box-text").text(g.extractText(b,"text_preview")),d.find(".se-box-date").text(c),d.find(".se-box-domain").text(g.urlDomain(b.data("url"))),d.find(".se-box").attr("href",b.data("url")),send_to_editor&&send_to_editor(d.html())))});b.on("click",".se-metabox-results-list h4",function(){a(this).closest(".se-metabox-results-list").toggleClass("se-hidden")})}};return{initialize:function(){g.resizeSearchInput();g.handleWindowResize();g.handleMetaboxActions();g.handleSearch();g.initResultBehaviour();
return this}}}(jQuery);jQuery(function(){SearchEverything.initialize();jQuery("a.se-back").on("click",function(a){a.preventDefault();window.history.back()})});
