var searchPage = {
batchSize: 20,
endIndex: 0,
query: null,
//arrays to keep track the facet selection
facetedkey: [],
facetedvalue: [],
init: function () {
queryly.groups = [];
queryly.QuerylyKey = 'a7dbcffb18bb41eb';
queryly.jquery = jQuery;
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++) {
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0].toLowerCase() == 'searchkey') {
searchPage.query = sParameterName[1] + ' ';
}
else if (sParameterName[0].toLowerCase() == 'groups') {
queryly.groups = sParameterName[1].split(",");
}
}
if (searchPage.query != '' && searchPage.query != null) {
searchPage.dofacetedsearch(0, '', '');
}
},
atiTrack: function (link) {
var title = link.text();
var href = link.attr('href');
var completedquery = searchPage.query.trim().replace(/&/g, '_').replace(/\s+/g, '_');
title = title.replace(/&/g, '_').replace(/\s+/g, '_');
try {
var atclickurl = '//logw348.ati-host.net/hit.xiti?s=538291&mc=' + completedquery + '&np=1&mcrg=1&click=IS';
new Image().src = atclickurl + '&_=' + Math.random();
}
catch (e) { }
},
//This render the faceted object into html. In the current rss feed, creator and pubdate are used in the facet.
renderFaceted: function (faceted) {
var html = '
Filter by Author
';
//build the filters for creator field
var creators = faceted.creator;
for (var i = 0; i < creators.length; i++) {
html = html + '';
}
html = html + 'Filter by Date
';
//build the filters for publication date, data are grouped into buckets. for example, 168 means "published in the past 168 hours"
var dates = faceted.pubDate;
for (var i = 0; i < dates.length; i++) {
var datename = '';
if (dates[i].key == '24') {
datename = "within 24 hours";
}
else if (dates[i].key == '168') {
datename = "within one week";
}
else if (dates[i].key == '720') {
datename = "within 30 days";
}
else if (dates[i].key == '8760') {
datename = "within a year";
}
else if (dates[i].key == '26280') {
datename = "within three years";
}
if (datename != '') {
html = html + '';
}
}
queryly.jquery('#faceteddata').html(html);
},
resultcallback: function (results) {
//retrieve metadata
var total = results.metadata.total;
searchPage.endIndex = results.metadata.endindex;
var completedquery = searchPage.query.trim().replace(/&/g, '_').replace(/\s+/g, '_');
var np = Math.ceil(searchPage.endIndex / searchPage.batchSize);
var hiturl = '//logw348.ati-host.net/hit.xiti?s=538291&p=internal_search_engine_page&s2=5&mc=' + completedquery + '&np=' + np;
new Image().src = hiturl + '&_=' + Math.random();
//if there is faceted data in results object, render it.
if (results.faceted != null && results.faceted != undefined) {
this.renderFaceted(results.faceted);
}
queryly.jquery('#resultdata').append("Search results for " + results.metadata.query + "
");
//loop through each result.
for (var i = 0; i < results.items.length; i++) {
//display search native ad if available
if (results.ads != null && results.ads.length > 0) {
for (var j = 0; j < results.ads.length; j++) {
if (results.ads[j].index == i) {
queryly.jquery('#resultdata').append(searchPage.renderitem(results.ads[j]));
break;
}
}
}
var item = results.items[i];
if (queryly.groups.length > 0 && queryly.lastgroup != item._group) {
if (item._group != '' && typeof item._group != 'undefined') {
queryly.jquery('#resultdata').append("Results from " + item._group.replace(/\//g, ' ') + " section
");
}
else {
queryly.jquery('#resultdata').append("Results from other sections
");
}
queryly.lastgroup = item._group;
}
var html = searchPage.renderitem(item);
queryly.jquery('#resultdata').append(html);
}
var pagerhtml = '';
if (total > searchPage.endIndex) {
pagerhtml = pagerhtml + 'Next Page
';
}
if (searchPage.endIndex > searchPage.batchSize) {
var prev = Math.max(0, searchPage.endIndex - 2 * searchPage.batchSize);
pagerhtml = pagerhtml + 'Previous Page
';
}
queryly.jquery('#resultdata').append(pagerhtml);
queryly.jquery(document).scrollTop();
},
renderitem: function (item) {
item.image = item.image.replace("http://", "//");
item.link = item.link.replace("http://", "//");
var trackcall = 'onmousedown = "queryly.util.trackClick(\'' + item.link + '\',\'' + searchPage.query + '\');searchPage.atiTrack(queryly.jquery(this));"';
if (item.isad == 1) {
trackcall = 'onmousedown = "queryly.util.trackAdClick(' + item._id + ')"';
}
var html = '';
}
var publishdate = item.pubdate + "... ";
if (item.isad == 1) {
publishdate = '';
}
html = html + '' + publishdate + item.description.replace("\"", "").replace("'", "").replace(/(([^\s]+\s\s*){24})(.*)/, "$1...") + '
';
html = html + '';
return html;
},
turnpage: function (index) {
queryly.jquery('#resultdata').html('');
var keys = '';
var values = ''
for (var i = 0; i < searchPage.facetedkey.length; i++) {
keys = keys + searchPage.facetedkey[i] + "|";
values = values + searchPage.facetedvalue[i] + "|";
}
var url = "https://api.queryly.com/json.aspx?queryly_key=" + queryly.QuerylyKey + "&query=" + searchPage.query + "&endindex=" + index + "&batchsize=" + searchPage.batchSize + "&callback=searchPage.resultcallback&showfaceted=true&timezoneoffset=" + (new Date(0)).getTimezoneOffset();
if (searchPage.facetedkey.length > 0) {
url = url + "&facetedkey=" + encodeURIComponent(keys) + "&facetedvalue=" + encodeURIComponent(values);
}
if (queryly.groups.length > 0) {
url = url + "&groups=" + queryly.groups.join();
}
//making the search call to Queryly server
queryly.jquery.getScript(url, function (data, textStatus, jqxhr) {
window.scrollTo(0, 0);
});
},
//similar with dosearch method, but with faceted turned on. It passes in the current facet selection if any.
dofacetedsearch: function (index, key, value) {
queryly.jquery('#resultdata').html('');
//assemble the rest api.
var url = "https://api.queryly.com/json.aspx?queryly_key=" + queryly.QuerylyKey + "&query=" + searchPage.query + "&endindex=" + index + "&batchsize=" + searchPage.batchSize + "&callback=searchPage.resultcallback&showfaceted=true&groupstimezoneoffset=" + (new Date(0)).getTimezoneOffset();
queryly.lastgroup = '';
if (queryly.groups.length > 0) {
url = url + "&groups=" + queryly.groups.join();
}
if (key != '') {
if (searchPage.facetedkey.indexOf(key) >= 0) {
var i = searchPage.facetedkey.indexOf(key);
searchPage.facetedvalue[i] = value;
}
else {
searchPage.facetedkey.push(key);
searchPage.facetedvalue.push(value);
}
var keys = '';
var values = ''
for (var i = 0; i < searchPage.facetedkey.length; i++) {
keys = keys + searchPage.facetedkey[i] + "|";
values = values + searchPage.facetedvalue[i] + "|";
}
url = url + "&facetedkey=" + encodeURIComponent(keys) + "&facetedvalue=" + encodeURIComponent(values);
}
else {
searchPage.facetedkey = [];
searchPage.facetedvalue = [];
}
//making the search call to Queryly server
queryly.jquery.getScript(url, function (data, textStatus, jqxhr) {
window.scrollTo(0, 0);
});
}
}
searchPage.init();