jQuery.autocomplete=function(d,a){function p(){k={data:{},length:0}}function D(a){var c=$("li",q);c&&(l+=a,0>l?l=0:l>=c.size()&&(l=c.size()-1),c.removeClass("ac_over"),$(c[l]).addClass("ac_over"))}function I(){var b=$("li.ac_over",q)[0];if(!b){var c=$("li",q);a.selectOnly?1==c.length&&(b=c[0]):a.selectFirst&&(b=c[0])}return b?(z(b),!0):!1}function z(b){b||(b=document.createElement("li"),b.extra=[],b.selectValue="");null!=a.copySelectedContentTo&&$(".autocompleteSelected").length&&($(".autocompleteSelected").show(),
$(".autocompleteSelectedNoText").length&&$(".autocompleteSelectedNoText").show(),$(".autocompleteSelected").html(b.innerHTML));b=$.trim(b.selectValue?b.selectValue:b.innerHTML);v=d.lastSelected=b;m.html("");s();b=b.split("|@|");var c=[];if(0<b.length)for(var e=0;e<b.length;e++){var f=b[e].split("|=|");f[0]=unescape(f[0]);f[1]=unescape(f[1]);c.push(f)}a.onItemSelect&&setTimeout(function(){a.onItemSelect(c)},1)}function s(){n&&clearTimeout(n);h.removeClass(a.loadingClass);m.is(":visible")&&m.hide();
a.mustMatch&&h.val()!=d.lastSelected&&z(null)}function E(b,c){var e,f;if(c){h.removeClass(a.loadingClass);q.innerHTML="";if(!A||0==c.length)return s();q.appendChild(J(c));if(a.autoFill&&h.val().toLowerCase()==b.toLowerCase()&&(f=c[0][0],8!=r)){h.val(h.val()+f.substring(v.length));e=v.length;f=f.length;var g=h.get(0);if(g.createTextRange){var w=g.createTextRange();w.collapse(!0);w.moveStart("character",e);w.moveEnd("character",f);w.select()}else g.setSelectionRange?g.setSelectionRange(e,f):g.selectionStart&&
(g.selectionStart=e,g.selectionEnd=f);g.focus()}e=d;g=e.offsetLeft||0;for(f=e.offsetTop||0;e=e.offsetParent;)g+=e.offsetLeft,f+=e.offsetTop;e=g;g=0<a.width?a.width:h.width();m.css({width:parseInt(g)+"px",top:f+d.offsetHeight+"px",left:e+"px"}).show()}else s()}function J(b){var c=document.createElement("ul");if(void 0!=$(b).children(".VortalAutoComplete")&&0<$(b).children(".VortalAutoComplete").children().length){b=$(b).children(".VortalAutoComplete").children();var e=b.length;0<a.maxItemsToShow&&
a.maxItemsToShow<e&&(e=a.maxItemsToShow);for(var f=0;f<e;f++){var g=b[f];if(g){var h=document.createElement("li");h.selectValue=$(g).attr("datakeys");$(g).removeAttr("datakeys");h.innerHTML=g.innerHTML;var d=null;if(1<g.length)for(var d=[],k=1;k<g.length;k++)d[d.length]=g[k];h.extra=d;c.appendChild(h);$(h).hover(function(){$("li",c).removeClass("ac_over");$(this).addClass("ac_over");l=$("li",c).indexOf($(this).get(0))},function(){$(this).removeClass("ac_over")}).click(function(a){a.preventDefault();
a.stopPropagation();z(this)})}}}return c}function K(b){a.matchCase||(b=b.toLowerCase());var c=a.cacheLength?b?k.data[b]?k.data[b]:null:null:null;if(c)E(b,c);else if("string"==typeof a.url&&0<a.url.length){var e=F(b);jQuery.ajax({type:"GET",url:e,success:function(a){B(b,a);E(b,a)},error:function(a){addMessage(MessageType.Error,"Unable to retreive data from "+e+" for autocomplete.")},async:!1})}else h.removeClass(a.loadingClass)}function F(b){b=eval(unescape(a.url));for(var c in a.extraParams)b+="&"+
c+"="+encodeURI(a.extraParams[c]);return b}function C(b,c){c&&h.removeClass(a.loadingClass);for(var e=c?c.length:0,f=null,g=0;g<e;g++){var d=c[g];if(d[0].toLowerCase()==b.toLowerCase()){f=document.createElement("li");f.innerHTML=a.formatItem?a.formatItem(d,g,e):d[0];f.selectValue=d[0];var k=null;if(1<d.length)for(var k=[],l=1;l<d.length;l++)k[k.length]=d[l];f.extra=k}}a.onFindValue&&setTimeout(function(){a.onFindValue(f)},1)}function B(b,c){c&&b&&a.cacheLength&&(!k.length||k.length>a.cacheLength?
(p(),k.length++):k[b]||k.length++,k.data[b]=c)}var h=$(d).attr("autocomplete","off");a.inputClass&&h.addClass(a.inputClass);var q=document.createElement("div"),m=$(q);m.hide().addClass(a.resultsClass).css("position","absolute");0<a.width&&m.css("width",a.width);$("body").append(q);d.autocompleter=this;var n=null,v="",l=-1,k={},A=!1,r=null,G=!1;p();if(null!=a.data){var x="",t={},y=[];"string"!=typeof a.url&&(a.cacheLength=1);for(var u=0;u<a.data.length;u++)y="string"==typeof a.data[u]?[a.data[u]]:
a.data[u],0<y[0].length&&(x=y[0].substring(0,1).toLowerCase(),t[x]||(t[x]=[]),t[x].push(y));for(var H in t)a.cacheLength++,B(H,t[H])}h.keydown(function(b){17!=r||65!=b.keyCode&&67!=b.keyCode||(G=!0);r=b.keyCode;switch(b.keyCode){case 38:b.preventDefault();D(-1);break;case 40:b.preventDefault();D(1);break;case 9:case 13:I()&&(h.get(0).blur(),b.preventDefault());break;default:l=-1,n&&clearTimeout(n),n=setTimeout(function(){if(8<r&&32>r||33<=r&&40>=r||G)m.hide();else{var b=h.val();v=b;b.length>=a.minChars?
(h.addClass(a.loadingClass),K(b)):(h.removeClass(a.loadingClass),m.hide());a.removeInputText&&(b=getNextSiblingByNodeName(d,"input"),""!=b.val()&&b.val("").change());null!=a.copySelectedContentTo&&$(".autocompleteSelected").length&&($(".autocompleteSelected").hide(),$(".autocompleteSelectedNoText").length&&$(".autocompleteSelectedNoText").hide(),$(".autocompleteSelected").html(""));h.change()}},a.delay)}}).focus(function(){A=!0}).blur(function(){A=!1;n&&clearTimeout(n);n=setTimeout(s,200);""==getNextSiblingByNodeName(d,
"input").val()&&a.removeInputText&&h.val("")});s();this.flushCache=function(){p()};this.setExtraParams=function(b){a.extraParams=b};this.findValue=function(){var b=h.val();a.matchCase||(b=b.toLowerCase());var c=a.cacheLength?b?k.data[b]?k.data[b]:null:null:null;c?C(b,c):"string"==typeof a.url&&0<a.url.length?$.get(F(b),function(a){B(b,a);C(b,a)}):C(b,null)}};
jQuery.fn.autocomplete=function(d,a,p){a=a||{};a.url=d;a.data="object"==typeof p&&p.constructor==Array?p:null;a.inputClass=a.inputClass||"ac_input";a.resultsClass=a.resultsClass||"ac_results";a.lineSeparator=a.lineSeparator||"\n";a.cellSeparator=a.cellSeparator||"|";a.minChars=a.minChars||1;a.delay=a.delay||400;a.matchCase=a.matchCase||0;a.matchContains=a.matchContains||0;a.cacheLength=a.cacheLength||1;a.mustMatch=a.mustMatch||0;a.extraParams=a.extraParams||{};a.loadingClass=a.loadingClass||"ac_loading";
a.selectFirst=a.selectFirst||!1;a.selectOnly=a.selectOnly||!1;a.maxItemsToShow=a.maxItemsToShow||10;a.autoFill=a.autoFill||!1;a.width=parseInt(a.width,10)||0;this.each(function(){new jQuery.autocomplete(this,a)});return this};jQuery.fn.autocompleteArray=function(d,a){return this.autocomplete(null,a,d)};jQuery.fn.indexOf=function(d){for(var a=0;a<this.length;a++)if(this[a]==d)return a;return-1};
