!function(a){"use strict";function b(a,e){c++,this.element=d(a),this.options=d.extend({},b.defaults,e),this.cache={},this.id="remotelist-"+c,this.options.param||(this.options.param=this.element.prop("name")||"q"),this._createDatalist(),this._bindEvents(this)}var c=0,d=window.webshims&&webshims.$||window.jQuery||window.$;b.defaults={minLength:2,maxLength:-1,source:"",param:"",select:d.noop,renderItem:null},b.createOption=function(a){var b=d(document.createElement("option"));return a&&"string"!=typeof a||(a={value:a}),b.prop("value",a.value),a.label&&b.prop("label",a.label),b.data("optionData",a),b[0]},b.prototype={selectedData:function(){var a=this.selectedOption();return a&&d(a).data("optionData")},selectedOption:function(){var a=null,b=this.val();return b&&(a=d('[value="'+b+'"]',this.element.prop("list"))[0]||null),a},search:function(a){var b,c,e,f,g=this,h=this.options,i=a;h.maxLength>-1&&(i=i.substr(h,h.maxLength)),this.doAjax=!1,this.cache[i]?this.setList(this.cache[i],i):this.currentAjax?this.doAjax=a:(this.element.addClass("list-search"),b={},b[h.param]=a,this.currentAjax=!0,f=function(){f.xhr&&f.xhr.abort&&f.xhr.abort(),g.currentAjax=!1,clearTimeout(f.timer),g.doAjax?g.search(g.doAjax):g.element.removeClass("list-search")},c=d.isFunction(h.source)?h.source:function(a,b,c,e,f){return d.ajax({dataType:"json",url:f,data:e,context:this,error:c,success:b})},e=function(a){g.setList(a,i),f()},f.timer=setTimeout(f,999),f.xhr=c(a,e,f,b,h.source))},setList:function(a,c){a||(a=[]),this.currentOptions!=a&&this.currentVal!==c&&(this.currentOptions=a,this.currentVal=c,this.cache[c]=a,a=d.map(a,b.createOption),this.datalistSelect.html(a),d.fn.updatePolyfill&&this.datalistSelect.updatePolyfill())},_createDatalist:function(){this.datalistSelect=this.element.prop("list"),this.datalistSelect||(this.datalistSelect=d(''),this.element.attr("list",this.id),this.element.after(this.datalistSelect)),this.datalistSelect=d("select",this.datalistSelect)},val:function(){return window.webshims&&webshims.getDataListVal?webshims.getDataListVal(this.element[0]):this.element.prop("value")},widget:function(){return this},_bindEvents:function(a){var b,c,e,f=a.options,g=function(){var c;return function(g){var h=a.val();if(h!==c&&(c=h,"change"==g||!e||e.toLowerCase()!=h.charAt(h.length-1).toLowerCase()))return a.selectedOption()?(clearTimeout(b),f.select&&f.select.call(a.element[0],d.Event("listselect")),a.element.trigger("listselect"),!0):void 0}}();a.element.on({"input focus":function(){var c=function(){var b=a.val();b.length>=f.minLength&&a.search(b)};return function(){clearTimeout(b),b=setTimeout(c,99)}}(),"input change":function(d){clearTimeout(c),"change"==d.type?(clearTimeout(b),a.element.is(":focus")&&g("change")):c=setTimeout(g,9)},keypress:function(){var a=function(){e=""};return function(b){e=String.fromCharCode(b.charCode),setTimeout(a,20)}}(),getoptioncontent:function(a,b){return f.renderItem?f.renderItem(''+b.item.value+"",b.item.label&&''+b.item.label+"",d.data(b.item.elem,"optionData")):void 0}})}},d.fn.remoteList=function(c,e){var f=c,g=this;return this.each(function(){var h,i,j=d(this).data("remoteList");if("string"==typeof j&&(i={source:j},j=!1),j&&d.isPlainObject(j)&&(i=j),j&&"string"==typeof f&&f in j){if(h=j[f]&&j[f].apply?j[f].apply(j,e||[]):j[f],h!==a)return g=h,!1}else d(this).data("remoteList",new b(this,d.extend({},c,i)))}),g},d.fn.remoteList.constructorFn=b}();