(function(b,q){function l(){this._state=[];this._defaults={classHolder:"sbHolder",classHolderDisabled:"sbHolderDisabled",classSelector:"sbSelector",classOptions:"sbOptions",classGroup:"sbGroup",classSub:"sbSub",classDisabled:"sbDisabled",classToggleOpen:"sbToggleOpen",classToggle:"sbToggle",classFocus:"sbFocus",speed:200,effect:"slide",onChange:null,onOpen:null,onClose:null}}var m=!0;b.extend(l.prototype,{_isOpenSelectbox:function(b){return!b?!1:this._getInst(b).isOpen},_isDisabledSelectbox:function(b){return!b?
    !1:this._getInst(b).isDisabled},_attachSelectbox:function(c,a){function f(){var a,c,e=this.attr("id").split("_")[1];for(a in g._state)a!==e&&g._state.hasOwnProperty(a)&&(c=b("select[sb='"+a+"']")[0])&&g._closeSelectbox(c)}function h(a,d){var t=d&&d.sub?!0:!1,f=d&&d.disabled?!0:!1;a.each(function(a){var d=b(this),r=b("<li>");d.is(":selected")&&(n.text(d.text()),l=m);a===q-1&&r.addClass("last");!d.is(":disabled")&&!f?(a=b("<a>",{href:"#"+d.val(),rel:d.val()}).text(d.text()).bind("click.sb",function(a){a&&
    a.preventDefault&&a.preventDefault();a=s;var d=b(this);a.attr("id").split("_");g._changeSelectbox(c,d.attr("rel"),d.text());g._closeSelectbox(c)}).bind("mouseover.sb",function(){var a=b(this);a.parent().siblings().find("a").removeClass(e.settings.classFocus);a.addClass(e.settings.classFocus)}).bind("mouseout.sb",function(){b(this).removeClass(e.settings.classFocus)}),t&&a.addClass(e.settings.classSub),d.is(":selected")&&a.addClass(e.settings.classFocus)):(a=b("<span>",{text:d.text()}).addClass(e.settings.classDisabled),
    t&&a.addClass(e.settings.classSub));a.appendTo(r);r.appendTo(p)})}if(this._getInst(c))return!1;var d=b(c),g=this,e=g._newInst(d),k,n,s,p,l=!1;d.find("optgroup");var u=d.find("option"),q=u.length;d.attr("sb",e.uid);b.extend(e.settings,g._defaults,a);g._state[e.uid]=!1;d.hide();k=b("<div>",{id:"sbHolder_"+e.uid,"class":e.settings.classHolder,tabindex:d.attr("tabindex")});n=b("<a>",{id:"sbSelector_"+e.uid,href:"#","class":e.settings.classSelector,click:function(a){a.preventDefault();f.apply(b(this),
    []);a=b(this).attr("id").split("_")[1];g._state[a]?(e.input.trigger("focusout"),g._closeSelectbox(c)):(e.input.trigger("focusin"),g._openSelectbox(c))}});s=b("<a>",{id:"sbToggle_"+e.uid,href:"#","class":e.settings.classToggle,click:function(a){a.preventDefault();f.apply(b(this),[]);a=b(this).attr("id").split("_")[1];g._state[a]?(e.input.trigger("focusout"),g._closeSelectbox(c)):(e.input.trigger("focusin"),g._openSelectbox(c))}});s.appendTo(k);p=b("<ul>",{id:"sbOptions_"+e.uid,"class":e.settings.classOptions,
    css:{display:"none"}});d.children().each(function(){var a=b(this),c,d={};a.is("option")?h(a):a.is("optgroup")&&(c=b("<li>"),b("<span>",{text:a.attr("label")}).addClass(e.settings.classGroup).appendTo(c),c.appendTo(p),a.is(":disabled")&&(d.disabled=!0),d.sub=!0,h(a.find("option"),d))});l||n.text(u.first().text());b.data(c,"selectbox",e);k.data("uid",e.uid).bind("keydown.sb",function(a){var c=a.charCode?a.charCode:a.keyCode?a.keyCode:0,d=b(this),e=d.data("uid"),f=d.siblings("select[sb='"+e+"']").data("selectbox"),
    h=d.siblings(["select[sb='",e,"']"].join("")).get(0),j=d.find("ul").find("a."+f.settings.classFocus);switch(c){case 37:case 38:0<j.length&&(b("a",d).removeClass(f.settings.classFocus),c=j.parent().prevAll("li:has(a)").eq(0).find("a"),0<c.length&&(c.addClass(f.settings.classFocus).focus(),b("#sbSelector_"+e).text(c.text())));break;case 39:case 40:b("a",d).removeClass(f.settings.classFocus);c=0<j.length?j.parent().nextAll("li:has(a)").eq(0).find("a"):d.find("ul").find("a").eq(0);0<c.length&&(c.addClass(f.settings.classFocus).focus(),
    b("#sbSelector_"+e).text(c.text()));break;case 13:0<j.length&&g._changeSelectbox(h,j.attr("rel"),j.text());g._closeSelectbox(h);break;case 9:if(h&&(f=g._getInst(h)))0<j.length&&g._changeSelectbox(h,j.attr("rel"),j.text()),g._closeSelectbox(h);e=parseInt(d.attr("tabindex"),10);a.shiftKey?e--:e++;b("*[tabindex='"+e+"']").focus();break;case 27:g._closeSelectbox(h)}a.stopPropagation();return!1}).delegate("a","mouseover",function(){b(this).addClass(e.settings.classFocus)}).delegate("a","mouseout",function(){b(this).removeClass(e.settings.classFocus)});
    n.appendTo(k);p.appendTo(k);k.insertAfter(d);b("html").live("mousedown",function(a){a.stopPropagation();b("select").selectbox("close")});b([".",e.settings.classHolder,", .",e.settings.classSelector].join("")).mousedown(function(a){a.stopPropagation()})},_detachSelectbox:function(c){var a=this._getInst(c);if(!a)return!1;b("#sbHolder_"+a.uid).remove();b.data(c,"selectbox",null);b(c).show()},_changeSelectbox:function(c,a,f){var h,d=this._getInst(c);d&&(h=this._get(d,"onChange"),b("#sbSelector_"+d.uid).text(f));
    a=a.replace(/\'/g,"\\'");b(c).find("option[value='"+a+"']").attr("selected",m);d&&h?h.apply(d.input?d.input[0]:null,[a,d]):d&&d.input&&d.input.trigger("change")},_enableSelectbox:function(c){var a=this._getInst(c);if(!a||!a.isDisabled)return!1;b("#sbHolder_"+a.uid).removeClass(a.settings.classHolderDisabled);a.isDisabled=!1;b.data(c,"selectbox",a)},_disableSelectbox:function(c){var a=this._getInst(c);if(!a||a.isDisabled)return!1;b("#sbHolder_"+a.uid).addClass(a.settings.classHolderDisabled);a.isDisabled=
    m;b.data(c,"selectbox",a)},_optionSelectbox:function(c,a,f){var h=this._getInst(c);if(!h)return!1;h[a]=f;b.data(c,"selectbox",h)},_openSelectbox:function(c){var a=this._getInst(c);if(a&&!a.isOpen&&!a.isDisabled){var f=b("#sbOptions_"+a.uid),h=parseInt(b(window).height(),10),d=b("#sbHolder_"+a.uid).offset(),g=b(window).scrollTop(),e=f.prev().height(),h=h-(d.top-g)-e/2,d=this._get(a,"onOpen");f.css({top:e+"px",maxHeight:h-e+"px"});"fade"===a.settings.effect?f.fadeIn(a.settings.speed):f.slideDown(a.settings.speed);
    b("#sbToggle_"+a.uid).addClass(a.settings.classToggleOpen);this._state[a.uid]=m;a.isOpen=m;d&&d.apply(a.input?a.input[0]:null,[a]);b.data(c,"selectbox",a)}},_closeSelectbox:function(c){var a=this._getInst(c);if(a&&a.isOpen){var f=this._get(a,"onClose");"fade"===a.settings.effect?b("#sbOptions_"+a.uid).fadeOut(a.settings.speed):b("#sbOptions_"+a.uid).slideUp(a.settings.speed);b("#sbToggle_"+a.uid).removeClass(a.settings.classToggleOpen);this._state[a.uid]=!1;a.isOpen=!1;f&&f.apply(a.input?a.input[0]:
    null,[a]);b.data(c,"selectbox",a)}},_newInst:function(b){return{id:b[0].id.replace(/([^A-Za-z0-9_-])/g,"\\\\$1"),input:b,uid:Math.floor(99999999*Math.random()),isOpen:!1,isDisabled:!1,settings:{}}},_getInst:function(c){try{return b.data(c,"selectbox")}catch(a){throw"Missing instance data for this selectbox";}},_get:function(b,a){return b.settings[a]!==q?b.settings[a]:this._defaults[a]}});b.fn.selectbox=function(c){var a=Array.prototype.slice.call(arguments,1);return"string"==typeof c&&"isDisabled"==
    c||"option"==c&&2==arguments.length&&"string"==typeof arguments[1]?b.selectbox["_"+c+"Selectbox"].apply(b.selectbox,[this[0]].concat(a)):this.each(function(){"string"==typeof c?b.selectbox["_"+c+"Selectbox"].apply(b.selectbox,[this].concat(a)):b.selectbox._attachSelectbox(this,c)})};b.selectbox=new l;b.selectbox.version="0.2"})(jQuery);
