if(jQuery){(function($){$.extend($.fn,{multiSelect:function(options,callback){var defaults={selectAll:true,selectAllText:"Select All",noneSelected:"Select options",allSelected:"All select",oneOrMoreSelected:"% selected",inputClass:"multiSelect",listClass:"multiSelectOptions",listWidth:"",listTop:34,listLeft:4,listDirection:"down"};var options=$.extend(defaults,options);var hasfocus=0;$(this).each(function(){var select=$(this);var html='<input type="text" readonly="readonly" class="'+options.inputClass+'" value="" style="cursor: default;" />';html+='<div class="'+options.listClass+'" style="position: absolute; z-index: 301; display: none;">';if(options.selectAll){html+='<label class="selectAll"><span>'+options.selectAllText+'</span><input type="checkbox" class="selectAll" /></label>'}$(select).find("option").each(function(){if($(this).val()!=""){html+="<label><span>"+$(this).html()+'</span><input type="checkbox" name="'+$(select).attr("name")+'" value="'+$(this).val()+'"';if($(this).attr("selected")){html+=' checked="checked"'}html+=" /></label>"}});html+="</div>";$(select).after(html);$(select).next("."+options.inputClass).mouseover(function(){$(this).addClass("hover")}).mouseout(function(){$(this).removeClass("hover")}).click(function(){if($(this).hasClass("active")){$(this).multiSelectOptionsHide()}else{$(this).multiSelectOptionsShow(options)}return false}).focus(function(){$(this).addClass("focus")}).blur(function(){$(this).removeClass("focus");if($(this).hasClass("active")&&hasfocus>0){}else{$(this).multiSelectOptionsHide()}});if(options.selectAll){var sa=true;$(select).next("."+options.inputClass).next("."+options.listClass).find("input:checkbox").not(".selectAll").each(function(){if(!$(this).attr("checked")){sa=false}});if(sa){$(select).next("."+options.inputClass).next("."+options.listClass).find("input.selectAll").attr("checked",true).parent().addClass("checked")}}$(select).next("."+options.inputClass).next("."+options.listClass).find("input.selectAll").click(function(){if($(this).attr("checked")==true){$(this).parent().parent().find("input:checkbox").attr("checked",true).parent().addClass("checked")}else{$(this).parent().parent().find("input:checkbox").attr("checked",false).parent().removeClass("checked")}});$(select).next("."+options.inputClass).next("."+options.listClass).find("input:checkbox").click(function(){$(this).parent().parent().multiSelectUpdateSelected(options);$(this).parent().parent().find("label").removeClass("checked").find("input:checked").parent().addClass("checked");$(this).parent().parent().prev("."+options.inputClass).focus();if(!$(this).attr("checked")){$(this).parent().parent().find("input:checkbox.selectAll").attr("checked",false).parent().removeClass("checked")}if(callback){callback($(this).val())}});$(select).next("."+options.inputClass).next("."+options.listClass).each(function(){$(this).multiSelectUpdateSelected(options);$(this).find("input:checked").parent().addClass("checked")});$(select).next("."+options.inputClass).next("."+options.listClass).find("label").mouseover(function(){hasfocus=1;$(this).parent().find("label").removeClass("hover");$(this).addClass("hover")}).mouseout(function(){hasfocus=-1;$(this).parent().find("label").removeClass("hover")});$(select).next("."+options.inputClass).keydown(function(e){if($(this).next("."+options.listClass).is(":visible")){if(e.keyCode==9){$(this).addClass("focus").trigger("click");$(this).focus().next(":input").focus();return true}if(e.keyCode==27||e.keyCode==37||e.keyCode==39){$(this).addClass("focus").trigger("click")}if(e.keyCode==40){if(!$(this).next("."+options.listClass).find("label").hasClass("hover")){$(this).next("."+options.listClass).find("label:first").addClass("hover")}else{$(this).next("."+options.listClass).find("label.hover").removeClass("hover").next("label").addClass("hover");if(!$(this).next("."+options.listClass).find("label").hasClass("hover")){$(this).next("."+options.listClass).find("label:first").addClass("hover")}}return false}if(e.keyCode==38){if(!$(this).next("."+options.listClass).find("label").hasClass("hover")){$(this).next("."+options.listClass).find("label:first").addClass("hover")}else{$(this).next("."+options.listClass).find("label.hover").removeClass("hover").prev("label").addClass("hover");if(!$(this).next("."+options.listClass).find("label").hasClass("hover")){$(this).next("."+options.listClass).find("label:last").addClass("hover")}}return false}if(e.keyCode==13||e.keyCode==32){if($(this).next("."+options.listClass).find("label.hover input:checkbox").hasClass("selectAll")){if($(this).next("."+options.listClass).find("label.hover input:checkbox").attr("checked")){$(this).next("."+options.listClass).find("input:checkbox").attr("checked",false).parent().removeClass("checked")}else{$(this).next("."+options.listClass).find("input:checkbox").attr("checked",true).parent().addClass("checked")}$(this).next("."+options.listClass).multiSelectUpdateSelected(options);if(callback){callback($(this))}return false}if($(this).next("."+options.listClass).find("label.hover input:checkbox").attr("checked")){$(this).next("."+options.listClass).find("label.hover input:checkbox").attr("checked",false);$(this).next("."+options.listClass).multiSelectUpdateSelected(options);$(this).next("."+options.listClass).find("label").removeClass("checked").find("input:checked").parent().addClass("checked");$(this).next("."+options.listClass).find("input:checkbox.selectAll").attr("checked",false).parent().removeClass("checked");if(callback){callback($(this))}}else{$(this).next("."+options.listClass).find("label.hover input:checkbox").attr("checked",true);$(this).next("."+options.listClass).multiSelectUpdateSelected(options);$(this).next("."+options.listClass).find("label").removeClass("checked").find("input:checked").parent().addClass("checked");if(callback){callback($(this))}}}return false}else{if(e.keyCode==38||e.keyCode==40||e.keyCode==13||e.keyCode==32){$(this).removeClass("focus").trigger("click");$(this).next("."+options.listClass).find("label:first").addClass("hover");return false}if(e.keyCode==9){$(this).focus().next(":input").focus();return true}}if(e.keyCode==13){return false}});$(select).remove()})},multiSelectOptionsHide:function(){$(this).hasfocus=0;$(this).removeClass("active").next("div").hide()},multiSelectOptionsShow:function(options){$("."+options.inputClass).multiSelectOptionsHide();$(this).next("."+options.listClass).find("label").removeClass("hover");$(this).addClass("active").next("."+options.listClass).show();if(options.listWidth!=""){$(this).next("."+options.listClass).css({width:options.listWidth+"px"})}if(options.listDirection=="up"){var offset=$(this).next("."+options.listClass).outerHeight();$(this).next("."+options.listClass).css({top:-(offset)+"px"})}else{$(this).next("."+options.listClass).css({top:options.listTop+"px"})}$(this).next("."+options.listClass).css({left:options.listLeft+"px"});multiSelectCurrent=$(this);var timer="";$(this).next("."+options.listClass).hover(function(){clearTimeout(timer)},function(){timer=setTimeout('$(multiSelectCurrent).multiSelectOptionsHide(); $(multiSelectCurrent).unbind("hover");',250)})},multiSelectUpdateSelected:function(options){var i=0,s="";$(this).find("input:checkbox:checked").not(".selectAll").each(function(){i++});if(i==0){$(this).prev("input."+options.inputClass).val(options.noneSelected)}else{if(options.selectAll&&$(this).find("label.selectAll input:checkbox").attr("checked")){$(this).prev("input."+options.inputClass).val(options.allSelected)}else{if(options.oneOrMoreSelected=="*"){var display="";$(this).find("input:checkbox:checked").each(function(){if($(this).parent().text()!=options.selectAllText){display=display+$(this).parent().text()+", "}});display=display.substr(0,display.length-2);$(this).prev("input."+options.inputClass).val(display)}else{$(this).prev("input."+options.inputClass).val(options.oneOrMoreSelected.replace("%",i))}}}}})})(jQuery)};