/*=======================================================================*\
|| ###################################################################### ||
|| # vBulletin 5.1.9 Patch Level 4
|| # ------------------------------------------------------------------ # ||
|| # Copyright 2000-2016 vBulletin Solutions Inc. All Rights Reserved.  # ||
|| # This file may not be redistributed in whole or significant part.   # ||
|| # ----------------- VBULLETIN IS NOT FREE SOFTWARE ----------------- # ||
|| # http://www.vbulletin.com | http://www.vbulletin.com/license.html   # ||
|| ###################################################################### ||
\*========================================================================*/
window.vBulletin=window.vBulletin||{};window.vBulletin.phrase=window.vBulletin.phrase||{};window.vBulletin.phrase.precache=window.vBulletin.phrase.precache||[];window.vBulletin.phrase.precache=$.merge(window.vBulletin.phrase.precache,[]);var vBulletin_Autocomplete=function(V,c){var B=$(V),a=c.apiClass||null,L=c.apiFunction||"getAutocomplete",e=c.delay||500,d=c.minLength||3,Z=c.maxItems||false,F=B.attr("name")||"autocompleteElements",J=c.containerClass||"entry-field",K=c.placeholderText,P=((typeof c.autoFocus!="undefined")?c.autoFocus:false),G=((typeof c.appendTo!="undefined")?c.appendTo:"body"),N=[],A=B.parent(".autocomplete-container"),g=A.find(".autocompleteHelper"),b=false,T=false,R=B.siblings(".autocomplete-box"),I=c.beforeAdd||false,H=c.afterAdd||false,E=c.beforeDelete||false,C=c.afterDelete||false,O=this,Y=c.editorContext||null,X=function(){if(A.length==0){B.wrap('<div class="autocomplete-container" />');A=B.parent(".autocomplete-container");A.addClass(J).on("mousedown",function(m){var i=this;setTimeout(function(){var n=$(".autocompleteHelper:visible",i);if(n.length>0){n[0].focus()}},10);return false})}if(R.length==0){R=$("<div />").addClass("autocomplete-box").insertBefore(B)}if(g.length==0){g=$('<input type="text" name="autocompleteHelper" />').addClass("autocompleteHelper b-form-input__input--hide-clear-button").attr("placeholder",function(){return K?K:null}).attr("maxlength",function(){return B.attr("maxlength")?B.attr("maxlength"):null}).insertAfter(R)}g.attr("tabindex",B.attr("tabindex")||0);g.off("keydown").on("keydown",function(m){if(m.which==13){var i=$(m.target).val();if(i.length>=d){S(i,true,m)}}});g.off("blur").on("blur",function(i){b=window.setTimeout(function(){if(!T){var m=g.val();if(m.length>=d){S(m,true,i)}}T=false;b=false},200)});g.off("focus").on("focus",function(i){T=b?true:false;return false});var j=["font-family","font-size","font-weight","font-style","color","line-height","height"];var k;for(k=0;k<j.length;++k){A.css(j[k],B.css(j[k]))}var h='<input type="hidden" name="'+F+'"';if(B.attr("id")){h+=' id="'+B.attr("id")+'"'}if(B.attr("class")){h+=' class="'+B.attr("class")+'"'}if(B.attr("data-value")){var l=B.attr("data-value").split(",")}h+=" />";B.replaceWith(h);B=A.find('[name="'+F+'"]');g.autocomplete({autoFocus:P,appendTo:G,delay:e,minLength:d,search:function(i,m){console.log("searching for "+this.value)},source:function(i,m){console.log("getting source for "+i.term);vBulletin.AJAX({url:vBulletin.getAjaxBaseurl()+"/ajax/api/"+a+"/"+L,data:({searchStr:i.term}),success:function(o){console.log("got source for "+i.term+":");if(o&&$.isArray(o.suggestions)){var n=[];$.each(o.suggestions,function(p,q){if(Y&&Y.type=="tags"){n.push({label:q.title,value:pageData.userid})}else{n.push({label:q.title,value:q.value})}});m(n)}else{console.log("/ajax/api/"+a+"/"+L+" successful, but response was not an array");openAlertDialog({title:vBulletin.phrase.get("error"),message:vBulletin.phrase.get("invalid_server_response_please_try_again"),iconType:"error"})}},error_phrase:"error_getting_suggestions"})},select:function(m,i){O.addElement(i.item.label,i.item.value);g.val("");m.preventDefault()},focus:function(m,i){g.val(i.item.label);m.preventDefault()}});R.delegate(".element-x","click",function(p){var n=$(this).closest(".autocomplete");var m=n.find(".element-text").text().toLowerCase();var i=[];var o=false;$(N).each(function(q,r){if(r.label.toLowerCase()==m){o=r}else{i.push(r)}});if(typeof E=="function"){if(!E.call(this,o.label,o.data)){return false}}N=i;n.remove();f();if(typeof C=="function"){C.call(this,o.label,o.data)}});A.off("click").on("click",function(i){if(!$(i.target).hasClass("ui-autocomplete-input")){$(".ui-autocomplete-input",$(this)).focus()}return false});if(l){W.call(this,l)}},M=function(l,k){if(l==""){return false}if(typeof I=="function"){if(!I.call(this,l,k)){return false}}var j=true;$(N).each(function(n,o){if(o.label==l){j=false}});if(!j){return false}if(Z){if(N.length>=Z){return false}}var m=$("<div />").addClass("element-x vb-icon vb-icon-x-blue").text("X");var h=$("<div />").addClass("element-text").text(l);var i=$("<div />").addClass("autocomplete").append(h).append(m).appendTo(R);N.push({label:l,value:k});f();if(typeof H=="function"){H.call(this,l,k)}R.show();return true},W=function(k){var h=k.length,j;for(j=0;j<h;++j){M(k[j])}},U=function(h){Q();W(h)},Q=function(){R.html("");g.val("");N=[];f()},f=function(){var h=[];$(N).each(function(i,j){h.push(j.label)});B.val(h.join(","))},D=function(){return d},S=function(j,h,i){if(Y){switch(Y.type){case"tags":Y.context.addTag(j);break;default:break}}else{O.addElement(j,j);g.val("")}if(typeof h!=="undefined"&&typeof i!=="undefined"){i.preventDefault();return false}};X.call(this);this.addElement=function(i,h){return M.call(this,i,h)};this.addElements=function(h){W.call(this,h)};this.setElements=function(h){U.call(this,h)};this.getElements=function(){return N};this.getLabels=function(){var h=[];$(N).each(function(i,j){h.push(j.label)});return h};this.getValues=function(){var h=[];$(N).each(function(i,j){h.push(j.value)});return h};this.getInputField=function(){return g};this.clearElements=function(){return Q.call(this)};this.countElements=function(){return $(N).length};this.getMinLength=function(){return D()}};