(function($){
$.fn.extend({autocomplete:function(_2,_3){
var _4=typeof _2=="string";
_3=$.extend({},$.Autocompleter.defaults,{url:_4?_2:null,data:_4?null:_2,delay:_4?$.Autocompleter.defaults.delay:10,max:_3&&!_3.scroll?10:150},_3);
_3.highlight=_3.highlight||function(_5){
return _5;
};
_3.formatMatch=_3.formatMatch||_3.formatItem;
return this.each(function(){
new $.Autocompleter(this,_3);
});
},result:function(_6){
return this.bind("result",_6);
},search:function(_7){
return this.trigger("search",[_7]);
},flushCache:function(){
return this.trigger("flushCache");
},setOptions:function(_8){
return this.trigger("setOptions",[_8]);
},unautocomplete:function(){
return this.trigger("unautocomplete");
}});
$.Autocompleter=function(_9,_a){
var _b={UP:38,DOWN:40,DEL:46,TAB:9,RETURN:13,ESC:27,COMMA:188,PAGEUP:33,PAGEDOWN:34,BACKSPACE:8};
var _c=$(_9).attr("autocomplete","off").addClass(_a.inputClass);
var _d;
var _e="";
var _f=$.Autocompleter.Cache(_a);
var _10=0;
var _11;
var _12={mouseDownOnSelect:false};
var _13=$.Autocompleter.Select(_a,_9,selectCurrent,_12);
var _14;
$.browser.opera&&$(_9.form).bind("submit.autocomplete",function(){
if(_14){
_14=false;
return false;
}
});
_c.bind(($.browser.opera?"keypress":"keydown")+".autocomplete",function(_15){
_11=_15.keyCode;
switch(_15.keyCode){
case _b.UP:
_15.preventDefault();
if(_13.visible()){
_13.prev();
}else{
onChange(0,true);
}
break;
case _b.DOWN:
_15.preventDefault();
if(_13.visible()){
_13.next();
}else{
onChange(0,true);
}
break;
case _b.PAGEUP:
_15.preventDefault();
if(_13.visible()){
_13.pageUp();
}else{
onChange(0,true);
}
break;
case _b.PAGEDOWN:
_15.preventDefault();
if(_13.visible()){
_13.pageDown();
}else{
onChange(0,true);
}
break;
case _a.multiple&&$.trim(_a.multipleSeparator)==","&&_b.COMMA:
case _b.TAB:
case _b.RETURN:
if(selectCurrent()){
_15.preventDefault();
_14=true;
return false;
}
break;
case _b.ESC:
_13.hide();
break;
default:
clearTimeout(_d);
_d=setTimeout(onChange,_a.delay);
break;
}
}).focus(function(){
_10++;
}).blur(function(){
_10=0;
if(!_12.mouseDownOnSelect){
hideResults();
}
}).click(function(){
if(_10++>1&&!_13.visible()){
onChange(0,true);
}
}).bind("search",function(){
var fn=(arguments.length>1)?arguments[1]:null;
function findValueCallback(q,_18){
var _19;
if(_18&&_18.length){
for(var i=0;i<_18.length;i++){
if(_18[i].result.toLowerCase()==q.toLowerCase()){
_19=_18[i];
break;
}
}
}
if(typeof fn=="function"){
fn(_19);
}else{
_c.trigger("result",_19&&[_19.data,_19.value]);
}
}
$.each(trimWords(_c.val()),function(i,_1c){
request(_1c,findValueCallback,findValueCallback);
});
}).bind("flushCache",function(){
_f.flush();
}).bind("setOptions",function(){
$.extend(_a,arguments[1]);
if("data" in arguments[1]){
_f.populate();
}
}).bind("unautocomplete",function(){
_13.unbind();
_c.unbind();
$(_9.form).unbind(".autocomplete");
});
function selectCurrent(){
var _1d=_13.selected();
if(!_1d){
return false;
}
var v=_1d.result;
_e=v;
if(_a.multiple){
var _1f=trimWords(_c.val());
if(_1f.length>1){
v=_1f.slice(0,_1f.length-1).join(_a.multipleSeparator)+_a.multipleSeparator+v;
}
v+=_a.multipleSeparator;
}
_c.val(v);
hideResultsNow();
_c.trigger("result",[_1d.data,_1d.value]);
return true;
}
function onChange(_20,_21){
if(_11==_b.DEL){
_13.hide();
return;
}
var _22=_c.val();
if(!_21&&_22==_e){
return;
}
_e=_22;
_22=lastWord(_22);
if(_22.length>=_a.minChars){
_c.addClass(_a.loadingClass);
if(!_a.matchCase){
_22=_22.toLowerCase();
}
request(_22,receiveData,hideResultsNow);
}else{
stopLoading();
_13.hide();
}
}
function trimWords(_23){
if(!_23){
return [""];
}
var _24=_23.split(_a.multipleSeparator);
var _25=[];
$.each(_24,function(i,_27){
if($.trim(_27)){
_25[i]=$.trim(_27);
}
});
return _25;
}
function lastWord(_28){
if(!_a.multiple){
return _28;
}
var _29=trimWords(_28);
return _29[_29.length-1];
}
function autoFill(q,_2b){
if(_a.autoFill&&(lastWord(_c.val()).toLowerCase()==q.toLowerCase())&&_11!=_b.BACKSPACE){
_c.val(_c.val()+_2b.substring(lastWord(_e).length));
$.Autocompleter.Selection(_9,_e.length,_e.length+_2b.length);
}
}
function hideResults(){
clearTimeout(_d);
_d=setTimeout(hideResultsNow,200);
}
function hideResultsNow(){
var _2c=_13.visible();
_13.hide();
clearTimeout(_d);
stopLoading();
if(_a.mustMatch){
_c.search(function(_2d){
if(!_2d){
if(_a.multiple){
var _2e=trimWords(_c.val()).slice(0,-1);
_c.val(_2e.join(_a.multipleSeparator)+(_2e.length?_a.multipleSeparator:""));
}else{
_c.val("");
}
}
});
}
if(_2c){
$.Autocompleter.Selection(_9,_9.value.length,_9.value.length);
}
}
function receiveData(q,_30){
if(_30&&_30.length&&_10){
stopLoading();
_13.display(_30,q);
autoFill(q,_30[0].value);
_13.show();
}else{
hideResultsNow();
}
}
function request(_31,_32,_33){
if(!_a.matchCase){
_31=_31.toLowerCase();
}
var _34=_f.load(_31);
if(_34&&_34.length){
_32(_31,_34);
}else{
if((typeof _a.url=="string")&&(_a.url.length>0)){
var _35={timestamp:+new Date()};
$.each(_a.extraParams,function(key,_37){
_35[key]=typeof _37=="function"?_37():_37;
});
$.ajax({mode:"abort",port:"autocomplete"+_9.name,dataType:_a.dataType,url:_a.url,data:$.extend({q:lastWord(_31),limit:_a.max},_35),success:function(_38){
var _39=_a.parse&&_a.parse(_38)||parse(_38);
_f.add(_31,_39);
_32(_31,_39);
}});
}else{
_13.emptyList();
_33(_31);
}
}
}
function parse(_3a){
var _3b=[];
var _3c=_3a.split("\n");
for(var i=0;i<_3c.length;i++){
var row=$.trim(_3c[i]);
if(row){
row=row.split("|");
_3b[_3b.length]={data:row,value:row[0],result:_a.formatResult&&_a.formatResult(row,row[0])||row[0]};
}
}
return _3b;
}
function stopLoading(){
_c.removeClass(_a.loadingClass);
}
};
$.Autocompleter.defaults={inputClass:"ac_input",resultsClass:"ac_results",loadingClass:"ac_loading",minChars:1,delay:400,matchCase:false,matchSubset:true,matchContains:false,cacheLength:10,max:100,mustMatch:false,extraParams:{},selectFirst:true,formatItem:function(row){
return row[0];
},formatMatch:null,autoFill:false,width:0,multiple:false,multipleSeparator:", ",highlight:function(_40,_41){
return _40.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)("+_41.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi,"\\$1")+")(?![^<>]*>)(?![^&;]+;)","gi"),"<strong>$1</strong>");
},scroll:true,scrollHeight:180};
$.Autocompleter.Cache=function(_42){
var _43={};
var _44=0;
function matchSubset(s,sub){
if(!_42.matchCase){
s=s.toLowerCase();
}
var i=s.indexOf(sub);
if(i==-1){
return false;
}
return i==0||_42.matchContains;
}
function add(q,_49){
if(_44>_42.cacheLength){
flush();
}
if(!_43[q]){
_44++;
}
_43[q]=_49;
}
function populate(){
if(!_42.data){
return false;
}
var _4a={},_4b=0;
if(!_42.url){
_42.cacheLength=1;
}
_4a[""]=[];
for(var i=0,ol=_42.data.length;i<ol;i++){
var _4e=_42.data[i];
_4e=(typeof _4e=="string")?[_4e]:_4e;
var _4f=_42.formatMatch(_4e,i+1,_42.data.length);
if(_4f===false){
continue;
}
var _50=_4f.charAt(0).toLowerCase();
if(!_4a[_50]){
_4a[_50]=[];
}
var row={value:_4f,data:_4e,result:_42.formatResult&&_42.formatResult(_4e)||_4f};
_4a[_50].push(row);
if(_4b++<_42.max){
_4a[""].push(row);
}
}
$.each(_4a,function(i,_53){
_42.cacheLength++;
add(i,_53);
});
}
setTimeout(populate,25);
function flush(){
_43={};
_44=0;
}
return {flush:flush,add:add,populate:populate,load:function(q){
if(!_42.cacheLength||!_44){
return null;
}
if(!_42.url&&_42.matchContains){
var _55=[];
for(var k in _43){
if(k.length>0){
var c=_43[k];
$.each(c,function(i,x){
if(matchSubset(x.value,q)){
_55.push(x);
}
});
}
}
return _55;
}else{
if(_43[q]){
return _43[q];
}else{
if(_42.matchSubset){
for(var i=q.length-1;i>=_42.minChars;i--){
var c=_43[q.substr(0,i)];
if(c){
var _55=[];
$.each(c,function(i,x){
if(matchSubset(x.value,q)){
_55[_55.length]=x;
}
});
return _55;
}
}
}
}
}
return null;
}};
};
$.Autocompleter.Select=function(_5d,_5e,_5f,_60){
var _61={ACTIVE:"ac_over"};
var _62,_63=-1,_64,_65="",_66=true,_67,_68;
function init(){
if(!_66){
return;
}
_67=$("<div/>").hide().addClass(_5d.resultsClass).css("position","absolute").appendTo(document.body);
_68=$("<ul/>").appendTo(_67).mouseover(function(_69){
if(target(_69).nodeName&&target(_69).nodeName.toUpperCase()=="LI"){
_63=$("li",_68).removeClass(_61.ACTIVE).index(target(_69));
$(target(_69)).addClass(_61.ACTIVE);
}
}).click(function(_6a){
$(target(_6a)).addClass(_61.ACTIVE);
_5f();
_5e.focus();
return false;
}).mousedown(function(){
_60.mouseDownOnSelect=true;
}).mouseup(function(){
_60.mouseDownOnSelect=false;
});
if(_5d.width>0){
_67.css("width",_5d.width);
}
_66=false;
}
function target(_6b){
var _6c=_6b.target;
while(_6c&&_6c.tagName!="LI"){
_6c=_6c.parentNode;
}
if(!_6c){
return [];
}
return _6c;
}
function moveSelect(_6d){
_62.slice(_63,_63+1).removeClass(_61.ACTIVE);
movePosition(_6d);
var _6e=_62.slice(_63,_63+1).addClass(_61.ACTIVE);
if(_5d.scroll){
var _6f=0;
_62.slice(0,_63).each(function(){
_6f+=this.offsetHeight;
});
if((_6f+_6e[0].offsetHeight-_68.scrollTop())>_68[0].clientHeight){
_68.scrollTop(_6f+_6e[0].offsetHeight-_68.innerHeight());
}else{
if(_6f<_68.scrollTop()){
_68.scrollTop(_6f);
}
}
}
}
function movePosition(_70){
_63+=_70;
if(_63<0){
_63=_62.size()-1;
}else{
if(_63>=_62.size()){
_63=0;
}
}
}
function limitNumberOfItems(_71){
return _5d.max&&_5d.max<_71?_5d.max:_71;
}
function fillList(){
_68.empty();
var max=limitNumberOfItems(_64.length);
for(var i=0;i<max;i++){
if(!_64[i]){
continue;
}
var _74=_5d.formatItem(_64[i].data,i+1,max,_64[i].value,_65);
if(_74===false){
continue;
}
var li=$("<li/>").html(_5d.highlight(_74,_65)).addClass(i%2==0?"ac_even":"ac_odd").appendTo(_68)[0];
$.data(li,"ac_data",_64[i]);
}
_62=_68.find("li");
if(_5d.selectFirst){
_62.slice(0,1).addClass(_61.ACTIVE);
_63=0;
}
if($.fn.bgiframe){
_68.bgiframe();
}
}
return {display:function(d,q){
init();
_64=d;
_65=q;
fillList();
},next:function(){
moveSelect(1);
},prev:function(){
moveSelect(-1);
},pageUp:function(){
if(_63!=0&&_63-8<0){
moveSelect(-_63);
}else{
moveSelect(-8);
}
},pageDown:function(){
if(_63!=_62.size()-1&&_63+8>_62.size()){
moveSelect(_62.size()-1-_63);
}else{
moveSelect(8);
}
},hide:function(){
_67&&_67.hide();
_62&&_62.removeClass(_61.ACTIVE);
_63=-1;
},visible:function(){
return _67&&_67.is(":visible");
},current:function(){
return this.visible()&&(_62.filter("."+_61.ACTIVE)[0]||_5d.selectFirst&&_62[0]);
},show:function(){
var _78=$(_5e).offset();
_67.css({width:typeof _5d.width=="string"||_5d.width>0?_5d.width:$(_5e).width(),top:_78.top+_5e.offsetHeight,left:_78.left}).show();
if(_5d.scroll){
_68.css({maxHeight:_5d.scrollHeight,overflow:"auto"});
if($.browser.msie&&typeof document.body.style.maxHeight==="undefined"){
var _79=0;
_62.each(function(){
_79+=this.offsetHeight;
});
var _7a=_79>_5d.scrollHeight;
_68.css("height",_7a?_5d.scrollHeight:_79);
if(!_7a){
_62.width(_68.width()-parseInt(_62.css("padding-left"))-parseInt(_62.css("padding-right")));
}
}
}
},selected:function(){
var _7b=_62&&_62.filter("."+_61.ACTIVE).removeClass(_61.ACTIVE);
return _7b&&_7b.length&&$.data(_7b[0],"ac_data");
},emptyList:function(){
_68&&_68.empty();
},unbind:function(){
_67&&_67.remove();
}};
};
$.Autocompleter.Selection=function(_7c,_7d,end){
if(_7c.createTextRange){
var _7f=_7c.createTextRange();
_7f.collapse(true);
_7f.moveStart("character",_7d);
_7f.moveEnd("character",end);
_7f.select();
}else{
if(_7c.setSelectionRange){
_7c.setSelectionRange(_7d,end);
}else{
if(_7c.selectionStart){
_7c.selectionStart=_7d;
_7c.selectionEnd=end;
}
}
}
_7c.focus();
};
})(jQuery);
