(function(e){e.fn.extend({autocomplete:function(t,n){var r=typeof t=="string";n=e.extend({},e.Autocompleter.defaults,{url:r?t:null,data:r?null:t,delay:r?e.Autocompleter.defaults.delay:10,max:n&&!n.scroll?10:150},n);n.highlight=n.highlight||function(e){return e};n.formatMatch=n.formatMatch||n.formatItem;return this.each(function(){new e.Autocompleter(this,n)})},result:function(e){return this.bind("result",e)},search:function(e){return this.trigger("search",[e])},flushCache:function(){return this.trigger("flushCache")},setOptions:function(e){return this.trigger("setOptions",[e])},unautocomplete:function(){return this.trigger("unautocomplete")}});e.Autocompleter=function(t,n){function p(){var r=c.selected();if(!r)return false;var s=r.result;o=s;if(n.multiple){var u=v(i.val());if(u.length>1){var a=n.multipleSeparator.length;var f=e(t).selection().start;var l,h=0;e.each(u,function(e,t){h+=t.length;if(f<=h){l=e;return false}h+=a});u[l]=s;s=u.join(n.multipleSeparator)}s+=n.multipleSeparator}i.val(s);b();i.trigger("result",[r.data,r.value]);return true}function d(e,t){if(f==r.DEL){c.hide();return}var s=i.val();if(!t&&s==o)return;o=s;s=m(s);if(s.length>=n.minChars){i.addClass(n.loadingClass);if(!n.matchCase)s=s.toLowerCase();E(s,w,b)}else{x();c.hide()}}function v(t){if(!t)return[""];if(!n.multiple)return[e.trim(t)];return e.map(t.split(n.multipleSeparator),function(n){return e.trim(t).length?e.trim(n):null})}function m(r){if(!n.multiple)return r;var i=v(r);if(i.length==1)return i[0];var s=e(t).selection().start;if(s==r.length)i=v(r);else i=v(r.replace(r.substring(s),""));return i[i.length-1]}function g(s,u){if(n.autoFill&&m(i.val()).toLowerCase()==s.toLowerCase()&&f!=r.BACKSPACE){i.val(i.val()+u.substring(m(o).length));e(t).selection(o.length,o.length+u.length)}}function y(){clearTimeout(s);s=setTimeout(b,200)}function b(){var e=c.visible();c.hide();clearTimeout(s);x();if(n.mustMatch)i.search(function(e){if(!e)if(n.multiple){var t=v(i.val()).slice(0,-1);i.val(t.join(n.multipleSeparator)+(t.length?n.multipleSeparator:""))}else{i.val("");i.trigger("result",null)}})}function w(e,t){if(t&&t.length&&a){x();c.display(t,e);g(e,t[0].value);c.show()}else b()}function E(r,i,s){if(!n.matchCase)r=r.toLowerCase();var o=u.load(r);if(o&&o.length)i(r,o);else if(typeof n.url=="string"&&n.url.length>0){var a={timestamp:+(new Date)};e.each(n.extraParams,function(e,t){a[e]=typeof t=="function"?t():t});e.ajax({mode:"abort",port:"autocomplete"+t.name,dataType:n.dataType,url:n.url,data:e.extend({q:m(r),limit:n.max},a),success:function(e){var t=n.parse(e);u.add(r,t);i(r,t)}})}else{c.emptyList();s(r)}}function S(t){var r=[];var i=t.split("\n");for(var s=0;s1&&!c.visible())d(0,true)}).bind("search",function(){function n(e,n){var r;if(n&&n.length)for(var s=0;s1?arguments[1]:null;e.each(v(i.val()),function(e,t){E(t,n,n)})}).bind("flushCache",function(){u.flush()}).bind("setOptions",function(){e.extend(n,arguments[1]);if("data"in arguments[1])u.populate()}).bind("unautocomplete",function(){c.unbind();i.unbind();e(t.form).unbind(".autocomplete")})};e.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(e){return e[0]},formatMatch:null,autoFill:false,width:0,multiple:false,multipleSeparator:", ",highlight:function(e,t){return e.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)("+t.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi,"\\$1")+")(?![^<>]*>)(?![^&;]+;)","gi"),"$1")},scroll:true,scrollHeight:180};e.Autocompleter.Cache=function(t){function i(e,n){if(!t.matchCase)e=e.toLowerCase();var r=e.indexOf(n);if(t.matchContains=="word")r=e.toLowerCase().search("\\b"+n.toLowerCase());if(r==-1)return false;return r==0||t.matchContains}function s(e,i){if(r>t.cacheLength)u();if(!n[e])r++;n[e]=i}function o(){if(!t.data)return false;var n={},r=0;if(!t.url)t.cacheLength=1;n[""]=[];for(var i=0,o=t.data.length;i0){var a=n[u];e.each(a,function(e,t){if(i(t.value,s))o.push(t)})}return o}else if(n[s])return n[s];else if(t.matchSubset)for(var f=s.length-1;f>=t.minChars;f--){var a=n[s.substr(0,f)];if(a){var o=[];e.each(a,function(e,t){if(i(t.value,s))o[o.length]=t});return o}}return null}}};e.Autocompleter.Select=function(t,n,r,i){function p(){if(!l)return;c=e("
").hide().addClass(t.resultsClass).css("position","absolute").appendTo(document.body);h=e("
    ").appendTo(c).mouseover(function(t){if(d(t).nodeName&&d(t).nodeName.toUpperCase()=="LI"){u=e("li",h).removeClass(s.ACTIVE).index(d(t));e(d(t)).addClass(s.ACTIVE)}}).click(function(t){e(d(t)).addClass(s.ACTIVE);r();n.focus();return false}).mousedown(function(){i.mouseDownOnSelect=true}).mouseup(function(){i.mouseDownOnSelect=false});if(t.width>0)c.css("width",t.width);l=false}function d(e){var t=e.target;while(t&&t.tagName!="LI")t=t.parentNode;if(!t)return[];return t}function v(e){o.slice(u,u+1).removeClass(s.ACTIVE);m(e);var n=o.slice(u,u+1).addClass(s.ACTIVE);if(t.scroll){var r=0;o.slice(0,u).each(function(){r+=this.offsetHeight});if(r+n[0].offsetHeight-h.scrollTop()>h[0].clientHeight)h.scrollTop(r+n[0].offsetHeight-h.innerHeight());else if(r=o.size())u=0}function g(e){return t.max&&t.max").html(t.highlight(i,f)).addClass(r%2==0?"ac_even":"ac_odd").appendTo(h)[0];e.data(l,"ac_data",a[r])}o=h.find("li");if(t.selectFirst){o.slice(0,1).addClass(s.ACTIVE);u=0}if(e.fn.bgiframe)h.bgiframe()}var s={ACTIVE:"ac_over"};var o,u=-1,a,f="",l=true,c,h;return{display:function(e,t){p();a=e;f=t;y()},next:function(){v(1)},prev:function(){v(-1)},pageUp:function(){if(u!=0&&u-8<0)v(-u);else v(-8)},pageDown:function(){if(u!=o.size()-1&&u+8>o.size())v(o.size()-1-u);else v(8)},hide:function(){c&&c.hide();o&&o.removeClass(s.ACTIVE);u=-1},visible:function(){return c&&c.is(":visible")},current:function(){return this.visible()&&(o.filter("."+s.ACTIVE)[0]||t.selectFirst&&o[0])},show:function(){var r=e(n).offset();c.css({width:typeof t.width=="string"||t.width>0?t.width:e(n).width(),top:r.top+n.offsetHeight,left:r.left}).show();if(t.scroll){h.scrollTop(0);h.css({maxHeight:t.scrollHeight,overflow:"auto"});if(e.browser.msie&&typeof document.body.style.maxHeight==="undefined"){var i=0;o.each(function(){i+=this.offsetHeight});var s=i>t.scrollHeight;h.css("height",s?t.scrollHeight:i);if(!s)o.width(h.width()-parseInt(o.css("padding-left"))-parseInt(o.css("padding-right")))}}},selected:function(){var t=o&&o.filter("."+s.ACTIVE).removeClass(s.ACTIVE);return t&&t.length&&e.data(t[0],"ac_data")},emptyList:function(){h&&h.empty()},unbind:function(){c&&c.remove()}}};e.fn.selection=function(e,t){if(e!==undefined)return this.each(function(){if(this.createTextRange){var n=this.createTextRange();if(t===undefined||e==t){n.move("character",e);n.select()}else{n.collapse(true);n.moveStart("character",e);n.moveEnd("character",t);n.select()}}else if(this.setSelectionRange)this.setSelectionRange(e,t);else if(this.selectionStart){this.selectionStart=e;this.selectionEnd=t}});var n=this[0];if(n.createTextRange){var r=document.selection.createRange(),i=n.value,s="<->",o=r.text.length;r.text=s;var u=n.value.indexOf(s);n.value=i;this.selection(u,u+o);return{start:u,end:u+o}}else if(n.selectionStart!==undefined)return{start:n.selectionStart,end:n.selectionEnd}}})(jQuery)