(function($){$.widget("lws.lws_autocomplete",{options:{css:"",cssColor:"",readOnly:!0,sharedElt:undefined,sharedSource:'source',sharedData:'lws_autocomplete'},value:function(value){if(value===undefined){return this._getOption().value}
var elts=$.grep(this.remote.concat(this.local()),function(elt){return elt.value==value});if(elts.length>0)
this.input.val(this._setOption(elts[0]).text());else{this.input.val(value);this.element.empty();this.element.val(value)}},local:function(value){if(value!==undefined){if(!Array.isArray(value))
value=Object.keys(value).map(function(key){return value[key]});this.options.sharedElt.data(this.options.sharedData,value)}
return this.options.sharedElt.data(this.options.sharedData)},_create:function(){var customCss="lws-autocomplete";if(this.element.data('class')!=undefined)customCss+=(" "+this.element.data('class'));this.ajax=(this.element.data('ajax')!=undefined?this.element.data('ajax'):undefined);this.wrapper=$("<div>").addClass(customCss).insertAfter(this.element);this.textbox=$("<div>").appendTo(this.wrapper);if(this.options.css!="")
this.wrapper.addClass(this.options.css);if(this.options.sharedElt==undefined)
this.options.sharedElt=this.element;this.local([]);this.remote=[];this.lastTerm="";this.minSearch=(this.element.data('minsearch')!=undefined?this.element.data('minsearch'):3);this.minOption=(this.element.data('minoption')!=undefined?this.element.data('minoption'):0);this.spec=this.element.data('spec');this.element.hide();this._grabOptions();this._createAutocomplete();this._createShowAllButton();this._on(!0,this.element,{"change":function(){var opt=this._getOption();if(this.input.val()!=opt.label)
this.input.val(this._setOption(opt).text());}})},_createAutocomplete:function(){var arg={source:$.proxy(this,"_source")};if(this.element.data('delay')!=undefined)arg.delay=this.element.data('delay');else if(this.ajax==undefined)arg.delay=0;if(this.element.data('minlength')!=undefined)arg.minLength=this.element.data('minlength');else if(this.ajax==undefined)arg.minLength=0;this.input=$("<input>").appendTo(this.textbox).val(this._getOption().label).attr("title","").addClass("lws-autocomplete-input ui-widget ui-widget-content ui-state-default ui-corner-left").autocomplete(arg).tooltip({tooltipClass:"lws-tooltip lws-autocomplete-tooltip"});if(this.element.data('name')!=undefined)
this.input.attr('name',this.element.data('name'));if(this.element.data("placeholder")!=undefined)
this.input.attr("placeholder",this.element.data("placeholder"));this.input.data("ui-autocomplete")._renderItem=this._renderItem;this.input.data("ui-autocomplete")._resizeMenu=function(){var ul=this.menu.element;ul.outerWidth(this.element.outerWidth())}
this._on(this.input,{autocompleteselect:"_apply",autocompletechange:"_removeIfInvalid",autocompletefocus:"_focusItem",autocompleteopen:"_shiftWidget"})},_createShowAllButton:function(){var input=this.input,wasOpen=!1;var title="Show All Items";if(typeof lws_autocomplete_localize!=="undefined")
title=lws_autocomplete_localize.btnTitle;var a=$("<a>").attr("tabIndex",-1).attr("title",title).tooltip().appendTo(this.wrapper).removeClass("ui-corner-all").addClass("lws-autocomplete-toggle lws-icon lws-icon-small-down");if(this.options.cssColor!="")
a.addClass(this.options.cssColor);a.on("mousedown",function(){wasOpen=input.autocomplete("widget").is(":visible")})
a.on("click",function(){input.trigger("focus");if(wasOpen){return}
var minLength=input.autocomplete("option","minLength");input.autocomplete("option","minLength",0);input.autocomplete("search","");input.autocomplete("option","minLength",minLength)})},_shiftWidget:function(){var offset=this.input.autocomplete("widget").offset();this.input.autocomplete("widget").offset({top:(offset.top+2),left:(offset.left-2)})},_apply:function(event,ui){this.input.val(this._setOption(ui.item).text());return!1},_focusItem:function(event,ui){return!1},_grabOptions:function(){if(this.options.sharedElt.data(this.options.sharedSource)!=undefined){this.local(lwsBase64.toObj(this.options.sharedElt.data(this.options.sharedSource)))}else{var local=[];this.element.children("option").each(function(){var opt=$(this);var arg={value:opt.val(),label:opt.html()};if(opt.data("detail")!=undefined)
arg.detail=opt.data("detail");local.push(arg)});this.local(local)}},_getOption:function(){var value=this.element.val();var elts=$.grep(this.remote.concat(this.local()),function(elt){return elt.value==value});return(elts.length>0?elts[0]:{value:'',label:''})},_setOption:function(opt){var oldVal=this.element.val();this.element.empty();var item=$("<option>").attr("value",opt.value).html(opt.label).attr("selected","selected");this.element.append(item);this.element.val(opt.value);if(oldVal!=this.element.val())
this.element.trigger("change");return item},_source:function(request,response){if(this.ajax!=undefined){if(request.term.length>=this.minSearch)
this._sourceRemote(request,response);else{var rest=this._filterLocal(request);if(rest.length<=this.minOption)
this._sourceRemote(request,response);else response(rest)}}else response(this._filterLocal(request))},_filterLocal:function(request){var matcher=new RegExp($.ui.autocomplete.escapeRegex(request.term),"i");return $.map(this.local(),function(elt){if(elt.value&&(!request.term||matcher.test(elt.label)))
return elt})},_sourceRemote:function(request,response){if(this.lastTerm.length>0&&request.term.indexOf(this.lastTerm)>=0){var matcher=new RegExp($.ui.autocomplete.escapeRegex(request.term),"i");response($.map(this.remote,function(elt){if(elt.value&&(!request.term||matcher.test(elt.label)))
return elt}))}else{var args={action:this.ajax,term:request.term};if(this.spec!=undefined)
args.spec=this.spec;var me=this;$.getJSON(lws_ajax.url,args,function(items){me.remote=[];if(null==items)me._tooltip("Autocomplete ressource error.");else if(0===items)me._tooltip("Autocomplete action not found.");else $.each(items,function(index,value){me.remote.push(value)});response(me.remote);me.lastTerm=request.term}).fail(function(d,textStatus,error){response(me.remote);me._tooltip("Autocomplete loading error, status: "+textStatus+", error: "+error)})}},_renderItem:function(ul,item){var inside=(typeof item.detail!=="undefined"?item.detail:item.label);return $("<li>").attr("data-value",item.value).append(inside).appendTo(ul)},_removeIfInvalid:function(event,ui){if(ui.item)return;var text=this.input.val();var exp=$.ui.autocomplete.escapeRegex(text);if(!this.options.readOnly)
exp='^'+exp+'$';var matcher=new RegExp(exp,"i");if(this._setLastChoice(this.remote,matcher))
return!1;if(this._setLastChoice(this.local(),matcher))
return!1;if(this.options.readOnly){this.input.val("");this.element.val("");this.input.autocomplete("instance").term="";this._tooltip(text+(typeof lws_autocomplete_localize!=="undefined"?lws_autocomplete_localize.notMatch:" didn't match any items"))}else{var option={label:text,value:text};this.local().push(option);this.input.val(this._setOption(option).text())}},_setLastChoice:function(list,matcher){var match=$.map(list,function(elt){if(elt.value&&matcher.test(elt.label))
return elt});if(match.length==1){this.input.val(this._setOption(match[0]).text());return!0}
return!1},_tooltip:function(text){this.input.attr("title",text).tooltip("open");this._delay(function(){this.input.tooltip("close").attr("title","")},2500)},_destroy:function(){this.wrapper.remove();this.element.show()}})})(jQuery)
jQuery(function($){$(".lws_autocomplete").lws_autocomplete()})