/*
* EasyAutocomplete
* jQuery plugin for autocompletion
*
* @author Łukasz Pawełczak
* @version 1.0.0
* Copyright MIT License: https://github.com/pawelczak/easy-autocomplete/blob/master/LICENSE.txt
*/
var EasyAutocomplete=function(a){return a.Configuration=function(a){function b(){"xml"===a.dataType&&(a.getValue||(a.getValue=function(a){return $(a).text()}),a.list||(a.list={}),a.list.sort||(a.list.sort={}),a.list.sort.method=function(b,c){return b=a.getValue(b),c=a.getValue(c),c>b?-1:b>c?1:0},a.list.match||(a.list.match={}),a.list.match.method=function(b,c){return b=a.getValue(b),c=a.getValue(c),b===c?!0:!1})}function c(){function b(a,c){var d=a||{};for(var e in a)void 0!==c[e]&&null!==c[e]&&("object"!=typeof c[e]||c[e]instanceof Array?d[e]=c[e]:b(a[e],c[e]));return d}f=b(f,a)}function d(){if("list-required"!==f.url&&"function"!=typeof f.url){var a=f.url;f.url=function(){return a}}if("string"==typeof f.listLocation){var b=f.listLocation;f.listLocation=function(a){return a[b]}}if("string"==typeof f.getValue){var c=f.getValue;f.getValue=function(a){return a[c]}}}function e(a){return void 0!==f[a]&&null!==f[a]?!0:!1}var f={data:"list-required",url:"list-required",dataType:"json",listLocation:function(a){return a},xmlElementName:"",getValue:function(a){return a},autocompleteOff:!0,placeholder:!1,ajaxCallback:function(){},list:{sort:{enabled:!1,method:function(a,b){return a=f.getValue(a),b=f.getValue(b),b>a?-1:a>b?1:0}},maxNumberOfElements:6,match:{enabled:!1,caseSensitive:!1,method:function(a,b){return a=f.getValue(a),b=f.getValue(b),a===b?!0:!1}},showAnimation:{type:"normal",time:400,callback:function(){}},hideAnimation:{type:"normal",time:400,callback:function(){}}},highlightPhrase:!0,theme:"",cssClasses:""};b(),c(),d(),this.get=function(a){return f[a]},this.equals=function(a,b){return e(a)&&f[a]===b?!0:!1},this.checkDataUrlProperties=function(){return"list-required"===f.url&&"list-required"===f.data?!1:!0},this.checkRequiredProperties=function(){for(var a in f)if("required"===f[a])return logger.error("Option "+a+" must be defined"),!1;return!0}},a}(EasyAutocomplete||{}),EasyAutocomplete=function(a){return a.Logger=function(){this.error=function(a){console.log("ERROR: "+a)},this.warning=function(a){console.log("WARNING: "+a)}},a}(EasyAutocomplete||{}),EasyAutocomplete=function(a){return a.Constans=function(){var a={CONTAINER_CLASS:"easy-autocomplete-container",CONTAINER_ID:"eac-container-",WRAPPER_CSS_CLASS:"easy-autocomplete"};this.getValue=function(b){return a[b]}},a}(EasyAutocomplete||{}),EasyAutocomplete=function(a){return a.proccess=function(a,b,c){function d(b,c){var d=[],e="";if(a.get("list").match.enabled)for(var f=0,g=b.length;g>f;f+=1)e=a.get("getValue")(b[f]),a.get("list").match.caseSensitive||("string"==typeof e&&(e=e.toLowerCase()),c=c.toLowerCase()),e.search(c)>-1&&d.push(b[f]);else d=b;return d}function e(b){return b.length>a.get("list").maxNumberOfElements&&(b=b.slice(0,a.get("list").maxNumberOfElements)),b}function f(b){return a.get("list").sort.enabled&&b.sort(a.get("list").sort.method),b}var g=c;return b=d(b,g),b=e(b),b=f(b)},a}(EasyAutocomplete||{}),EasyAutocomplete=function(a){return a.main=function(b,c){function d(){return n.checkDataUrlProperties()?n.checkRequiredProperties()?(e(),void g()):void o.error("Will not work without mentioned properties."):void o.error("One of options variables 'data' or 'url' must be defined.")}function e(){function a(){var a=$("
"),c=m.getValue("WRAPPER_CSS_CLASS");n.get("theme")&&(c+=" eac-"+n.get("theme")),n.get("cssClasses")&&(c+=" "+n.get("cssClasses")),a.addClass(c),r.wrap(a),b()}function b(){var a=r.outerWidth();r.parent().css("width",a)}function c(){r.unwrap()}function d(){var a=$("
").addClass(m.getValue("CONTAINER_CLASS"));a.attr("id",f()).prepend($("
")),function(){a.on("show",function(){switch(n.get("list").showAnimation.type){case"slide":var b=n.get("list").showAnimation.time,c=n.get("list").showAnimation.callback;a.find("ul").slideDown(b,c);break;case"fade":var b=n.get("list").showAnimation.time,c=n.get("list").showAnimation.callback;a.find("ul").fadeIn(b),c;break;default:a.find("ul").show()}}).on("hide",function(){switch(n.get("list").hideAnimation.type){case"slide":var b=n.get("list").hideAnimation.time,c=n.get("list").hideAnimation.callback;a.find("ul").slideUp(b,c);break;case"fade":var b=n.get("list").hideAnimation.time,c=n.get("list").hideAnimation.callback;a.find("ul").fadeOut(b,c);break;default:a.find("ul").hide()}}).on("selectElement",function(){a.find("ul li").removeClass("selected"),a.find("ul li:nth-child("+(u+1)+")").addClass("selected")}).on("loadElements",function(b,c,d){var e="",f=($(""),a.find("ul"));f.empty();for(var h=0,i=c.length;i>h;h+=1)e=$(" "),function(){var a=h,b=n.get("getValue")(c[a]);e.find("span").on("click",function(){r.val(b),j(a)}).mouseover(function(){u=a,j(a)}).html(g(b,d))}(),f.append(e)})}(),r.after(a)}function e(){r.next("."+m.getValue("CONTAINER_CLASS")).remove()}function g(a,b){return n.get("highlightPhrase")&&""!==b?h(a,b):a}function h(a,b){return(a+"").replace(new RegExp("("+b+")","gi"),"$1")}r.parent().hasClass(m.getValue("WRAPPER_CSS_CLASS"))&&(e(),c()),a(),d(),s=$("#"+f()),n.get("placeholder")&&r.attr("placeholder",n.get("placeholder"))}function f(){var a=r.attr("id");if(void 0===a||null===a){do a=m.getValue("CONTAINER_ID")+Math.rand(1e4);while(0===$("#"+a).length)}else a=m.getValue("CONTAINER_ID")+a;return a}function g(){function a(){q("autocompleteOff",!0)&&g(),b(),c(),d(),e(),f()}function b(){r.off("keyup").keyup(function(a){function b(){function a(a){var b=[];return $(a).find(n.get("xmlElementName")).each(function(){b.push(this)}),b}var b=r.val();"list-required"!==n.get("data")&&(t=p(n,n.get("data"),r.val()),k(t,b),h()),"list-required"!==n.get("url")&&$.ajax({url:n.get("url")(b),dataType:n.get("dataType")}).done(function(c){t=n.get("listLocation")(c),"XML"===n.get("dataType").toUpperCase()&&(t=a(t));var d=t.length;0!==d&&(t=p(n,t,r.val()),k(t,b),h(),n.get("ajaxCallback")())}).fail(function(){o.warning("Fail to load response data")}).always(function(){})}switch(a.keyCode){case 27:i(),l();break;case 38:a.preventDefault(),t.length>0&&u>0&&(u-=1,r.val(n.get("getValue")(t[u])),j(u));break;case 40:a.preventDefault(),t.length>0&&u40||8===a.keyCode)&&b()}})}function c(){r.on("keydown",function(a){a=a||window.event;var b=a.keyCode;return 38===b?(suppressKeypress=!0,!1):void 0}).keydown(function(a){13===a.keyCode&&u>-1&&(r.val(n.get("getValue")(t[u])),u=-1,i(),a.preventDefault())})}function d(){r.off("keypress")}function e(){r.focus(function(){""!==r.val()&&t.length>0&&(u=-1,h())})}function f(){r.blur(function(){setTimeout(function(){u=-1,i()},250)})}function g(){r.attr("autocomplete","off")}a()}function h(){s.trigger("show"),j(u)}function i(){s.trigger("hide")}function j(a){s.trigger("selectElement",a)}function k(a,b){s.trigger("loadElements",[a,b])}function l(){r.trigger("blur")}var m=new a.Constans,n=new a.Configuration(c),o=new a.Logger,p=a.proccess,q=n.equals,r=b,s="",t=[],u=-1;this.getConstants=function(){return m},this.getConfiguration=function(){return n},this.getContainer=function(){return s},this.build=function(){e()},this.init=function(){d()}},a}(EasyAutocomplete||{});$.fn.easyAutocomplete=function(a){new EasyAutocomplete.main(this,a).init()};