")
.hide()
.addClass(options.resultsClass)
.css("position", "absolute")
.appendTo("body");
var list = jQuery("
").appendTo(element).mouseover( function(event) {
active = jQuery("li", list).removeClass().index(target(event));
jQuery(target(event)).addClass(CLASSES.ACTIVE);
}).mouseout( function(event) {
jQuery(target(event)).removeClass();
}).click(function(event) {
jQuery(target(event)).addClass(CLASSES.ACTIVE);
select();
input.focus();
return false;
});
var listItems,
active = -1,
data,
term = "";
if( options.moreItems.length > 0 )
var moreItems = jQuery("")
.addClass("ac_moreItems")
.css("display", "none")
.html(options.moreItems)
.appendTo(element);
if( options.width > 0 )
element.css("width", options.width);
function target(event) {
var element = event.target;
while(element.tagName != "LI")
element = element.parentNode;
return element;
}
function moveSelect(step) {
active += step;
wrapSelection();
listItems.removeClass().eq(active).addClass(CLASSES.ACTIVE);
};
function wrapSelection() {
if (active < 0) {
active = listItems.size() - 1;
} else if (active >= listItems.size()) {
active = 0;
}
}
function limitNumberOfItems(available) {
return (options.max > 0) && (options.max < available)
? options.max
: available;
}
function fillList() {
list.empty();
var num = limitNumberOfItems(data.length);
for (var i=0; i < num; i++) {
if (!data[i])
continue;
var formatted = options.formatItem ? options.formatItem(data[i].data, i+1, num, data[i].value) : data[i].value;
if ( formatted === false )
continue;
jQuery("
- ").html( options.highlight(formatted, term) ).appendTo(list)[0].index = i;
}
listItems = list.find("li");
if ( options.selectFirst ) {
listItems.eq(0).addClass(CLASSES.ACTIVE);
active = 0;
}
if( options.moreItems.length > 0 ) moreItems.css("display", (data.length > num)? "block" : "none");
list.bgiframe();
}
return {
display: function(d, q) {
data = d;
term = q;
fillList();
},
next: function() {
moveSelect(1);
},
prev: function() {
moveSelect(-1);
},
hide: function() {
element.hide();
active = -1;
},
visible : function() {
return element.is(":visible");
},
current: function() {
return this.visible() && (listItems.filter("." + CLASSES.ACTIVE)[0] || options.selectFirst && listItems[0]);
},
show: function() {
element.css({
width: typeof options.width == "string" || options.width > 0 ? options.width : jQuery(input).width()
//height: jQuery(listItems[0]).height() * options.size,
}).below(input).show();
},
selected: function() {
return data && data[ listItems.filter("." + CLASSES.ACTIVE)[0].index ];
}
};
};
jQuery.Autocompleter.Selection = function(field, start, end) {
if( field.createTextRange ){
var selRange = field.createTextRange();
selRange.collapse(true);
selRange.moveStart("character", start);
selRange.moveEnd("character", end);
selRange.select();
} else if( field.setSelectionRange ){
field.setSelectionRange(start, end);
} else {
if( field.selectionStart ){
field.selectionStart = start;
field.selectionEnd = end;
}
}
field.focus();
};