var API_KEY="AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";var INDEX_ID=0;var TITLE_FIELD="title";var TEXT_FIELD="body";var URL_FIELD="url";var IMAGE_FIELD="image";var DATE_FIELD="date";var QUERY_URL=`api/v1/index/${INDEX_ID}/query`;var FILE_URL=`api/v1/index/${INDEX_ID}/file`;var STATUS_URL=`api/v1/index/${INDEX_ID}`;var IS_DATE_FIELD=false;var MIN_YEAR=0;var MAX_YEAR=0;var MIN_YEAR_FILTER=0;var MAX_YEAR_FILTER=0;var ranges=[];var status_loaded=false;let query="";let maxPages=-1;let currPage=1;let currSort='score';let mainAutocomplete=null;function update_filter(min,max)
{MIN_YEAR_FILTER=min;MAX_YEAR_FILTER=max;loadResults(1,currSort);}
function debounce(func,wait,immediate){var timeout;return function(){var context=this,args=arguments;var later=function(){timeout=null;if(!immediate)func.apply(context,args);};var callNow=immediate&&!timeout;clearTimeout(timeout);timeout=setTimeout(later,wait);if(callNow)func.apply(context,args);};}
function moveSplitter(y,hide=false){const[l1,l2,l3,l4]=[$("#l1"),$("#l2"),$("#l3"),$("#l4")];l1.attr("y2",y-70);l2.attr("y1",y-70);l2.attr("y2",y-30);l3.attr("y1",y-30);l3.attr("y2",y+10);l4.attr("y1",y+10);if(hide)$("#splitter").hide();else $("#splitter").show();}
function updateTitle(useDefault=false){const title=query&&!useDefault?`${query}-SeekStorm Search`:"SeekStorm Search";if(document.title!=title){document.title=title;}}
let active=null;function setHeader(xhr){xhr.setRequestHeader('apikey',API_KEY);}
function truncate(input,len)
{}
function loadResults(page,sort,ignoreHistory=false){updateTitle();active=null;if((currPage!=page||currSort!=sort)&&!ignoreHistory){var newURL=updateURLParameter(window.location.href,"q",query);newURL=updateURLParameter(newURL,"page",page);newURL=updateURLParameter(newURL,"sort",sort);window.history.pushState("","",newURL);}
currPage=page;currSort=sort;const contentBox=$("#contentBox");const prevUrl=$("#prevUrl");const prevTitle=$("#prevTitle");const prevText=$("#prevText");const prevText2=$("#prevText2");moveSplitter(-50,true);prevUrl.hide();prevTitle.text("");prevText2.text("");prevText.hide();if(!status_loaded)showStatus(false);var d_min=new Date(MIN_YEAR_FILTER,0,1,0,0,0,0);var timestamp_min=d_min.getTime()/1000;var d_max=new Date(MAX_YEAR_FILTER,0,1,0,0,0,0);var timestamp_max=d_max.getTime()/1000;var rewrite=document.activeElement===document.getElementById('searchMain');if(!rewrite){$("#searchMain").css("text-decoration","none");$("#searchMain").val(query);}
$.ajax({url:QUERY_URL,data:JSON.stringify({query:query,enable_empty_query:true,offset:(page-1)*10,length:10,query_rewriting:rewrite?{SearchRewrite:{distance:1,term_length_threshold:[2,8],correct:2,complete:2,length:5}}:{SearchSuggest:{distance:1,term_length_threshold:[2,8],correct:2,complete:2,length:5}},realtime:true,highlights:[{field:TITLE_FIELD,fragment_number:0,fragment_size:1000,highlight_markup:true},{field:TEXT_FIELD,fragment_number:2,fragment_size:160,highlight_markup:true},{field:TEXT_FIELD,name:"body2",fragment_number:0,fragment_size:4000,highlight_markup:true}],result_sort:sort=='score'?[]:sort=='new'?[{field:"date",order:"Descending",base:"None"}]:[{field:"date",order:"Ascending",base:"None"}],query_facets:ranges.length>0?[{Timestamp:{field:DATE_FIELD,range_type:"CountWithinRange",ranges:ranges}}]:[],facet_filter:IS_DATE_FIELD&&(MIN_YEAR!=MIN_YEAR_FILTER||MAX_YEAR+1!=MAX_YEAR_FILTER)?[{Timestamp:{field:"date",filter:[timestamp_min,timestamp_max]}}]:[]}),traditional:true,cache:false,type:"POST",dataType:'json',success:function(data){contentBox.empty();query=data.query;if(data.query.toLowerCase().startsWith(query.toLowerCase())){$("#searchMain").removeClass("wrong");}else{$("#searchMain").addClass("wrong");}
if(IS_DATE_FIELD){var newEntry1=$(`<div id="chart"style="width:100%;"><div id="histogramSlider"style="width:100%"></div><table style="display: none;"><tr id="data"><th>Value</th><th>Running Count</th></tr></table></div>`);contentBox.append(newEntry1);var numBins=MAX_YEAR-MIN_YEAR+1;var histogram_data={"items":[]};if(data.facets[DATE_FIELD]){data.facets[DATE_FIELD].forEach(element=>{var index=parseInt(element[0])-MIN_YEAR
var count=element[1];histogram_data.items.push({"index":index,"count":count});});}
$("#histogramSlider").histogramSlider({data:histogram_data,sliderRange:[MIN_YEAR,MAX_YEAR+1],optimalRange:[-100,-10],selectedRange:[MIN_YEAR_FILTER,MAX_YEAR_FILTER],height:60,numberOfBins:numBins,showTooltips:true,showSelectedRange:true});renderData(histogram_data);function renderData(data){var dataArray=[];for(var i=0;i<data.items.length;i++){dataArray.push(data.items[i].value);}
dataArray=dataArray.sort(function(a,b){return b-a;});var count=0,dataTable=$("#data");for(var i=0;i<dataArray.length;i++){count++;dataTable.after("<tr><td>"+dataArray[i]+"</td><td>"+count+"</td></tr>");if(round25000(dataArray[i])>round25000(dataArray[i+1])){count=0;dataTable.after("<tr><td>----------</td></tr>");}}}
function round25000(x)
{return Math.ceil(x/25000)*25000;}
let newEntry=$(`<div id="sort"class="resultEntry col-xs-12"style="display:flex"><span id="sortBy">sort by</span><div id="score"class="${sort=='score' ? 'active' : ''}"><span>score</span></div><div class="${sort=='new' ? 'active' : ''}"id="new"><span>newest</span></div><div class="${sort=='old' ? 'active' : ''}"id="old"><span>oldest</span></div></div>`);contentBox.append(newEntry);$("#sort div").click(event=>{var target_id=event.currentTarget.id;$("#sort div").each(function(){if($(this).attr("id")===target_id){$(this).addClass("active");}else{$(this).removeClass("active");}});loadResults(1,target_id);});}else{var input=data.original_query.trim();var suggestion=data.query;if(rewrite&&!suggestion.startsWith(input)){let newEntry=$(`<div id="did_you_mean"style="margin-bottom:140px"><span>searching for</span><span id="query_1"class="query_1">${suggestion}</span><span>.search for</span><span id="query_2"class="query_2">${input}</span><span>instead</span></div>`);contentBox.append(newEntry);$("#query_1").click(event=>{let target=$(event.target);const q=target.text();$("#searchMain").css("text-decoration","none");$("#searchMain").val(q);});$("#query_2").click(event=>{let target=$(event.target);const q=target.text();query=q;$("#searchMain").css("text-decoration","none");$("#searchMain").val(q);loadResults(1,'score');});}else{let newEntry=$(`<div style="margin-bottom:140px"><span>&nbsp;</span></div>`);contentBox.append(newEntry);}};data.results.forEach(element=>{var link_text=valid(element[URL_FIELD]);var title_text=valid(element[TITLE_FIELD]);var date_value=valid(element[DATE_FIELD]);var image_data=valid(element[IMAGE_FIELD]);var date_text=element.date?" - "+(new Date(date_value*1000)).toLocaleDateString("en-US",{year:'numeric',month:'numeric',day:'numeric'}):'';title_text=title_text+date_text;var is_pdf=link_text.toLowerCase().endsWith(".pdf");var is_link=link_text.toLowerCase().startsWith("http");var link_url=is_pdf?FILE_URL+"/"+valid(element['_id']):link_text;const iUrl=element.iurl;let newEntry=$(`<div class="resultEntry col-xs-12"><div class="row">${iUrl?`<div class="col-xs-2 imgPrev"style="background-image: url('${iUrl}')"></div>`:""}<div class="col-xs-${iUrl ? 10 : 12}"><h1 style="padding-bottom:4px;">${is_pdf?`<span class="pdf">PDF</span>`:``}<a href="${link_text}">${title_text}</a></h1><p class="kwic">${valid(element[TEXT_FIELD])}</p><p class="directLink"><a href="${link_url}"class="url">${link_text}</a></p></div></div></div>`);if(image_data.startsWith("data:image")){var image=new Image();image.src=image_data;image.height=100;newEntry[0].insertBefore(image,newEntry[0].firstChild);}
contentBox.append(newEntry);if(is_pdf){var url=FILE_URL+"/"+valid(element['_id']+"#search="+query);var filename=link_text.split(/\/|\\/).pop();const options={headers:{apikey:API_KEY}};newEntry.on("mouseenter",function(){moveSplitter(newEntry[0].getBoundingClientRect().y);prevUrl.html(link_text);prevUrl.show();prevTitle.html(title_text);fetch(url,options).then(res=>res.blob()).then(blob=>{var file=new File([blob],filename,{type:"application/pdf",});file=window.URL.createObjectURL(file);document.getElementById("prevText").src=file;});prevText.show();});$(newEntry).on("click","a.url",function(){fetch(url,options).then(res=>res.blob()).then(blob=>{var file=new File([blob],filename,{type:"application/pdf",});file=window.URL.createObjectURL(file);window.location.assign(file);});return false;});}else if(is_link){newEntry.on("mouseenter",function(){moveSplitter(newEntry[0].getBoundingClientRect().y);prevUrl.html(link_text);prevUrl.show();prevTitle.html(title_text);document.getElementById("prevText").src=link_url;prevText.show();});}else{newEntry.on("mouseenter",function(){moveSplitter(newEntry[0].getBoundingClientRect().y);prevTitle.html(title_text);prevText2.html(element.body2.replace(new RegExp("\r","gi"),"<br>"));});}});if(mainAutocomplete){mainAutocomplete.updateSuggestions(data.suggestions);}
setPage(page,data.count,data.count_total,data.time);},error:function(xhr){alert("error");},beforeSend:setHeader});}
function valid(element){return element?element:''}
function showStatus(async=true)
{$.ajax({url:STATUS_URL,data:{},traditional:true,cache:false,type:"GET",async:async,dataType:'json',success:function(data){$("#welcome").hide();$("#version").text(data.version);$("#indexName").text(data.name);$("#indexedDocs").text(data.indexed_doc_count.toLocaleString());let schema=new Map(Object.entries(data.schema).sort((a,b)=>a[1].field_id-b[1].field_id));for(let[key,value]of schema){if(value.field_type=="Timestamp"){IS_DATE_FIELD=true;}
if(value.field_type=="Binary"&&value.stored){if(!data.schema[IMAGE_FIELD])IMAGE_FIELD=key;continue;}
if(value.field_type=="U32"){continue;}
if((key!=TITLE_FIELD)&&(key!=TEXT_FIELD)&&(key!=URL_FIELD)&&(key!=DATE_FIELD)&&value.stored)
{if(!data.schema[TITLE_FIELD])TITLE_FIELD=key;else
if(!data.schema[TEXT_FIELD])TEXT_FIELD=key;else
if(!data.schema[URL_FIELD])URL_FIELD=key;else
if(!data.schema[DATE_FIELD]&&value.field_type=="Timestamp")DATE_FIELD=key;else
break;}}
if(IS_DATE_FIELD){MIN_YEAR=(new Date(data.facets_minmax[DATE_FIELD].min*1000)).getFullYear();MAX_YEAR=(new Date(data.facets_minmax[DATE_FIELD].max*1000)).getFullYear();MIN_YEAR_FILTER=MIN_YEAR;MAX_YEAR_FILTER=MAX_YEAR+1;for(let i=MIN_YEAR;i<=MAX_YEAR;i++){var d=new Date(i,0,1,0,0,0,0);var timestamp=d.getTime()/1000;var range=[i.toString(),timestamp];ranges.push(range);}}
status_loaded=true;},error:function(xhr){$("#landingSearch").hide();$('#welcome').css('display','flex');},beforeSend:setHeader});}
function setPage(page,results,count_total,statsTime){maxPages=Math.ceil(count_total/10);let vars=$("#footer").children();const[from,to,count,stats]=[$("#from"),$("#to"),$("#count"),$("#stats")];from.text(((page-1)*10+1).toLocaleString());to.text(Math.min(page*10,count_total).toLocaleString());count.text(count_total.toLocaleString());stats.attr("title",`${count_total.toLocaleString()} results in ${(statsTime/1000_000).toLocaleString(undefined,{minimumFractionDigits:0,maximumFractionDigits:3})}ms`);for(const element of vars){const elem=$(element);elem.removeClass("active");elem.show();}
if(page==1)$(vars[0]).hide();else $(vars[0]).show();if(page==maxPages)$(vars[11]).hide();else $(vars[11]).show();if(page>5){for(let i=0;i<10;i++){const element=$(vars[i+1]);element.text(page-5+i);if(i==5){element.addClass("active");}
if(page-5+i>maxPages){element.hide();}}}else{for(let i=0;i<10;i++){const element=$(vars[i+1]);element.text(i+1);if(i==page-1){element.addClass("active");}
if(i>=maxPages){element.hide();}}}}
$(function(){mainAutocomplete=new AutocompleteManager("#searchMain","#suggestions");$("#searchInput").on("input",function(e){e.preventDefault();$("#landingSearch").hide();$("#landingMain").show();$("#searchMain").val($("#searchInput").val()).trigger("input");$("#searchMain").focus();$("#searchInput").val("");});$("#footer, #topBar").on("mouseenter",function(){const prevUrl=$("#prevUrl");const prevTitle=$("#prevTitle");const prevText=$("#prevText");const prevText2=$("#prevText2");moveSplitter(-50,true);prevUrl.hide();prevTitle.text("");prevText2.text("");prevText.hide();});$("#topLogo").on("click",function(e){e.preventDefault();$("#landingSearch").show();$("#searchInput").focus();$("#landingMain").hide();updateTitle(true);window.history.pushState("","",window.location.href.split("?")[0]);});const mainSearch=$("#searchMain");mainSearch.on("input",debounce(()=>{query=mainSearch.val();loadResults(1,'score');},50));mainSearch.on("input",debounce(()=>{if(mainAutocomplete){mainAutocomplete.applyAutoCompletion();}},100));mainSearch.on("input",debounce(()=>{var newURL=updateURLParameter(window.location.href,"q",query);newURL=updateURLParameter(newURL,"page",1);window.history.pushState("","",newURL);},700));$("#footer div").click(event=>{let target=$(event.target);if(!target.hasClass("action")){loadResults(target.text(),currSort);}else{const cPage=parseInt($("#footer div.active")[0].innerText);if(target.attr("next")){loadResults(cPage+1,currSort);}else{loadResults(cPage-1,currSort);}}});window.addEventListener("popstate",function(e){checkUrl();});checkUrl();showStatus();});function checkUrl(){const queryStrings=parse_query_string(window.location.search.substring(1));if(queryStrings.api_key){API_KEY=queryStrings.api_key;}
if((queryStrings.index_id)&&!isNaN(queryStrings.index_id)){STATUS_URL=`api/v1/index/${queryStrings.index_id}`;QUERY_URL=`api/v1/index/${queryStrings.index_id}/query`;FILE_URL=`api/v1/index/${INDEX_ID}/file`;}
if(queryStrings.q){query=queryStrings.q;let loadPage=1;if(queryStrings.page)loadPage=parseInt(queryStrings.page);loadResults(loadPage,'score',true);$("#searchMain").val(query);$("#landingSearch").hide();$("#landingMain").show();}else{$("#landingSearch").show();$("#searchInput").focus();$("#landingMain").hide();}}
$(document).keydown(function(e){if($("#searchMain").is(":focus")||$("#footer div.active")[0]===undefined)
return;const cPage=parseInt($("#footer div.active")[0].innerText);switch(e.which){case 38:if(cPage>1)loadResults(cPage-1,'score');break;case 40:if(cPage<maxPages)loadResults(cPage+1,'score');break;default:return;}
e.preventDefault();});document.addEventListener("touchstart",handleTouchStart,false);document.addEventListener("touchmove",handleTouchMove,false);var xDown=null;var yDown=null;function getTouches(evt){return(evt.touches||evt.originalEvent.touches);}
function handleTouchStart(evt){const firstTouch=getTouches(evt)[0];xDown=firstTouch.clientX;yDown=firstTouch.clientY;}
function handleTouchMove(evt){if(!xDown||!yDown){return;}
var xUp=evt.touches[0].clientX;var yUp=evt.touches[0].clientY;var xDiff=xDown-xUp;var yDiff=yDown-yUp;if(Math.abs(xDiff)>Math.abs(yDiff)){const cPage=parseInt($("#footer div.active")[0].innerText);if(xDiff>0){if(cPage<maxPages)loadResults(cPage+1,'score');}else{if(cPage>1)loadResults(cPage-1,'score');}}else{if(yDiff>0){}else{}}
xDown=null;yDown=null;}
function updateURLParameter(url,param,paramVal){var TheAnchor=null;var newAdditionalURL="";var tempArray=url.split("?");var baseURL=tempArray[0];var additionalURL=tempArray[1];var temp="";if(additionalURL){var tmpAnchor=additionalURL.split("#");var TheParams=tmpAnchor[0];TheAnchor=tmpAnchor[1];if(TheAnchor)additionalURL=TheParams;tempArray=additionalURL.split("&");for(var i=0;i<tempArray.length;i++){if(tempArray[i].split("=")[0]!=param){newAdditionalURL+=temp+tempArray[i];temp="&";}}}else{var tmpAnchor=baseURL.split("#");var TheParams=tmpAnchor[0];TheAnchor=tmpAnchor[1];if(TheParams)baseURL=TheParams;}
if(TheAnchor)paramVal+="#"+TheAnchor;var rows_txt=temp+""+param+"="+paramVal;return baseURL+"?"+newAdditionalURL+rows_txt;}
function parse_query_string(query){var vars=query.split("&");var query_string={};for(var i=0;i<vars.length;i++){var pair=vars[i].split("=");if((pair.length==3)&&(pair[2]==""))pair[1]=pair[1]+"=";var key=decodeURIComponent(pair[0]);var value=decodeURIComponent(pair[1]);if(typeof query_string[key]==="undefined"){query_string[key]=decodeURIComponent(value);}else if(typeof query_string[key]==="string"){var arr=[query_string[key],decodeURIComponent(value)];query_string[key]=arr;}else{query_string[key].push(decodeURIComponent(value));}}
return query_string;}