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