/*
 * 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 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.3 - 2011-05-05
 */
!function(a){function b(b,c){this.$original=a(b),this.buildingSelect=!1,this.ieClick=!1,this.ignoreOriginalChangeEvent=!1,this.options=c,this.buildDom()}b.prototype={generateUid:function(a){return this.uid=this.options.containerClass+a},buildDom:function(){var b=this,c=this.options;"original"===c.addItemTarget&&a("option",this.$original).each(function(b,c){null===a(c).data("bsm-order")&&a(c).data("bsm-order",b)});for(var d=0;a("#"+this.generateUid(d)).size();d++);this.$select=a("<select>",{"class":c.selectClass,name:c.selectClass+this.uid,id:c.selectClass+this.uid,change:a.proxy(this.selectChangeEvent,this),click:a.proxy(this.selectClickEvent,this)}),this.$list=a.isFunction(c.listType)?c.listType(this.$original):a("<"+c.listType+">",{id:c.listClass+this.uid}),this.$list.addClass(c.listClass),this.$container=a("<div>",{"class":c.containerClass,id:this.uid}),this.buildSelect(),this.$original.change(a.proxy(this.originalChangeEvent,this)).wrap(this.$container).before(this.$select),this.$list.parent().length||this.$original.before(this.$list),this.$original.attr("id")&&a("label[for="+this.$original.attr("id")+"]").attr("for",this.$select.attr("id")),this.$list.delegate("."+c.removeClass,"click",function(){return b.dropListItem(a(this).closest("li")),!1}),a.each(c.plugins,function(){this.init(b)})},selectChangeEvent:function(){if(!(a.browser.msie&&a.browser.version<7)||this.ieClick){var b=a("option:selected:eq(0)",this.$select);b.data("orig-option")&&(this.addListItem(b),this.triggerOriginalChange(b.data("orig-option"),"add")),this.ieClick=!1}},selectClickEvent:function(){this.ieClick=!0},originalChangeEvent:function(){this.ignoreOriginalChangeEvent?this.ignoreOriginalChangeEvent=!1:(this.buildSelect(),a.browser.opera&&this.$list.hide().show())},buildSelect:function(){var b=this;this.buildingSelect=!0,this.$select.empty().prepend(a('<option value=""></option>').text(this.$original.attr("title")||this.options.title)),this.$list.empty(),this.$original.children().each(function(){a(this).is("option")?b.addSelectOption(b.$select,a(this)):a(this).is("optgroup")&&b.addSelectOptionGroup(b.$select,a(this))}),this.options.debugMode||this.$original.hide(),this.selectFirstItem(),this.buildingSelect=!1},addSelectOption:function(b,c){var d=a("<option>",{text:c.text(),val:c.val()}).appendTo(b).data("orig-option",c),e=c.is(":selected"),f=c.is(":disabled");c.data("bsm-option",d),e&&!f?(this.addListItem(d),this.disableSelectOption(d)):!e&&f&&this.disableSelectOption(d)},addSelectOptionGroup:function(b,c){var d=this,e=a("<optgroup>",{label:c.attr("label")}).appendTo(b);c.is(":disabled")&&e.attr("disabled","disabled"),a("option",c).each(function(){d.addSelectOption(e,a(this))})},selectFirstItem:function(){a("option:eq(0)",this.$select).attr("selected","selected")},disableSelectOption:function(b){b.addClass(this.options.optionDisabledClass).removeAttr("selected").attr("disabled","disabled").toggle(!this.options.hideWhenAdded),a.browser.msie&&a.browser.version<8&&this.$select.hide().show()},enableSelectOption:function(b){b.removeClass(this.options.optionDisabledClass).removeAttr("disabled").toggle(!this.options.hideWhenAdded),a.browser.msie&&a.browser.version<8&&this.$select.hide().show()},addListItem:function(b){var c,d=b.data("orig-option"),e=this.options;if(d){if(!this.buildingSelect){if(d.is(":selected"))return;d.attr("selected","selected")}switch(c=a("<li>",{"class":e.listItemClass}).append(a("<span>",{"class":e.listItemLabelClass,html:e.extractLabel(b,e)})).append(a("<a>",{href:"#","class":e.removeClass,html:e.removeLabel})).data("bsm-option",b),this.disableSelectOption(b.data("item",c)),e.addItemTarget){case"bottom":this.$list.append(c.hide());break;case"original":var f=d.data("bsm-order"),g=!1;a("."+e.listItemClass,this.$list).each(function(){return f<a(this).data("bsm-option").data("orig-option").data("bsm-order")?(c.hide().insertBefore(this),g=!0,!1):void 0}),g||this.$list.append(c.hide());break;default:this.$list.prepend(c.hide())}this.buildingSelect?a.bsmSelect.effects.show(c):(e.showEffect(c),e.highlightEffect(this.$select,c,e.highlightAddedLabel,this.options),this.selectFirstItem())}},dropListItem:function(b){var c=b.data("bsm-option"),d=this.options;c.removeData("item").data("orig-option").removeAttr("selected"),(this.buildingSelect?a.bsmSelect.effects.remove:d.hideEffect)(b),this.enableSelectOption(c),d.highlightEffect(this.$select,b,d.highlightRemovedLabel,d),this.triggerOriginalChange(c.data("orig-option"),"drop")},triggerOriginalChange:function(a,b){this.ignoreOriginalChangeEvent=!0,this.$original.trigger("change",[{option:a,value:a.val(),item:a.data("bsm-option").data("item"),type:b}])}},a.fn.bsmSelect=function(c){var d=a.extend({},a.bsmSelect.conf,c);return this.each(function(){var c=a(this).data("bsmSelect");c||(c=new b(a(this),d),a(this).data("bsmSelect",c))})},a.bsmSelect={},a.extend(a.bsmSelect,{effects:{show:function(a){a.show()},remove:function(a){a.remove()},highlight:function(b,c,d,e){var f,g=b.attr("id")+e.highlightClass;a("#"+g).remove(),f=a("<span>",{"class":e.highlightClass,id:g,html:d+c.children("."+e.listItemLabelClass).first().text()}).hide(),b.after(f.fadeIn("fast").delay(50).fadeOut("slow",function(){a(this).remove()}))},verticalListAdd:function(b){b.animate({opacity:"show",height:"show"},100,function(){a(this).animate({height:"+=2px"},100,function(){a(this).animate({height:"-=2px"},100)})})},verticalListRemove:function(b){b.animate({opacity:"hide",height:"hide"},100,function(){a(this).prev("li").animate({height:"-=2px"},100,function(){a(this).animate({height:"+=2px"},100)}),a(this).remove()})}},plugins:{}}),a.bsmSelect.conf={listType:"ol",showEffect:a.bsmSelect.effects.show,hideEffect:a.bsmSelect.effects.remove,highlightEffect:a.noop,addItemTarget:"bottom",hideWhenAdded:!1,debugMode:!1,title:"Select...",removeLabel:"remove",highlightAddedLabel:"Added: ",highlightRemovedLabel:"Removed: ",extractLabel:function(a){return a.html()},plugins:[],containerClass:"bsmContainer",selectClass:"bsmSelect",optionDisabledClass:"bsmOptionDisabled",listClass:"bsmList",listItemClass:"bsmListItem",listItemLabelClass:"bsmListItemLabel",removeClass:"bsmListItemRemove",highlightClass:"bsmHighlight"}}(jQuery);
