if(jQuery)(function(a){a.extend(a.fn,{selectBox:function(b,c){var d,e="";var f=function(b,c){if(navigator.userAgent.match(/iPad|iPhone/i))return false;if(b.tagName.toLowerCase()!=="select")return false;b=a(b);if(b.data("selectBox-control"))return false;var d=a('<a class="selectBox" />'),e=b.attr("multiple")||parseInt(b.attr("size"))>1;var f=c||{};d.addClass(b.attr("class")).attr("style",b.attr("style")||"").attr("title",b.attr("title")||"").attr("tabindex",parseInt(b.attr("tabindex"))).css("display","inline-block").bind("focus.selectBox",function(){if(this!==document.activeElement)a(document.activeElement).blur();if(d.hasClass("selectBox-active"))return;d.addClass("selectBox-active");b.trigger("focus")}).bind("blur.selectBox",function(){if(!d.hasClass("selectBox-active"))return;d.removeClass("selectBox-active");b.trigger("blur")});if(b.attr("disabled"))d.addClass("selectBox-disabled");if(e){var h=g(b,"inline");d.append(h).data("selectBox-options",h).addClass("selectBox-inline").addClass("selectBox-menuShowing").bind("keydown.selectBox",function(a){o(b,a)}).bind("keypress.selectBox",function(a){p(b,a)}).bind("mousedown.selectBox",function(b){if(a(b.target).is("A.selectBox-inline"))b.preventDefault();if(!d.hasClass("selectBox-focus"))d.focus()}).insertAfter(b);if(!b[0].style.height){var k=b.attr("size")?parseInt(b.attr("size")):5;var l=d.clone().removeAttr("id").css({position:"absolute",top:"-9999em"}).show().appendTo("body");l.find(".selectBox-options").html("<li><a> </a></li>");optionHeight=parseInt(l.find(".selectBox-options A:first").html(" ").outerHeight());l.remove();d.height(optionHeight*k)}u(d)}else{var m=a('<span class="selectBox-label" />'),n=a('<span class="selectBox-arrow" />');m.text(a(b).find("OPTION:selected").text()||" ");var h=g(b,"dropdown");h.appendTo("BODY");d.data("selectBox-options",h).addClass("selectBox-dropdown").append(m).append(n).bind("mousedown.selectBox",function(a){if(d.hasClass("selectBox-menuShowing")){j()}else{a.stopPropagation();h.data("selectBox-down-at-x",a.screenX).data("selectBox-down-at-y",a.screenY);i(b)}}).bind("keydown.selectBox",function(a){o(b,a)}).bind("keypress.selectBox",function(a){p(b,a)}).insertAfter(b);u(d)}b.addClass("selectBox").data("selectBox-control",d).data("selectBox-settings",f).hide()};var g=function(b,c){var d;switch(c){case"inline":d=a('<ul class="selectBox-options" />');if(b.find("OPTGROUP").length){b.find("OPTGROUP").each(function(){var b=a('<li class="selectBox-optgroup" />');b.text(a(this).attr("label"));d.append(b);a(this).find("OPTION").each(function(){var b=a("<li />"),c=a("<a />");b.addClass(a(this).attr("class"));c.attr("rel",a(this).val()).text(a(this).text());b.append(c);if(a(this).attr("disabled"))b.addClass("selectBox-disabled");if(a(this).attr("selected"))b.addClass("selectBox-selected");d.append(b)})})}else{b.find("OPTION").each(function(){var b=a("<li />"),c=a("<a />");b.addClass(a(this).attr("class"));c.attr("rel",a(this).val()).text(a(this).text());b.append(c);if(a(this).attr("disabled"))b.addClass("selectBox-disabled");if(a(this).attr("selected"))b.addClass("selectBox-selected");d.append(b)})}d.find("A").bind("mouseover.selectBox",function(c){l(b,a(this).parent())}).bind("mouseout.selectBox",function(c){m(b,a(this).parent())}).bind("mousedown.selectBox",function(a){a.preventDefault();if(!b.selectBox("control").hasClass("selectBox-active"))b.selectBox("control").focus()}).bind("mouseup.selectBox",function(c){j();k(b,a(this).parent(),c)});u(d);return d;case"dropdown":d=a('<ul class="selectBox-dropdown-menu selectBox-options" />');if(b.find("OPTGROUP").length){b.find("OPTGROUP").each(function(){var b=a('<li class="selectBox-optgroup" />');b.text(a(this).attr("label"));d.append(b);a(this).find("OPTION").each(function(){var b=a("<li />"),c=a("<a />");b.addClass(a(this).attr("class"));c.attr("rel",a(this).val()).text(a(this).text());b.append(c);if(a(this).attr("disabled"))b.addClass("selectBox-disabled");if(a(this).attr("selected"))b.addClass("selectBox-selected");d.append(b)})})}else{if(b.find("OPTION").length>0){b.find("OPTION").each(function(){var b=a("<li />"),c=a("<a />");b.addClass(a(this).attr("class"));c.attr("rel",a(this).val()).text(a(this).text());b.append(c);if(a(this).attr("disabled"))b.addClass("selectBox-disabled");if(a(this).attr("selected"))b.addClass("selectBox-selected");d.append(b)})}else{d.append("<li> </li>")}}d.data("selectBox-select",b).css("display","none").appendTo("BODY").find("A").bind("mousedown.selectBox",function(a){a.preventDefault();if(a.screenX===d.data("selectBox-down-at-x")&&a.screenY===d.data("selectBox-down-at-y")){d.removeData("selectBox-down-at-x").removeData("selectBox-down-at-y");j()}}).bind("mouseup.selectBox",function(c){if(c.screenX===d.data("selectBox-down-at-x")&&c.screenY===d.data("selectBox-down-at-y")){return}else{d.removeData("selectBox-down-at-x").removeData("selectBox-down-at-y")}k(b,a(this).parent());j()}).bind("mouseover.selectBox",function(c){l(b,a(this).parent())}).bind("mouseout.selectBox",function(c){m(b,a(this).parent())});u(d);return d}};var h=function(b){b=a(b);var c=b.data("selectBox-control");if(!c)return;var d=c.data("selectBox-options");d.remove();c.remove();b.removeClass("selectBox").removeData("selectBox-control").removeData("selectBox-settings").show()};var i=function(b){b=a(b);var c=b.data("selectBox-control"),d=b.data("selectBox-settings"),e=c.data("selectBox-options");if(c.hasClass("selectBox-disabled"))return false;j();e.css({width:c.outerWidth()-(parseInt(c.css("borderLeftWidth"))+parseInt(c.css("borderLeftWidth"))),top:c.offset().top+c.outerHeight()-parseInt(c.css("borderBottomWidth")),left:c.offset().left});switch(d.menuTransition){case"fade":e.fadeIn(d.menuSpeed);break;case"slide":e.slideDown(d.menuSpeed);break;default:e.show(d.menuSpeed);break}var f=e.find(".selectBox-selected:first");n(b,f,true);l(b,f);c.addClass("selectBox-menuShowing");a(document).bind("mousedown.selectBox",function(b){if(a(b.target).parents().andSelf().hasClass("selectBox-options"))return;j()})};var j=function(){if(a(".selectBox-dropdown-menu").length===0)return;a(document).unbind("mousedown.selectBox");a(".selectBox-dropdown-menu").each(function(){var b=a(this),c=b.data("selectBox-select"),d=c.data("selectBox-control"),e=c.data("selectBox-settings");switch(e.menuTransition){case"fade":b.fadeOut(e.menuSpeed);break;case"slide":b.slideUp(e.menuSpeed);break;default:b.hide(e.menuSpeed);break}d.removeClass("selectBox-menuShowing")})};var k=function(b,c,d){b=a(b);c=a(c);var e=b.data("selectBox-control"),f=b.data("selectBox-settings");if(e.hasClass("selectBox-disabled"))return false;if(c.length===0||c.hasClass("selectBox-disabled"))return false;if(b.attr("multiple")){if(d.shiftKey&&e.data("selectBox-last-selected")){c.toggleClass("selectBox-selected");var g;if(c.index()>e.data("selectBox-last-selected").index()){g=c.siblings().slice(e.data("selectBox-last-selected").index(),c.index())}else{g=c.siblings().slice(c.index(),e.data("selectBox-last-selected").index())}g=g.not(".selectBox-optgroup, .selectBox-disabled");if(c.hasClass("selectBox-selected")){g.addClass("selectBox-selected")}else{g.removeClass("selectBox-selected")}}else if(d.metaKey){c.toggleClass("selectBox-selected")}else{c.siblings().removeClass("selectBox-selected");c.addClass("selectBox-selected")}}else{c.siblings().removeClass("selectBox-selected");c.addClass("selectBox-selected")}if(e.hasClass("selectBox-dropdown")){e.find(".selectBox-label").text(c.text())}var h=0,i=[];if(b.attr("multiple")){e.find(".selectBox-selected A").each(function(){i[h++]=a(this).attr("rel")})}else{i=c.find("A").attr("rel")}e.data("selectBox-last-selected",c);if(b.val()!==i){b.val(i);b.trigger("change")}return true};var l=function(b,c){b=a(b);c=a(c);var d=b.data("selectBox-control"),e=d.data("selectBox-options");e.find(".selectBox-hover").removeClass("selectBox-hover");c.addClass("selectBox-hover")};var m=function(b,c){b=a(b);c=a(c);var d=b.data("selectBox-control"),e=d.data("selectBox-options");e.find(".selectBox-hover").removeClass("selectBox-hover")};var n=function(b,c,d){if(!c||c.length===0)return;b=a(b);var e=b.data("selectBox-control"),f=e.data("selectBox-options"),g=e.hasClass("selectBox-dropdown")?f:f.parent(),h=parseInt(c.offset().top-g.position().top),i=parseInt(h+c.outerHeight());if(d){g.scrollTop(c.offset().top-g.offset().top+g.scrollTop()-g.height()/2)}else{if(h<0){g.scrollTop(c.offset().top-g.offset().top+g.scrollTop())}if(i>g.height()){g.scrollTop(c.offset().top+c.outerHeight()-g.offset().top+g.scrollTop()-g.height())}}};var o=function(b,c){b=a(b);var d=b.data("selectBox-control"),f=d.data("selectBox-options"),g=0,h=0;if(d.hasClass("selectBox-disabled"))return;switch(c.keyCode){case 8:c.preventDefault();e="";break;case 9:case 27:j();m(b);break;case 13:if(d.hasClass("selectBox-menuShowing")){k(b,f.find("LI.selectBox-hover:first"),c);if(d.hasClass("selectBox-dropdown"))j()}else{i(b)}break;case 38:case 37:c.preventDefault();if(d.hasClass("selectBox-menuShowing")){var o=f.find(".selectBox-hover").prev("LI");g=f.find("LI:not(.selectBox-optgroup)").length;h=0;while(o.length===0||o.hasClass("selectBox-disabled")||o.hasClass("selectBox-optgroup")){o=o.prev("LI");if(o.length===0)o=f.find("LI:last");if(++h>=g)break}l(b,o);n(b,o)}else{i(b)}break;case 40:case 39:c.preventDefault();if(d.hasClass("selectBox-menuShowing")){var p=f.find(".selectBox-hover").next("LI");g=f.find("LI:not(.selectBox-optgroup)").length;h=0;while(p.length===0||p.hasClass("selectBox-disabled")||p.hasClass("selectBox-optgroup")){p=p.next("LI");if(p.length===0)p=f.find("LI:first");if(++h>=g)break}l(b,p);n(b,p)}else{i(b)}break}};var p=function(b,c){b=a(b);var f=b.data("selectBox-control"),g=f.data("selectBox-options");if(f.hasClass("selectBox-disabled"))return;switch(c.keyCode){case 9:case 27:case 13:case 38:case 37:case 40:case 39:break;default:if(!f.hasClass("selectBox-menuShowing"))i(b);c.preventDefault();clearTimeout(d);e+=String.fromCharCode(c.charCode||c.keyCode);g.find("A").each(function(){if(a(this).text().substr(0,e.length).toLowerCase()===e.toLowerCase()){l(b,a(this).parent());n(b,a(this).parent());return false}});d=setTimeout(function(){e=""},1e3);break}};var q=function(b){b=a(b);b.attr("disabled",false);var c=b.data("selectBox-control");if(!c)return;c.removeClass("selectBox-disabled")};var r=function(b){b=a(b);b.attr("disabled",true);var c=b.data("selectBox-control");if(!c)return;c.addClass("selectBox-disabled")};var s=function(b,c){b=a(b);b.val(c);c=b.val();var d=b.data("selectBox-control");if(!d)return;var e=b.data("selectBox-settings"),f=d.data("selectBox-options");d.find(".selectBox-label").text(a(b).find("OPTION:selected").text()||" ");f.find(".selectBox-selected").removeClass("selectBox-selected");f.find("A").each(function(){if(typeof c==="object"){for(var b=0;b<c.length;b++){if(a(this).attr("rel")==c[b]){a(this).parent().addClass("selectBox-selected")}}}else{if(a(this).attr("rel")==c){a(this).parent().addClass("selectBox-selected")}}});if(e.change)e.change.call(b)};var t=function(b,d){b=a(b);var e=b.data("selectBox-control"),f=b.data("selectBox-settings");switch(typeof c){case"string":b.html(c);break;case"object":b.html("");for(var h in c){if(c[h]===null)continue;if(typeof c[h]==="object"){var i=a('<optgroup label="'+h+'" />');for(var j in c[h]){i.append('<option value="'+j+'">'+c[h][j]+"</option>")}b.append(i)}else{var k=a('<option value="'+h+'">'+c[h]+"</option>");b.append(k)}}break}if(!e)return;e.data("selectBox-options").remove();var l=e.hasClass("selectBox-dropdown")?"dropdown":"inline",d=g(b,l);e.data("selectBox-options",d);switch(l){case"inline":e.append(d);break;case"dropdown":e.find(".selectBox-label").text(a(b).find("OPTION:selected").text()||" ");a("BODY").append(d);break}};var u=function(b){a(b).css("MozUserSelect","none").bind("selectstart",function(a){a.preventDefault()})};switch(b){case"control":return a(this).data("selectBox-control");break;case"settings":if(!c)return a(this).data("selectBox-settings");a(this).each(function(){a(this).data("selectBox-settings",a.extend(true,a(this).data("selectBox-settings"),c))});break;case"options":a(this).each(function(){t(this,c)});break;case"value":if(!c)return a(this).val();a(this).each(function(){s(this,c)});break;case"enable":a(this).each(function(){q(this)});break;case"disable":a(this).each(function(){r(this)});break;case"destroy":a(this).each(function(){h(this)});break;default:a(this).each(function(){f(this,b)});break}return a(this)}})})(jQuery)
