var ects_searchbox_init = function() {
var init = function(options) {
jQuery(function($) {
var $root = $('#' + options.root);
$root.addClass('ects_root');
$('head').append("");
$(function() {
$root.html(
" " +
"
");
var updatehints = function(input, hintsroot, data) {
hintsroot.find('*').remove();
if (data.hits.length > 0) {
for (i=0; i" + data.hits[i].html + "");
hintsroot.append(item);
item.data('key', data.hits[i].key);
if (item.hasClass('option')) {
item.hover(
function() { $(this).addClass('selected'); },
function() { $(this).removeClass('selected'); }
);
item.click(function() {
if ($(this).data('key'))
input.attr('value', $(this).data('key'));
input.blur();
hintsroot.hide();
if (input.data('autosubmit'))
input.closest('form').submit();
});
}
}
hintsroot.show();
} else {
hintsroot.hide();
}
}
$('.textbox_container').each(function() {
var container = $(this);
var label = $(this).attr('label');
var input = $(this).find('input');
input.focus(function() { container.addClass('focused_input'); });
input.blur(function() { container.removeClass('focused_input'); });
if (label) {
var placeholder = $("" + label + "
");
$(this).prepend(placeholder);
$(this).click(function() { input.focus(); });
input
.focus(function() { placeholder.hide(); })
.blur(function() { if (!input.attr('value')) placeholder.show(); });
if (!input.attr('value')) placeholder.show();
}
if ($(this).attr('hintsfrom')) {
var service = $(this).attr('hintsfrom');
input.data('autosubmit', $(this).attr('autosubmit') ? true : false);
var hintsroot = $("");
var sentnumber = 0;
var receivednumber = 0;
var cache = {};
$(this).before(hintsroot);
hintsroot.hide();
hintsroot.css({
"top": container.position().top + container.height() + 4,
"left": container.position().left
});
input.keydown(function(e) {
if (e.which == 27) { // esc
hintsroot.hide();
} else if (e.which == 38 || e.which == 40) { // up || down
items = hintsroot.find("div.option");
if (items.length > 0)
{
selected = hintsroot.find("div.selected");
if (selected.length == 0)
selected = (e.which==38) ? items.first() : items.last();
selected.removeClass("selected");
if (e.which == 38) { // up
selected = selected.prevAll('div.option').first();
if (selected.length == 0)
selected = items.last('div.option');
} else {
selected = selected.nextAll('div.option').first();
if (selected.length == 0)
selected = items.first('div.option');
}
selected.addClass("selected");
}
} else if (e.which == 37 || e.which == 39 || e.which == 9) { // left || right || tab
// ignore
} else if (e.which == 13) { // enter
selected = hintsroot.find("div.selected");
if (selected.length > 0) {
e.returnValue = false;
if (e.preventDefault)
e.preventDefault();
if (e.stopPropagation)
e.stopPropagation();
selected.click();
}
} else {
// przetwarzamy w keyup
}
});
input.keyup(function(e) {
if (e.which == 27 || e.which == 38 || e.which == 40 || e.which == 37 || e.which == 39 || e.which == 9) { // esc || up || down || left || right || tab
// ignore
} else {
var value = input.attr('value');
if (value.length >= 3) {
if (cache[value]) {
updatehints(input, hintsroot, cache[value]);
} else {
$.ajax({
url: service + "?type=jsonp-hints&n=" + sentnumber + "&q=" + encodeURIComponent(value),
jsonpCallback: 'ects_n' + sentnumber,
dataType: 'jsonp',
success: function(data) {
if (Number(data.n) < receivednumber)
return;
receivednumber = data.n;
cache[value] = data;
updatehints(input, hintsroot, data);
}
});
sentnumber++;
}
}
else
{
hintsroot.hide();
hintsroot.find('*').remove();
}
}
});
input.focus(function() { $(this).keyup(); });
input.blur(function() {
setTimeout(function() { hintsroot.hide(); }, 200);
});
}
});
});
});
};
return init;
}();