/*
 * Autocomplete - jQuery plugin 1.0.2
 *
 * Copyright (c) 2007 Dylan Verheul, Dan G. Switzer, Anjesh Tuladhar, Jörn Zaefferer
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * Revision: $Id: jquery.autocomplete.js 5747 2008-06-25 18:30:55Z joern.zaefferer $
 *
 */

// Input 0
(function(e){e.fn.extend({autocomplete:function(a,b){var m=typeof a=="string";b=e.extend({},e.Autocompleter.defaults,{url:m?a:null,data:m?null:a,delay:m?e.Autocompleter.defaults.delay:10,max:b&&!b.scroll?10:150},b);b.highlight=b.highlight||function(j){return j};b.formatMatch=b.formatMatch||b.formatItem;return this.each(function(){new e.Autocompleter(this,b)})},result:function(a){return this.bind("result",a)},search:function(a){return this.trigger("search",[a])},flushCache:function(){return this.trigger("flushCache")},
setOptions:function(a){return this.trigger("setOptions",[a])},unautocomplete:function(){return this.trigger("unautocomplete")}});e.Autocompleter=function(a,b){var m={UP:38,DOWN:40,DEL:46,TAB:9,RETURN:13,ESC:27,COMMA:188,PAGEUP:33,PAGEDOWN:34,BACKSPACE:8},j=e(a).attr("autocomplete","off").addClass(b.inputClass),q,k="",l=e.Autocompleter.Cache(b),h=0,p,r={mouseDownOnSelect:false},f=e.Autocompleter.Select(b,a,t,r),i;e.browser.opera&&e(a.form).bind("submit.autocomplete",function(){if(i)return i=false});
j.bind((e.browser.opera?"keypress":"keydown")+".autocomplete",function(c){p=c.keyCode;switch(c.keyCode){case m.UP:c.preventDefault();f.visible()?f.prev():s(0,true);break;case m.DOWN:c.preventDefault();f.visible()?f.next():s(0,true);break;case m.PAGEUP:c.preventDefault();f.visible()?f.pageUp():s(0,true);break;case m.PAGEDOWN:c.preventDefault();f.visible()?f.pageDown():s(0,true);break;case b.multiple&&e.trim(b.multipleSeparator)==","&&m.COMMA:case m.TAB:case m.RETURN:if(t()){c.preventDefault();i=true;
return false}break;case m.ESC:f.hide();break;default:clearTimeout(q);q=setTimeout(s,b.delay);break}}).focus(function(){h++}).blur(function(){h=0;r.mouseDownOnSelect||A()}).click(function(){h++>1&&!f.visible()&&s(0,true)}).bind("search",function(){var c=arguments.length>1?arguments[1]:null;function g(u,n){var x;if(n&&n.length)for(var w=0;w<n.length;w++)if(n[w].result.toLowerCase()==u.toLowerCase()){x=n[w];break}typeof c=="function"?c(x):j.trigger("result",x&&[x.data,x.value])}e.each(v(j.val()),function(u,
n){z(n,g,g)})}).bind("flushCache",function(){l.flush()}).bind("setOptions",function(c,g){e.extend(b,g);"data"in g&&l.populate()}).bind("unautocomplete",function(){f.unbind();j.unbind();e(a.form).unbind(".autocomplete")});function t(){var c=f.selected();if(!c)return false;var g=c.result;k=g;if(b.multiple){var u=v(j.val());if(u.length>1)g=u.slice(0,u.length-1).join(b.multipleSeparator)+b.multipleSeparator+g;g+=b.multipleSeparator}j.val(g);d();j.trigger("result",[c.data,c.value]);return true}function s(c,
g){if(p==m.DEL)f.hide();else{c=j.val();if(!(!g&&c==k)){k=c;c=y(c);if(c.length>=b.minChars){j.addClass(b.loadingClass);b.matchCase||(c=c.toLowerCase());z(c,o,d)}else{B();f.hide()}}}}function v(c){if(!c)return[""];c=c.split(b.multipleSeparator);var g=[];e.each(c,function(u,n){if(e.trim(n))g[u]=e.trim(n)});return g}function y(c){if(!b.multiple)return c;c=v(c);return c[c.length-1]}function C(c,g){if(b.autoFill&&y(j.val()).toLowerCase()==c.toLowerCase()&&p!=m.BACKSPACE){j.val(j.val()+g.substring(y(k).length));
e.Autocompleter.Selection(a,k.length,k.length+g.length)}}function A(){clearTimeout(q);q=setTimeout(d,200)}function d(){var c=f.visible();f.hide();clearTimeout(q);B();b.mustMatch&&j.search(function(g){if(!g)if(b.multiple){g=v(j.val()).slice(0,-1);j.val(g.join(b.multipleSeparator)+(g.length?b.multipleSeparator:""))}else j.val("")});c&&e.Autocompleter.Selection(a,a.value.length,a.value.length)}function o(c,g){if(g&&g.length&&h){B();f.display(g,c);C(c,g[0].value);f.show()}else d()}function z(c,g,u){b.matchCase||
(c=c.toLowerCase());var n=l.load(c);if(n&&n.length)g(c,n);else if(typeof b.url=="string"&&b.url.length>0){var x={timestamp:+new Date};e.each(b.extraParams,function(w,D){x[w]=typeof D=="function"?D():D});e.ajax({mode:"abort",port:"autocomplete"+a.name,dataType:b.dataType,url:b.url,data:e.extend({q:y(c),limit:b.max},x),success:function(w){w=b.parse&&b.parse(w)||E(w);l.add(c,w);g(c,w)}})}else{f.emptyList();u(c)}}function E(c){var g=[];c=c.split("\n");for(var u=0;u<c.length;u++){var n=e.trim(c[u]);if(n){n=
n.split("|");g[g.length]={data:n,value:n[0],result:b.formatResult&&b.formatResult(n,n[0])||n[0]}}}return g}function B(){j.removeClass(b.loadingClass)}};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(a){return a[0]},formatMatch:null,autoFill:false,width:0,multiple:false,multipleSeparator:", ",
highlight:function(a,b){return a.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)("+b.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi,"\\$1")+")(?![^<>]*>)(?![^&;]+;)","gi"),"<strong>$1</strong>")},scroll:true,scrollHeight:180};e.Autocompleter.Cache=function(a){var b={},m=0;function j(h,p){a.matchCase||(h=h.toLowerCase());h=h.indexOf(p);if(h==-1)return false;return h==0||a.matchContains}function q(h,p){m>a.cacheLength&&l();b[h]||m++;b[h]=p}function k(){if(!a.data)return false;var h={},p=0;if(!a.url)a.cacheLength=
1;h[""]=[];for(var r=0,f=a.data.length;r<f;r++){var i=a.data[r];i=typeof i=="string"?[i]:i;var t=a.formatMatch(i,r+1,a.data.length);if(t!==false){var s=t.charAt(0).toLowerCase();h[s]||(h[s]=[]);i={value:t,data:i,result:a.formatResult&&a.formatResult(i)||t};h[s].push(i);p++<a.max&&h[""].push(i)}}e.each(h,function(v,y){a.cacheLength++;q(v,y)})}setTimeout(k,25);function l(){b={};m=0}return{flush:l,add:q,populate:k,load:function(h){if(!a.cacheLength||!m)return null;if(!a.url&&a.matchContains){var p=[];
for(var r in b)if(r.length>0){var f=b[r];e.each(f,function(i,t){j(t.value,h)&&p.push(t)})}return p}else if(b[h])return b[h];else if(a.matchSubset)for(r=h.length-1;r>=a.minChars;r--)if(f=b[h.substr(0,r)]){p=[];e.each(f,function(i,t){if(j(t.value,h))p[p.length]=t});return p}return null}}};e.Autocompleter.Select=function(a,b,m,j){var q={ACTIVE:"ac_over"},k,l=-1,h,p="",r=true,f,i;function t(){if(r){f=e("<div/>").hide().addClass(a.resultsClass).css("position","absolute").appendTo(document.body);i=e("<ul/>").appendTo(f).mouseover(function(d){if(s(d).nodeName&&
s(d).nodeName.toUpperCase()=="LI"){l=e("li",i).removeClass(q.ACTIVE).index(s(d));e(s(d)).addClass(q.ACTIVE)}}).click(function(d){e(s(d)).addClass(q.ACTIVE);m();b.focus();return false}).mousedown(function(){j.mouseDownOnSelect=true}).mouseup(function(){j.mouseDownOnSelect=false});a.width>0&&f.css("width",a.width);r=false}}function s(d){for(d=d.target;d&&d.tagName!="LI";)d=d.parentNode;if(!d)return[];return d}function v(d){k.slice(l,l+1).removeClass(q.ACTIVE);y(d);d=k.slice(l,l+1).addClass(q.ACTIVE);
if(a.scroll){var o=0;k.slice(0,l).each(function(){o+=this.offsetHeight});if(o+d[0].offsetHeight-i.scrollTop()>i[0].clientHeight)i.scrollTop(o+d[0].offsetHeight-i.innerHeight());else o<i.scrollTop()&&i.scrollTop(o)}}function y(d){l+=d;if(l<0)l=k.size()-1;else if(l>=k.size())l=0}function C(d){return a.max&&a.max<d?a.max:d}function A(){i.empty();for(var d=C(h.length),o=0;o<d;o++)if(h[o]){var z=a.formatItem(h[o].data,o+1,d,h[o].value,p);if(z!==false){z=e("<li/>").html(a.highlight(z,p)).addClass(o%2==
0?"ac_even":"ac_odd").appendTo(i)[0];e.data(z,"ac_data",h[o])}}k=i.find("li");if(a.selectFirst){k.slice(0,1).addClass(q.ACTIVE);l=0}e.fn.bgiframe&&i.bgiframe()}return{display:function(d,o){t();h=d;p=o;A()},next:function(){v(1)},prev:function(){v(-1)},pageUp:function(){l!=0&&l-8<0?v(-l):v(-8)},pageDown:function(){l!=k.size()-1&&l+8>k.size()?v(k.size()-1-l):v(8)},hide:function(){f&&f.hide();k&&k.removeClass(q.ACTIVE);l=-1},visible:function(){return f&&f.is(":visible")},current:function(){return this.visible()&&
(k.filter("."+q.ACTIVE)[0]||a.selectFirst&&k[0])},show:function(){var d=e(b).offset();f.css({width:typeof a.width=="string"||a.width>0?a.width:e(b).width(),top:d.top+b.offsetHeight,left:d.left}).show();if(a.scroll){i.scrollTop(0);i.css({maxHeight:a.scrollHeight,overflow:"auto"});if(e.browser.msie&&typeof document.body.style.maxHeight==="undefined"){var o=0;k.each(function(){o+=this.offsetHeight});d=o>a.scrollHeight;i.css("height",d?a.scrollHeight:o);d||k.width(i.width()-parseInt(k.css("padding-left"))-
parseInt(k.css("padding-right")))}}},selected:function(){var d=k&&k.filter("."+q.ACTIVE).removeClass(q.ACTIVE);return d&&d.length&&e.data(d[0],"ac_data")},emptyList:function(){i&&i.empty()},unbind:function(){f&&f.remove()}}};e.Autocompleter.Selection=function(a,b,m){if(a.createTextRange){var j=a.createTextRange();j.collapse(true);j.moveStart("character",b);j.moveEnd("character",m);j.select()}else if(a.setSelectionRange)a.setSelectionRange(b,m);else if(a.selectionStart){a.selectionStart=b;a.selectionEnd=
m}a.focus()}})(jQuery);
