/*
 * Auto Complete 5.0
 * November 22, 2009
 * Corey Hart @ http://www.codenothing.com
 */
(function(f,j){f.fn.autoComplete=function(){var k=R.call(arguments),h=this,i=k.shift(),r=typeof i==="string";if(r)i=i.replace(".","-");k=i==="button-supply"||i==="direct-supply"?f.isArray(k[0])&&f.isArray(k[0][0])?k[0]:k:k[1]===j&&f.isArray(k[0])?k[0]:k;return r?f(h)[i==="option"&&k.length<2?"triggerHandler":"trigger"]("autoComplete."+i,k):i&&i[f.expando]?f(h).trigger(i,k):W.call(h,i)};f.fn.bgiframe=f.fn.bgiframe?f.fn.bgiframe:f.fn.bgIframe?f.fn.bgIframe:function(){return this};f.expando=f.expando!==
j?f.expando:function(){var k=f.Event("keyup"),h;for(h in k)if(h.indexOf("jQuery")===0)return h;return"jQuery"+k.timeStamp}();function X(){return(new Date).getTime()}var g=true,o=false,R=Array.prototype.slice,s=f.autoComplete={counter:0,length:0,stack:{},order:[],hasFocus:o,getFocus:function(){return this.order[0]?this.stack[this.order[0]]:j},getPrevious:function(){for(var k=1,h=this.order.length;k<h;k++)if(this.order[k])return this.stack[this.order[k]]},remove:function(k){for(var h=0,i=this.order.length;h<
i;h++)if(this.order[h]===k)this.order[h]=j;this.stack[k]=j;this.length--;delete this.stack[k]},getAll:function(){for(var k=0,h=this.counter,i=[];k<h;k++)this.stack[k]&&i.push(this.stack[k]);return f(i)},defaults:{backwardsCompatible:o,ajax:"ajax.php",ajaxCache:f.ajaxSettings.cache,dataSupply:[],dataFn:j,dataName:"ac-data",list:"auto-complete-list",rollover:"auto-complete-list-rollover",width:j,striped:j,maxHeight:j,newList:o,postVar:"value",postData:{},minChars:1,maxItems:-1,maxRequests:0,requestType:"POST",
inputControl:j,autoFill:o,nonInput:j,multiple:o,multipleSeparator:" ",onBlur:j,onFocus:j,onHide:j,onLoad:j,onMaxRequest:function(){},onRollover:j,onSelect:j,onShow:j,onSubmit:function(){return g},spinner:j,preventEnterSubmit:g,delay:0,useCache:g,cacheLimit:50}};function W(k){return this.each(function(){var h=this,i=f(h).attr("autocomplete","off"),r=g,n={},v="",w={length:0},l,G,M,I,P,A=o,H,N,q=-1,p,B,t=function(){s.length++;return++s.counter}(),Q=0,u={length:0,val:j,list:{}},d=f.extend({width:i.outerWidth()},
s.defaults,k||{},f.metadata?i.metadata():{}),e=!d.newList&&f("ul."+d.list)[0]?f("ul."+d.list).eq(0).bgiframe().data("autoComplete",g):f("<ul/>").appendTo("body").addClass(d.list).bgiframe().hide().data("ac-selfmade",g).data("autoComplete",g),Y=f(document).bind("click.autoComplete-"+t,function(b){var a;if(r&&A&&(!n||b.timeStamp-n.timeStamp>200)&&(a=f(b.target)).closest("ul").data("ac-input-index")!==t&&a.data("ac-input-index")!==t){e.hide(b);i.blur()}n=b});S();d.requestType=d.requestType.toUpperCase();
B=d.multiple?d.multipleSeparator:j;s.stack[t]=h;i.data("autoComplete",g).data("ac-input-index",t).data("ac-active",r).data("ac-initial-settings",f.extend(g,{},d)).data("ac-settings",d).bind(window.opera?"keypress.autoComplete":"keydown.autoComplete",function(b){if(!r)return g;var a=(n=b).keyCode,c=o;if(a==9&&A)T(b);else if(a==13&&l){c=d.preventEnterSubmit&&A?o:g;T(b)}else if(a==38)if(q>0){q--;U(b)}else{q=-1;i.val(v);e.hide(b)}else if(a==40){if(q<w.length-1){q++;V(b)}}else if(a==33){if(q>0){q-=P;if(q<
0)q=0;U(b)}}else if(a==34){if(q<w.length-1){q+=P;if(q>w.length-1)q=w.length-1;V(b)}}else if(d.nonInput&&f.inArray(a,d.nonInput))e.html("").hide(b);else return g;n[f.expando+"_autoComplete_keydown"]=g;return c}).bind("keyup.autoComplete",function(b){if(!r||n[f.expando+"_autoComplete_keydown"])return g;v=i.val();var a=(n=b).keyCode,c=B?v.split(B).pop():v;if(a!=13){u.val=d.inputControl===j?c:d.inputControl.apply(h,d.backwardsCompatible?[c,a,e,b]:[b,{val:c,key:a,ul:e}]);if(u.val.length>=d.minChars)J(b,
d,u,a==8||a==32);else a==8&&e.html("").hide(b)}}).bind("blur.autoComplete",function(b){if(!r||A)return g;n=b;i.data("ac-hasFocus",o);q=-1;s.order[0]!==j&&s.order.unshift(j);s.hasFocus=o;e.hide(b);if(d.onBlur)d.onBlur.apply(h,d.backwardsCompatible?[v,e,b]:[b,{val:v,ul:e}])}).bind("focus.autoComplete",function(b,a){if(!r||s.focus===t&&a===f.expando+"_autoComplete"||n[f.expando+"_autoComplete_enter"])return g;n=b;t!=e.data("ac-input-index")&&e.html("").hide(b);i.data("ac-hasFocus",g);if(s.order[0]===
j)if(s.order[1]===t)s.order.shift();else s.order[0]=t;else s.order[0]!=t&&s.order[1]!=t&&s.order.unshift(t);s.order.length>s.defaults.cacheLimit&&s.order.pop();s.hasFocus=g;if(d.onFocus)d.onFocus.apply(h,d.backwardsCompatible?[e,b]:[b,{ul:e}])}).bind("autoComplete.settings",function(b,a){if(!r)return g;if(f.isFunction(a)){a=a.apply(h,d.backwardsCompatible?[d,u,e,b]:[b,{settings:d,cache:u,ul:e}]);if(f.isArray(a)&&a[0]!==j){d=f.extend(g,{},d,a[0]||d);u=f.extend(g,{},u,a[1]||u)}}else d=f.extend(g,{},
d,a||{});d.requestType=d.requestType.toUpperCase();B=d.multiple?d.multipleSeparator:j;i.data("ac-settings",d);e=!d.newList&&e.hasClass(d.list)?e:!d.newList&&f("ul."+d.list)[0]?f("ul."+d.list).bgiframe().data("autoComplete",g):f("<ul/>").appendTo("body").addClass(d.list).bgiframe().hide().data("ac-selfmade",g).data("autoComplete",g);S();return n=b}).bind("autoComplete.flush",function(b,a){if(!r)return g;u={length:0,val:j,list:{}};a||(Q=0);return n=b}).bind("autoComplete.button-ajax",function(b,a,c){if(!r)return g;
n=b;i.trigger("focus",[f.expando+"_autoComplete"]);if(typeof a==="string"){c=a;a={}}u.val=c||"NON_404_<>!@$^&";return J(b,f.extend(g,{},d,{maxItems:-1,postData:a||{}}),u)}).bind("autoComplete.button-supply",function(b,a,c){if(!r)return g;n=b;i.trigger("focus",[f.expando+"_autoComplete"]);if(typeof a==="string"){c=a;a=j}u.val=c||"NON_404_SUPPLY_<>!@$^&";a=f.isArray(a)&&a.length?a:d.dataSupply;return J(b,f.extend(g,{},d,{maxItems:-1,dataSupply:a,dataFn:function(){return g}}),u)}).bind("autoComplete.direct-supply",
function(b,a,c){if(!r)return g;n=b;i.trigger("focus",[f.expando+"_autoComplete"]);if(typeof a==="string"){c=a;a=j}u.val=c||"NON_404_SUPPLY_<>!@$^&";a=f.isArray(a)&&a.length?a:d.dataSupply;return O(b,a,f.extend(g,{},d,{maxItems:-1,dataSupply:a,dataFn:function(){return g}}),u)}).bind("autoComplete.search",function(b,a){if(!r)return g;u.val=a||"";return J(n=b,d,u)}).bind("autoComplete.option",function(b){if(!r)return g;n=b;var a=R.call(arguments),c=a.length;return c==3?function(){d[a[1]]=a[2];i.data("ac-settings",
d);return a[2]}():c==2?function(){switch(a[1]){case "ul":return e;case "cache":return u;case "xhr":return N;case "input":return i;default:return d[a[1]]||j}}():d}).bind("autoComplete.enable",function(b){i.data("ac-active",r=g);return n=b}).bind("autoComplete.disable",function(b){i.data("ac-active",r=o);e.html("").hide(b);return n=b}).bind("autoComplete.destroy",function(b){i.removeData("autoComplete").removeData("ac-input-index").removeData("ac-initial-settings").removeData("ac-settings").removeData("ac-active").unbind(".autoComplete").unbind("autoComplete."+
["settings","flush","button-ajax","button-supply","direct-supply","search","option","enable","disable","destroy"].join(" autoComplete.")).parents("form").eq(0).unbind("submit.autoComplete-"+t);Y.unbind("click.autoComplete-"+t);s.remove(t);r=o;var a=e.html("").hide(b).data("ac-inputs"),c;a[t]=j;for(c in a)if(a[c]===g)return n=b;e.data("ac-selfmade")===g&&e.remove();return n=b}).parents("form").eq(0).bind("submit.autoComplete-"+t,function(b){if(!r)return g;var a=n[f.expando+"_autoComplete_enter"]||
o;n=b;return d.preventEnterSubmit?A||a?o:d.onSubmit.call(h,b,{form:this,ul:e}):d.onSubmit.call(h,b,{form:this,ul:e})});function J(b,a,c,m,y){a.spinner&&a.spinner.call(h,b,{active:g,ul:e});if(H)H=clearTimeout(H);if(a.delay>0&&y===j)return H=setTimeout(function(){J(b,a,c,m,g);H=clearTimeout(H)},a.delay);N&&N.abort();if(a.useCache&&c.list[c.val])return O(b,c.list[c.val],a,c,m);if(a.dataSupply.length)return Z(b,a,c,m);if(a.maxRequests&&++Q>=a.maxRequests){e.html("").hide(b);a.spinner&&a.spinner.call(h,
b,{active:o,ul:e});return Q>a.maxRequests?o:a.onMaxRequest.apply(h,a.backwardsCompatible?[c.val,e,b,v]:[b,{search:c.val,val:v,ul:e}])}a.postData[a.postVar]=c.val;return N=f.ajax({type:a.requestType,url:a.ajax,data:a.postData,dataType:"json",cache:a.ajaxCache,success:function(z){O(b,z,a,c,m)},error:function(){e.html("").hide(b);a.spinner&&a.spinner.call(h,b,{active:o,ul:e})}})}function Z(b,a,c,m){for(var y=[],z=[],K=f.isFunction(a.dataFn),E=K?j:new RegExp("^"+c.val,"i"),F=0,x,C=0,D=a.dataSupply.length;C<
D;C++){x=a.dataSupply[C];x=typeof x==="object"&&x.value?x:{value:x};z=a.backwardsCompatible?[c.val,x.value,y,C,a.dataSupply,e,b]:[b,{val:c.val,entry:x.value,list:y,i:C,supply:a.dataSupply,ul:e}];if(K&&a.dataFn.apply(h,z)||!K&&x.value.match(E)){if(a.maxItems>-1&&++F>a.maxItems)break;y.push(x)}}return O(b,y,a,c,m)}function T(b){if(A){if(d.onSelect)d.onSelect.apply(h,d.backwardsCompatible?[p,l,e,b]:[b,{data:p,li:l,ul:e}]);L(j);v=i.val();if(n.type=="keydown")n[f.expando+"_autoComplete_enter"]=g}e.hide(b);
return l}function U(b){l&&l.removeClass(d.rollover);e.show(b);l=w.eq(q).addClass(d.rollover);p=l.data(d.dataName);if(!l.length||!p)return o;L(p.value||"");if(d.onRollover)d.onRollover.apply(h,d.backwardsCompatible?[p,l,e,b]:[b,{data:p,li:l,ul:e}]);b=q*I;if(b<G-M){G=b+M;e.scrollTop(b)}return l}function V(b){l&&l.removeClass(d.rollover);e.show(b);l=w.eq(q).addClass(d.rollover);p=l.data(d.dataName);if(!l.length||!p)return o;L(p.value||"");var a=(q+1)*I;if(a>G)e.scrollTop((G=a)-M);if(d.onRollover)d.onRollover.apply(h,
d.backwardsCompatible?[p,l,e,b]:[b,{data:p,li:l,ul:e}]);return l}function S(){if(!e[f.expando+"_autoComplete"]){var b=e.hide,a=e.show;e.hide=function(m,y,z){if(d.onHide&&A){d.onHide.call(h,m,{ul:e});n[f.expando+"_autoComplete_hide"]=g}A=o;return b.call(e,y,z)};e.show=function(m,y,z){d.onShow&&!A&&d.onShow.call(h,m,{ul:e});A=g;return a.call(e,y,z)};e[f.expando+"_autoComplete"]=g}var c=e.data("ac-inputs")||{};c[t]=g;return e.data("ac-inputs",c)}function L(b){if(b===j){var a,c;a=c=i.val().length}else{if(B)b=
v.substr(0,v.length-v.split(B).pop().length)+b+B;a=v.length;c=b.length;i.val(b)}if(!d.autoFill||a>c)return o;else if(h.createTextRange){var m=h.createTextRange();if(b===j)m.move("character",a);else{m.collapse(g);m.moveStart("character",a);m.moveEnd("character",c)}m.select()}else if(h.setSelectionRange)h.setSelectionRange(a,c);else if(h.selectionStart){h.selectionStart=a;h.selectionEnd=c}return g}function O(b,a,c,m,y){if(c.onLoad)a=c.onLoad.call(h,b,{list:a,settings:c,cache:m,ul:e});c.spinner&&c.spinner.call(h,
b,{active:o,ul:e});if(c.useCache&&m.list[m.val]===j){m.length++;m.list[m.val]=a;if(m.length>c.cacheLimit){m.list={};m.length=0}}if(!a||a.length<1)return e.html("").hide(b);q=-1;m=i.offset();for(var z=[],K=0,E=0,F=0,x=o,C=a.length;F<C;F++)if(a[F].value){if(c.maxItems>-1&&++K>c.maxItems)break;z.push(c.striped&&x?'<li class="'+c.striped+'">':"<li>",a[F].display||a[F].value,"</li>");x=!x}w=e.html(z.join("")).children("li");for(C=w.length;E<C;E++){f.data(w[E],c.dataName,a[E]);f.data(w[E],"ac-index",E)}if(c.autoFill&&
!y){q=0;p=a[0];L(p.value||"");l=w.eq(0).addClass(c.rollover)}e.unbind(".autoComplete").data("ac-input-index",t).bind("mouseout.autoComplete",function(){l.removeClass(c.rollover)}).bind("mouseover.autoComplete",function(D){l=f(D.target).closest("li");if(l.length<1)return o;w.filter("."+c.rollover).removeClass(c.rollover);q=l.addClass(c.rollover).data("ac-index");p=l.data(c.dataName);if(c.onRollover)c.onRollover.apply(h,c.backwardsCompatible?[p,l,e,D]:[D,{data:p,li:l,ul:e}])}).bind("click.autoComplete",
function(D){i.trigger("focus",[f.expando+"_autoComplete"]);p=l.data(c.dataName);i.val(v=B?v.substr(0,v.length-v.split(B).pop().length)+p.value+B:p.value);e.hide(D);L(j);if(c.onSelect)c.onSelect.apply(h,c.backwardsCompatible?[p,l,e,D]:[D,{data:p,li:l,ul:e}])}).css({top:m.top+i.outerHeight(),left:m.left,width:c.width}).scrollTop(0);if(c.maxHeight)e.css({height:I*w.length>c.maxHeight?c.maxHeight:"auto",overflow:"auto"});G=M=e.show(b).outerHeight();I=w.eq(0).outerHeight();P=Math.floor(G/I);n.timeStamp=
X();return e}})}})(jQuery);
