(function(e){function t(t,n){this.$el=e(t);this.id=Math.random();this.options=n;this.multiple=this.$el.prop("multiple");this.activeOpt=null;this.widthSet=false;this.generate()}function n(t,n){this.$el=e(t);this.wSelect=n}t.prototype={generate:function(){if(!this.$select){var t=this;this.$select=e('
');this.$optionsHolder=e('
');this.$options=e('
');if(!e.support.placeholder){this.$select.css("zIndex",100-this.$el.index())}var n=function(n){n.stopPropagation();e("select").each(function(){var n=e(this).data("wSelect");if(n&&n.id!==t.id){if(!n.multiple){n.$optionsHolder.hide()}n.onBlur()}});if(!t.multiple){t.onClick(n)}t.$el.focus()};if(this.multiple){this.$select.addClass("wSelect-multiple");this.$optionsHolder.click(n)}else{this.$selected=e('
');this.$select.append(this.$selected);this.$select.click(n);this.$optionsHolder.click(function(e){e.stopPropagation();t.$el.focus()})}this.$select.hover(function(){t.onFocus("hover")},function(){t.onBlur("hover")});this.$el.addClass("wSelect-el").change(function(){t.change()}).focus(function(){t.onFocus()}).keydown(function(e){t.keydown(e)}).keyup(function(e){t.keyup(e)});e(document).click(function(){if(!t.multiple){t.$optionsHolder.hide()}t.onBlur()});this.widthSet=this.$select.width()>0;this.setTheme(this.options.theme);this.setSize(this.options.size);this.reset();this.$optionsHolder.append(this.$options);this.$select.append(this.$optionsHolder);this.$el.after(this.$select)}return this.$select},reset:function(){var t=this;this.$options.children().remove();this.$el.children().each(function(){var r=new n(this,t);e.data(this,"wSelect-option",r);t.$options.append(r.generate())});this.$options.children().removeClass("wSelect-option-last").last().addClass("wSelect-option-last");this.setSize(this.options.size)},change:function(){this.$options.children().removeClass("wSelect-option-selected wSelect-option-active");this.$el.children(":selected").each(function(){e(this).data("wSelect-option").select()})},keydown:function(e){if(e.keyCode===9){this.$optionsHolder.hide();this.onBlur()}},keyup:function(e){if(e.keyCode===13){this.$optionsHolder.hide()}else if(e.keyCode>=37&&e.keyCode<=40){this.change();var t=this.$options.find(".wSelect-option-selected:last"),n=this.$options.scrollTop(),r=t.position().top+n,i=this.$options.height(),s=t.outerHeight(true);if(r-n<0){this.$options.scrollTop(r)}else if(r+s-n>i){this.$options.scrollTop(r-i+s)}}},onClick:function(t){if(!this.$optionsHolder.is(":visible")){var n=this.$select.offset().top-e(window).scrollTop(),r=this.$optionsHolder.outerHeight(),i=n-r,s=e(window).height()-(n+this.$select.outerHeight()+r+5),o=s>0||s>i?this.$select.height():-r;this.$optionsHolder.css("top",o)}this.$optionsHolder.toggle()},onFocus:function(e){e=e||"active";if(this.options.highlight){this.$select.addClass("wSelect-"+e)}},onBlur:function(e){e=e||"active";if(this.options.highlight){this.$select.removeClass("wSelect-"+e)}},setTheme:function(e){this.$select.attr("class",this.$select.attr("class").replace(/wSelect-theme-.+\s|wSelect-theme-.+$/,""));this.$select.addClass("wSelect-theme-"+e)},setSize:function(t){var n=this.$options.children(":first").clone().css({position:"absolute",left:-1e4}),r=this.$el.children().length,i;e("body").append(n);i=n.outerHeight(true);n.remove();if(!this.multiple&&t>r){t=r}this.$options.height(i*t-1)}};n.prototype={generate:function(){var t=this;if(!this.$option){var n=this.$el.attr("data-icon");this.$option=e('
');this.$value=e('
');this.$option.append(this.$value);if(typeof n==="string"){this.$value.addClass("wSelect-option-icon");this.$value.css("backgroundImage","url("+n+")")}}if(this.$el.prop("selected")){this.select()}if(this.$el.prop("disabled")){this.$option.addClass("wSelect-option-disabled")}else{this.$option.removeClass("wSelect-option-disabled");this.$option.unbind("click").click(function(e){t.onClick(e)})}this.$value.html(this.$el.html());this.setWidth();return this.$option},select:function(){if(!this.wSelect.activeOpt){this.wSelect.activeOpt=this}if(!this.wSelect.multiple){var e=this.$el.attr("data-icon");if(typeof e==="string"){this.wSelect.$selected.addClass("wSelect-option-icon");this.wSelect.$selected.css("backgroundImage","url("+e+")")}else{this.wSelect.$selected.removeClass("wSelect-option-icon");this.wSelect.$selected.css("backgroundImage","")}this.wSelect.$selected.html(this.$el.html())}this.$option.addClass("wSelect-option-selected")},onClick:function(t){var n=null;if(this.wSelect.multiple&&(t.ctrlKey||t.shiftKey)){if(t.ctrlKey||!this.wSelect.activeOpt){n=this.wSelect.$el.val()||[];var r=this.$el.val(),i=e.inArray(r,n);if(i===-1){n.push(this.$el.val());this.wSelect.activeOpt=this}else{n.splice(i,1)}}else if(t.shiftKey){var s=this.wSelect.activeOpt.$el.index(),o=this.$el.index(),u=0,a=0,f=null;if(o>s){u=s;a=o}else{u=o;a=s}n=[];for(var l=u;l<=a;l++){f=this.wSelect.$el.children(":eq("+l+")");if(f.is(":not(:disabled)")){n.push(f.val())}}}}else{n=this.$el.val();this.wSelect.$optionsHolder.hide();this.wSelect.activeOpt=this}this.wSelect.$el.val(n).change()},setWidth:function(){if(this.wSelect.multiple||this.wSelect.widthSet){return true}this.$option.hide().appendTo("body");var e=this.$option.width();if(e>this.wSelect.$select.width()){this.wSelect.$select.width(e)}this.$option.detach().show()}};e.support.placeholder="placeholder"in document.createElement("input");e.fn.wSelect=function(n,r){function o(r){var i=e.data(r,"wSelect");if(!i){var s=jQuery.extend(true,{},n);s.size=e(r).prop("size")||s.size;i=new t(r,s);e.data(r,"wSelect",i)}return i}if(typeof n==="string"){var i=[];var s=this.each(function(){var t=e(this).data("wSelect");if(t){var s=(r?"set":"get")+n.charAt(0).toUpperCase()+n.substring(1).toLowerCase();if(t[n]){t[n].apply(t,[r])}else if(r){if(t[s]){t[s].apply(t,[r])}if(t.options[n]){t.options[n]=r}}else{if(t[s]){i.push(t[s].apply(t,[r]))}else if(t.options[n]){i.push(t.options[n])}else{i.push(null)}}}});if(i.length===1){return i[0]}else if(i.length>0){return i}else{return s}}n=e.extend({},e.fn.wSelect.defaults,n);return this.each(function(){o(this)})};e.fn.wSelect.defaults={theme:"classic",size:"4",highlight:true}})(jQuery)