/*
 * Better Select Multiple - jQuery Plugin
 *
 * based on Alternate Select Multiple (asmSelect) 1.0.4a beta (http://www.ryancramer.com/projects/asmselect/)
 *
 * Copyright (c) 2009 by Ryan Cramer - http://www.ryancramer.com
 * Copyright (c) 2010-2013 by Victor Berchet - http://www.github.com/vicb
 *
 * Dual licensed under the MIT (MIT-LICENSE.txt) and GPL (GPL-LICENSE.txt) licenses.
 *
 * bsmSelect version: v1.4.7 - 2013-12-19
 */
!function(t){function e(e,i){this.$original=t(e),this.buildingSelect=!1,this.ieClick=!1,this.ignoreOriginalChangeEvent=!1,this.options=i,this.buildDom()}e.prototype={generateUid:function(t){return this.uid=this.options.containerClass+t},buildDom:function(){var e=this,i=this.options;"original"===i.addItemTarget&&t("option",this.$original).each(function(e,i){void 0===t(i).data("bsm-order")&&t(i).data("bsm-order",e)});for(var s=0;t("#"+this.generateUid(s)).size();s++);this.$select=t("<select>",{class:i.selectClass,id:i.selectClass+this.uid,change:t.proxy(this.selectChangeEvent,this),click:t.proxy(this.selectClickEvent,this)}),this.$list=t.isFunction(i.listType)?i.listType(this.$original):t("<"+i.listType+">",{id:i.listClass+this.uid}),this.$list.addClass(i.listClass),this.$container=t("<div>",{class:i.containerClass,id:this.uid}),this.buildSelect(),this.$original.change(t.proxy(this.originalChangeEvent,this)).wrap(this.$container).before(this.$select),this.$list.parent().length||this.$original.before(this.$list),this.$original.attr("id")&&t("label[for='"+this.$original.attr("id")+"']").attr("for",this.$select.attr("id")),this.$list.delegate("."+i.removeClass,"click",function(){return e.dropListItem(t(this).closest("li")),!1}),t.each(i.plugins,function(){this.init(e)})},selectChangeEvent:function(){if(!(t.browser&&t.browser.msie&&t.browser.version<7)||this.ieClick){var e=t("option:selected:eq(0)",this.$select);e.data("orig-option")&&0==this.triggerOriginalChange(e.data("orig-option"),"add")&&this.addListItem(e),this.ieClick=!1}},selectClickEvent:function(){this.ieClick=!0},originalChangeEvent:function(){this.ignoreOriginalChangeEvent?this.ignoreOriginalChangeEvent=!1:(this.buildSelect(),t.browser&&t.browser.opera&&this.$list.hide().show())},buildSelect:function(){var e=this;this.buildingSelect=!0,this.$select.empty().prepend(t('<option value=""></option>').text(this.$original.attr("title")||this.options.title)),this.$list.empty(),this.$original.children().each(function(){t(this).is("option")?e.addSelectOption(e.$select,t(this)):t(this).is("optgroup")&&e.addSelectOptionGroup(e.$select,t(this))}),this.options.debugMode||this.$original.hide(),this.selectFirstItem(),this.buildingSelect=!1},addSelectOption:function(e,i){var s=t("<option>",{text:i.text(),val:i.val()}).appendTo(e).data("orig-option",i),o=i.is(":selected"),a=i.is(":disabled");i.data("bsm-option",s),o&&!a?(this.addListItem(s),this.disableSelectOption(s)):!o&&a&&this.disableSelectOption(s)},addSelectOptionGroup:function(e,i){var s=this,o=t("<optgroup>",{label:i.attr("label")}).appendTo(e);i.is(":disabled")&&o.attr("disabled","disabled"),t("option",i).each(function(){s.addSelectOption(o,t(this))})},selectFirstItem:function(){t("option:eq(0)",this.$select).attr("selected","selected")},disableSelectOption:function(e){e.addClass(this.options.optionDisabledClass).removeAttr("selected").attr("disabled","disabled").toggle(!this.options.hideWhenAdded),t.browser&&t.browser.msie&&t.browser.version<8&&this.$select.hide().show()},enableSelectOption:function(e){e.removeClass(this.options.optionDisabledClass).removeAttr("disabled").toggle(!this.options.hideWhenAdded),t.browser&&t.browser.msie&&t.browser.version<8&&this.$select.hide().show()},addListItem:function(e){var i,s=e.data("orig-option"),o=this.options;if(s){if(!this.buildingSelect){if(s.is(":selected"))return;s.attr("selected","selected")}switch(i=t("<li>",{class:o.listItemClass}).append(t("<span>",{class:o.listItemLabelClass,html:o.extractLabel(e,o)})).append(t("<a>",{href:"#",class:o.removeClass,html:o.removeLabel})).data("bsm-option",e),this.disableSelectOption(e.data("item",i)),o.addItemTarget){case"bottom":this.$list.append(i.hide());break;case"original":var a=s.data("bsm-order"),l=!1;this.$list.children().each(function(){if(a<t(this).data("bsm-option").data("orig-option").data("bsm-order"))return i.hide().insertBefore(this),l=!0,!1}),l||this.$list.append(i.hide());break;default:this.$list.prepend(i.hide())}this.buildingSelect?t.bsmSelect.effects.show(i):(o.showEffect(i),o.highlightEffect(this.$select,i,o.highlightAddedLabel,this.options),this.selectFirstItem())}},dropListItem:function(e){var i=e.data("bsm-option"),s=this.options;0==this.triggerOriginalChange(i.data("orig-option"),"drop")&&(i.removeData("item").data("orig-option").removeAttr("selected"),(this.buildingSelect?t.bsmSelect.effects.remove:s.hideEffect)(e),this.enableSelectOption(i),s.highlightEffect(this.$select,e,s.highlightRemovedLabel,s))},triggerOriginalChange:function(e,i){var s=t.Event("change");return this.ignoreOriginalChangeEvent=!0,this.$original.trigger(s,[{option:e,value:e.val(),item:e.data("bsm-option").data("item"),type:i}]),s.isDefaultPrevented()}},t.fn.bsmSelect=function(i){var s=t.extend({},t.bsmSelect.conf,i);return this.each(function(){var i=t(this).data("bsmSelect");i||(i=new e(t(this),s),t(this).data("bsmSelect",i))})},t.bsmSelect={},t.extend(t.bsmSelect,{effects:{show:function(t){t.show()},remove:function(t){t.remove()},highlight:function(e,i,s,o){var a,l=e.attr("id")+o.highlightClass;t("#"+l).remove(),a=t("<span>",{class:o.highlightClass,id:l,html:s+i.children("."+o.listItemLabelClass).first().text()}).hide(),e.after(a.fadeIn("fast").delay(50).fadeOut("slow",function(){t(this).remove()}))},verticalListAdd:function(e){e.animate({opacity:"show",height:"show"},100,function(){t(this).animate({height:"+=2px"},100,function(){t(this).animate({height:"-=2px"},100)})})},verticalListRemove:function(e){e.animate({opacity:"hide",height:"hide"},100,function(){t(this).prev("li").animate({height:"-=2px"},100,function(){t(this).animate({height:"+=2px"},100)}),t(this).remove()})}},plugins:{}}),t.bsmSelect.conf={listType:"ol",showEffect:t.bsmSelect.effects.show,hideEffect:t.bsmSelect.effects.remove,highlightEffect:t.noop,addItemTarget:"bottom",hideWhenAdded:!1,debugMode:!1,title:"Select...",removeLabel:"remove",highlightAddedLabel:"Added: ",highlightRemovedLabel:"Removed: ",extractLabel:function(t){return t.html()},plugins:[],containerClass:"bsmContainer",selectClass:"bsmSelect",optionDisabledClass:"bsmOptionDisabled",listClass:"bsmList",listItemClass:"bsmListItem",listItemLabelClass:"bsmListItemLabel",removeClass:"bsmListItemRemove",highlightClass:"bsmHighlight"}}(jQuery);