(function($){$.fn.extend({autocomplete:function(_,B){var A=typeof _=="string";B=$.extend({},$.Autocompleter.defaults,{url:A?_:null,data:A?null:_,delay:A?$.Autocompleter.defaults.delay:10,max:B&&!B.scroll?10:150},B);B.highlight=B.highlight||function($){return $};B.formatMatch=B.formatMatch||B.formatItem;return this.each(function(){new $.Autocompleter(this,B)})},result:function($){return this.bind("result",$)},search:function($){return this.trigger("search",[$])},flushCache:function(){return this.trigger("flushCache")},setOptions:function($){return this.trigger("setOptions",[$])},unautocomplete:function(){return this.trigger("unautocomplete")}});$.Autocompleter=function(input,options){var KEY={UP:38,DOWN:40,DEL:46,TAB:9,RETURN:13,ESC:27,COMMA:188,PAGEUP:33,PAGEDOWN:34,BACKSPACE:8},$input=$(input).attr("autocomplete","off").addClass(options.inputClass),timeout,previousValue="",cache=$.Autocompleter.Cache(options),hasFocus=0,lastKeyPressCode,config={mouseDownOnSelect:false},select=$.Autocompleter.Select(options,input,selectCurrent,config),blockSubmit;$.browser.opera&&$(input.form).bind("submit.autocomplete",function(){if(blockSubmit){blockSubmit=false;return false}});$input.bind(($.browser.opera?"keypress":"keydown")+".autocomplete",function(_){hasFocus=1;lastKeyPressCode=_.keyCode;switch(_.keyCode){case KEY.UP:_.preventDefault();if(select.visible())select.prev();else onChange(0,true);break;case KEY.DOWN:_.preventDefault();if(select.visible())select.next();else onChange(0,true);break;case KEY.PAGEUP:_.preventDefault();if(select.visible())select.pageUp();else onChange(0,true);break;case KEY.PAGEDOWN:_.preventDefault();if(select.visible())select.pageDown();else onChange(0,true);break;case options.multiple&&$.trim(options.multipleSeparator)==","&&KEY.COMMA:case KEY.TAB:case KEY.RETURN:if(selectCurrent()){_.preventDefault();blockSubmit=true;return false}break;case KEY.ESC:select.hide();break;default:clearTimeout(timeout);timeout=setTimeout(onChange,options.delay);break}}).focus(function(){hasFocus++}).blur(function(){hasFocus=0;if(!config.mouseDownOnSelect)hideResults()}).click(function(){if(hasFocus++>1&&!select.visible())onChange(0,true)}).bind("search",function(){var A=(arguments.length>1)?arguments[1]:null;function _(C,B){var _;if(B&&B.length)for(var $=0;$<B.length;$++)if(B[$].result.toLowerCase()==C.toLowerCase()){_=B[$];break}if(typeof A=="function")A(_);else $input.trigger("result",_&&[_.data,_.value])}$.each(trimWords($input.val()),function($,A){request(A,_,_)})}).bind("flushCache",function(){cache.flush()}).bind("setOptions",function(){$.extend(options,arguments[1]);if("data"in arguments[1])cache.populate()}).bind("unautocomplete",function(){select.unbind();$input.unbind();$(input.form).unbind(".autocomplete")});function selectCurrent(){var A=select.selected();if(!A)return false;var F=A.result;previousValue=F;if(options.multiple){var D=trimWords($input.val());if(D.length>1){var _=options.multipleSeparator.length,C=$(input).selection().start,B,E=0;$.each(D,function($,A){E+=A.length;if(C<=E){B=$;return false}E+=_});D[B]=F;F=D.join(options.multipleSeparator)}F+=options.multipleSeparator}$input.val(F);hideResultsNow();$input.trigger("result",[A.data,A.value]);return true}function onChange(A,$){if(lastKeyPressCode==KEY.DEL){select.hide();return}var _=$input.val();if(!$&&_==previousValue)return;previousValue=_;_=lastWord(_);if(_.length>=options.minChars){$input.addClass(options.loadingClass);if(!options.matchCase)_=_.toLowerCase();request(_,receiveData,hideResultsNow)}else{stopLoading();select.hide()}}function trimWords(_){if(!_)return[""];if(!options.multiple)return[$.trim(_)];return $.map(_.split(options.multipleSeparator),function(A){return $.trim(_).length?$.trim(A):null})}function lastWord(_){if(!options.multiple)return _;var B=trimWords(_);if(B.length==1)return B[0];var A=$(input).selection().start;if(A==_.length)B=trimWords(_);else B=trimWords(_.replace(_.substring(A),""));return B[B.length-1]}function autoFill(A,_){if(options.autoFill&&(lastWord($input.val()).toLowerCase()==A.toLowerCase())&&lastKeyPressCode!=KEY.BACKSPACE){$input.val($input.val()+_.substring(lastWord(previousValue).length));$(input).selection(previousValue.length,previousValue.length+_.length)}}function hideResults(){clearTimeout(timeout);timeout=setTimeout(hideResultsNow,200)}function hideResultsNow(){var $=select.visible();select.hide();clearTimeout(timeout);stopLoading();if(options.mustMatch)$input.search(function($){if(!$)if(options.multiple){var _=trimWords($input.val()).slice(0,-1);$input.val(_.join(options.multipleSeparator)+(_.length?options.multipleSeparator:""))}else{$input.val("");$input.trigger("result",null)}})}function receiveData(_,$){if($&&$.length&&hasFocus){stopLoading();select.display($,_);autoFill(_,$[0].value);select.show()}else hideResultsNow()}function request(A,C,D){if($.trim(A)!=""){if(!options.matchCase)A=A.toLowerCase();var B=cache.load(A);if(B&&B.length)C(A,B);else if((typeof options.url=="string")&&(options.url.length>0)){var _={timestamp:+new Date()};$.each(options.extraParams,function($,A){_[$]=typeof A=="function"?A():A});$.ajax({mode:"abort",port:"autocomplete"+input.name,dataType:options.dataType,url:options.url,data:$.extend({q:lastWord(A),limit:options.max},_),success:function(_){var $=options.parse&&options.parse(_)||parse(_);cache.add(A,$);C(A,$)}})}else{select.emptyList();D(A)}}}function parse(data){var parsed=[];if(data)eval(data);if(typeof buildingInfo!="undefined")for(row in buildingInfo)parsed[parsed.length]={data:buildingInfo[row],value:buildingInfo[row]["name"],result:options.formatResult&&options.formatResult(row,row["name"])||row["name"]};return parsed}function stopLoading(){$input.removeClass(options.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($){return $[0]},formatMatch:null,autoFill:false,width:0,multiple:false,multipleSeparator:", ",highlight:function($,_){return $.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)("+_.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi,"\\$1")+")(?![^<>]*>)(?![^&;]+;)","gi"),"<strong>$1</strong>")},scroll:true,scrollHeight:180};$.Autocompleter.Cache=function(F){var D={},C=0;function E(A,_){if(!F.matchCase)A=A.toLowerCase();var $=A.indexOf(_);if(F.matchContains=="word")$=A.toLowerCase().search("\\b"+_.toLowerCase());if($==-1)return false;return $==0||F.matchContains}function A(_,$){if(C>F.cacheLength)B();if(!D[_])C++;D[_]=$}function _(){if(!F.data)return false;var H={},I=0;if(!F.url)F.cacheLength=1;H[""]=[];for(var B=0,G=F.data.length;B<G;B++){var E=F.data[B];E=(typeof E=="string")?[E]:E;var C=F.formatMatch(E,B+1,F.data.length);if(C===false)continue;var _=C.charAt(0).toLowerCase();if(!H[_])H[_]=[];var D={value:C,data:E,result:F.formatResult&&F.formatResult(E)||C};H[_].push(D);if(I++<F.max)H[""].push(D)}$.each(H,function($,_){F.cacheLength++;A($,_)})}setTimeout(_,25);function B(){D={};C=0}return{flush:B,add:A,populate:_,load:function(H){if(!F.cacheLength||!C)return null;if(!F.url&&F.matchContains){var G=[];for(var A in D)if(A.length>0){var B=D[A];$.each(B,function($,_){if(E(_.value,H))G.push(_)})}return G}else if(D[H])return D[H];else if(F.matchSubset)for(var _=H.length-1;_>=F.minChars;_--){B=D[H.substr(0,_)];if(B){G=[];$.each(B,function($,_){if(E(_.value,H))G[G.length]=_});return G}}return null}}};$.Autocompleter.Select=function(H,B,I,L){var M={ACTIVE:"ac_over"},E,_=-1,P,O="",F=true,J,A;function C(){if(!F)return;J=$("<div/>").hide().addClass(H.resultsClass).css("position","absolute").appendTo(document.body);A=$("<ul/>").appendTo(J).mouseover(function(B){if(G(B).nodeName&&G(B).nodeName.toUpperCase()=="LI"){_=$("li",A).removeClass(M.ACTIVE).index(G(B));$(G(B)).addClass(M.ACTIVE)}}).click(function(_){$(G(_)).addClass(M.ACTIVE);I();B.focus();return false}).mousedown(function(){L.mouseDownOnSelect=true}).mouseup(function(){L.mouseDownOnSelect=false});if(H.width>0)J.css("width",H.width);F=false}function G(_){var $=_.target;while($&&$.tagName!="LI")$=$.parentNode;if(!$)return[];return $}function K(B){E.slice(_,_+1).removeClass(M.ACTIVE);Q(B);var $=E.slice(_,_+1).addClass(M.ACTIVE);if(H.scroll){var C=0;E.slice(0,_).each(function(){C+=this.offsetHeight});if((C+$[0].offsetHeight-A.scrollTop())>A[0].clientHeight)A.scrollTop(C+$[0].offsetHeight-A.innerHeight());else if(C<A.scrollTop())A.scrollTop(C)}}function Q($){_+=$;if(_<0)_=E.size()-1;else if(_>=E.size())_=0}function D($){return H.max&&H.max<$?H.max:$}function N(){A.empty();var G=D(P.length);for(var B=0;B<G;B++){if(!P[B])continue;var C=H.formatItem(P[B].data,B+1,G,P[B].value,O);if(C===false)continue;var F=$("<li/>").html(H.highlight(C,O)).addClass(B%2==0?"ac_even":"ac_odd").appendTo(A)[0];$.data(F,"ac_data",P[B])}E=A.find("li");if(H.selectFirst){E.slice(0,1).addClass(M.ACTIVE);_=0}if($.fn.bgiframe)A.bgiframe()}return{display:function($,_){C();P=$;O=_;N()},next:function(){K(1)},prev:function(){K(-1)},pageUp:function(){if(_!=0&&_-8<0)K(-_);else K(-8)},pageDown:function(){if(_!=E.size()-1&&_+8>E.size())K(E.size()-1-_);else K(8)},hide:function(){J&&J.hide();E&&E.removeClass(M.ACTIVE);_=-1},visible:function(){return J&&J.is(":visible")},current:function(){return this.visible()&&(E.filter("."+M.ACTIVE)[0]||H.selectFirst&&E[0])},show:function(){var D=$(B).offset();J.css({width:typeof H.width=="string"||H.width>0?H.width:$(B).width(),top:D.top+B.offsetHeight,left:D.left}).show();if(H.scroll){A.scrollTop(0);A.css({maxHeight:H.scrollHeight,overflow:"auto"});if($.browser.msie&&typeof document.body.style.maxHeight==="undefined"){var C=0;E.each(function(){C+=this.offsetHeight});var _=C>H.scrollHeight;A.css("height",_?H.scrollHeight:C);if(!_)E.width(A.width()-parseInt(E.css("padding-left"))-parseInt(E.css("padding-right")))}}},selected:function(){var _=E&&E.filter("."+M.ACTIVE).removeClass(M.ACTIVE);return _&&_.length&&$.data(_[0],"ac_data")},emptyList:function(){A&&A.empty()},unbind:function(){J&&J.remove()}}};$.fn.selection=function(A,B){if(A!==undefined)return this.each(function(){if(this.createTextRange){var $=this.createTextRange();if(B===undefined||A==B){$.move("character",A);$.select()}else{$.collapse(true);$.moveStart("character",A);$.moveEnd("character",B);$.select()}}else if(this.setSelectionRange)this.setSelectionRange(A,B);else if(this.selectionStart){this.selectionStart=A;this.selectionEnd=B}});var D=this[0];if(D.createTextRange){var E=document.selection.createRange(),C=D.value,_="<->",$=E.text.length;E.text=_;var F=D.value.indexOf(_);D.value=C;this.selection(F,F+$);return{start:F,end:F+$}}else if(D.selectionStart!==undefined)return{start:D.selectionStart,end:D.selectionEnd}}})(jQuery);function fbwAutocomplete(B,A,C,_){if(B&&A)$(B).autocomplete(A,{minChars:1,width:400,matchContains:"word",autoFill:false,formatItem:function(_,$,A){return _.name+" ["+_.addr+"]"},formatMatch:function(_,$,A){return _.name+" "+_.addr},formatResult:function($){return $.name}}).result(C)}