(function(a){var b="ui-customSelect-open",c="ui-customRange-disabled",d="customselect",e;a.widget("ui.customSelect",{options:{placeholder:"Please select some items",defaultValue:null,customRange:!1,customRanges:null,windowFormatter:function(a){return a},customRangeHelper:function(a,b){var c,d;a&&b?(c=a+"-"+b,d=a==b?a:a+" to "+b):a&&!b?(c=a+"p",d=a+"+"):!a&&b&&(c="0-"+b,d="0 to "+b);return[c,d]}},_create:function(){var f=this,g=f.options,h=g.defaultValue,i=this.element,j,k,l=!1;if(!i.is("select"))throw new TypeError("jquery.customSelect expects a <select> element.");f.customValue=null,f.rootId=i.attr("id")+"_customSelect",k=['<div class="ui-customSelect">','  <div class="ui-customSelect-window"><span></span>','    <div class="ui-customSelect-arrow ui-customSelect-downArrow">&#x25BC;</div>','    <div class="ui-customSelect-arrow ui-customSelect-upArrow">&#x25B2;</div>',"  </div>",'  <div class="ui-customSelect-dropdown"><ul></ul></div>',"</div>"],this.root=j=a(k.join("")).attr("id",f.rootId),i.after(j).hide(),this.window=j.find(".ui-customSelect-window span"),this.list=j.find("ul"),f._createFromSelect(),f._setWindowText(),f.window.parent().click(function(a){f._trigger(l?"blur":"focus",a)}),j.delegate("li>input","change",function(b){f._setWindowText(),i.val(f.getVal()).trigger("change"),f._trigger("change",b),a(".ui-customSelect-rangeContainer input").val(""),a(".ui-customSelect-error").hide(),f.customValue=null}),i.bind(d+"focus",function(){j.hasClass(c)||(l=!0,j.addClass(b),a(document).bind("click.customRange"+f.rootId,function(b){a.contains(j[0],b.target)||f._trigger("blur")}))}).bind(d+"blur",function(){a(document).unbind("click.customRange"+f.rootId),l=!1,j.removeClass(b)}).bind(d+"disabled",function(){j.addClass(c),l&&f._trigger("blur")}).bind(d+"enabled",function(){j.removeClass(c)});if(f.isMultiple&&h){function m(){f.getVal().length||f.list.find("input[value="+h+"]").attr("checked",!0).change()}i.bind(d+"change",m),m()}!f.isMultiple&&g.customRange&&e&&e.call(f,j,this.list,g)},getVal:function(){if(this.customValue!==null)return this.customValue;var a=this.root.find("li>input:checked").map(function(){return this.value}).toArray();return a.length?a[this.isMultiple?"slice":"pop"]():this.isMultiple?[]:null},friendlyVal:function(){return this.root.find("input:checked+label").map(function(){return this.innerHTML}).toArray()},_setWindowText:function(a){var b;a||(b=this.friendlyVal(),a=b.length?b.join(", "):this.options.placeholder),this.window.html(this.options.windowFormatter(a))},_createFromSelect:function(){var b=this,c=0,d=b.list,e=d.prev()[0]||d.parent();b.isMultiple=b.element.attr("multiple"),d.empty(),this.element.children().each(function(){var e=this,f=b.rootId+"_"+c++,g='<label for="'+f+'">'+e.innerHTML+"</label>",h;b.isMultiple?h=a('<input id="'+f+'" type="checkbox" value="'+e.value+'">'):h=a('<input id="'+f+'" name="'+b.rootId+'_radio" type="radio" value="'+e.value+'">'),d.append(a("<li></li>").append(h).append(" ").append(g)),e.selected&&h.attr("checked",!0)})},reload:function(){this.element.find("option[data-custom]").remove(),this._createFromSelect()},_setOption:function(a,b){a==="disabled"&&this._trigger((b?"dis":"en")+"abled")},destroy:function(){a.Widget.prototype.destroy.apply(this,arguments),a(document).unbind("click.customRange"+this.rootId),this.root.remove(),this.element.show()}}),e=function(b,c,d){function j(c){var e=minInput.val(),f=maxInput.val(),g={min:e,max:f,widget:i},h,j;isNaN(e)||isNaN(f)?i.setCustomRangeError("Please enter only numbers."):f&e>f?i.setCustomRangeError("Min cannot be bigger than max."):i._trigger("rangechange",c,g)&&(a(".ui-customSelect-error").hide(),h=d.customRangeHelper(e,f),i._setWindowText(h[1]),i.customValue=h[0],j=a('<option data-custom="true" value="'+h[0]+'">'+h[1]+"</option>"),i.element.find("option[data-custom]").remove(),i.element.append(j.attr("selected",!0)).trigger("change",c),i._trigger("change",c),b.find("input:checked").attr("checked",!1),i._trigger("blur"))}var e,f,g,h,i=this;e=['<div class="ui-customSelect-rangeContainer">','  <input class="ui-customSelect-min" placeholder="min" /> to &nbsp;<input class="ui-customSelect-max" placeholder="max" />','  <div style="display: none" class="ui-customSelect-error"></div>',"</div>"],c.after(a(e.join(""))),minInput=b.find(".ui-customSelect-min"),maxInput=b.find(".ui-customSelect-max"),f=b.find(".ui-customSelect-error"),i.setCustomRangeError=function(a){f.show().html(a)},d.customRanges&&(minInput.val(d.customRanges.min),maxInput.val(d.customRanges.max),j()),minInput.add(maxInput).bind("keydown",function(a){a.which===13&&j(a)})}})(jQuery)
