var shiftWindow = function () { scrollBy(0, -50) };
if (location.hash) setTimeout(shiftWindow, 1);
window.addEventListener("hashchange", shiftWindow);
var element = document.createElement("script");
element.src = "./jssearch.index.js";
document.body.appendChild(element);
var searchBox = $('#searchbox');
// search when typing in search field
searchBox.on("keyup", function(event) {
var query = $(this).val();
if (query == '' || event.which == 27) {
$('#search-resultbox').hide();
return;
} else if (event.which == 13) {
var selectedLink = $('#search-resultbox a.selected');
if (selectedLink.length != 0) {
document.location = selectedLink.attr('href');
return;
}
} else if (event.which == 38 || event.which == 40) {
$('#search-resultbox').show();
var selected = $('#search-resultbox a.selected');
if (selected.length == 0) {
$('#search-results').find('a').first().addClass('selected');
} else {
var next;
if (event.which == 40) {
next = selected.parent().next().find('a').first();
} else {
next = selected.parent().prev().find('a').first();
}
if (next.length != 0) {
var resultbox = $('#search-results');
var position = next.position();
// TODO scrolling is buggy and jumps around
// resultbox.scrollTop(Math.floor(position.top));
// console.log(position.top);
selected.removeClass('selected');
next.addClass('selected');
}
}
return;
}
$('#search-resultbox').show();
$('#search-results').html('No results');
var result = jssearch.search(query);
if (result.length > 0) {
var i = 0;
var resHtml = '';
for (var key in result) {
if (i++ > 20) {
break;
}
resHtml = resHtml +
'' + result[key].file.t + '' +
'' + result[key].file.d + '';
}
$('#search-results').html(resHtml);
}
});
// hide the search results on ESC
$(document).on("keyup", function(event) { if (event.which == 27) { $('#search-resultbox').hide(); } });
// hide search results on click to document
$(document).bind('click', function (e) { $('#search-resultbox').hide(); });
// except the following:
searchBox.bind('click', function(e) { e.stopPropagation(); });
$('#search-resultbox').bind('click', function(e) { e.stopPropagation(); });
});