if(jQuery)(function($){$.extend($.fn,{selectBox:function(o,data){var _show=function(event){var select=event.data.select;var control=event.data.control;if($(control).hasClass('selectBox-disabled'))return false;if($(control).hasClass('selectBox-focus')&&$("#selectBox-dropdown").size()===1){_hide(event,true);return false}$('.selectBox').not(control).trigger('blur');_focus(event);event.stopPropagation();$("#selectBox-dropdown").remove();var dropdown=$('
');var options=$('');if($(select).children('optgroup').size()===0){$(select).children('option').each(function(){var text=$(this).text()!==''?$(this).text():'\u00A0';var extraClasses='';if($(this).attr('disabled'))extraClasses+=' selectBox-disabled';$(options).append('')})}else{$(dropdown).addClass('selectBox-hasOptgroups');$(select).children('optgroup').each(function(){$(options).append(''+_htmlspecialchars($(this).attr('label'))+'');$(this).children('option').each(function(){var text=$(this).text()!==''?$(this).text():'\u00A0';var extraClasses='';if($(this).attr('disabled'))extraClasses+=' selectBox-disabled';$(options).append('')})})}$(dropdown).append(options);var selectedIndex=$(select)[0].selectedIndex;$(dropdown).find('LI.selectBox-option').eq(selectedIndex).addClass('selectBox-initial selectBox-current');$(dropdown).find('LI.selectBox-option').hover(function(){$(dropdown).find('.selectBox-current').removeClass('selectBox-current');$(this).addClass('selectBox-current')},function(){$(this).removeClass('selectBox-current')}).click({select:select,control:control},function(event){_select(event)}).mouseup({select:select,control:control},function(event){$(event.target).trigger('click')});$('BODY').append(dropdown);var cPos=$(control).offset();var cHeight=$(control).outerHeight();var cWidth=$(control).outerWidth();var borderAdjustment=parseInt($(dropdown).css('borderLeftWidth'))+parseInt($(dropdown).css('borderRightWidth'));$(dropdown).css({position:'absolute',zIndex:'999999',top:cPos.top+cHeight,left:cPos.left,width:cWidth-borderAdjustment});switch($(select).data('selectBox-options').transition){case'fade':$(dropdown).fadeIn('fast');break;case'slide':$(dropdown).slideDown('fast');break;default:$(dropdown).show();break}$(control).removeClass('ui-corner-all').addClass('ui-corner-top');_disableSelection(dropdown);_dropdownScrollFix(true)};var _hide=function(event,preventBlur){var select=event.data.select;var control=event.data.control;var dropdown=$("#selectBox-dropdown");switch($(select).data('selectBox-options').transition){case'fade':$(dropdown).fadeOut('fast',function(){$(this).remove()});break;case'slide':$(dropdown).slideUp('fast',function(){$(this).remove()});break;default:$(dropdown).remove();break}$(control).removeClass('ui-corner-top').addClass('ui-corner-all');if(!preventBlur){_blur(event)}else{$(control).focus()}};var _select=function(event,option){var select=event.data.select;var control=event.data.control;option=option?option:event.target;if($(option).hasClass('selectBox-disabled'))return false;var oldSelectedIndex=$(select)[0].selectedIndex;$('#selectBox-dropdown .selectBox-optgroup').remove();var newSelectedIndex=$('#selectBox-dropdown').find('LI.selectBox-current').index();if(oldSelectedIndex!==newSelectedIndex){$(select)[0].selectedIndex=newSelectedIndex;$(control).find('.selectBox-label').text($(option).text());$(select).trigger('change')}_hide(event,true)};var _focus=function(event){var select=event.data.select;var control=event.data.control;if($(control).hasClass('selectBox-disabled'))return true;if($(control).hasClass('selectBox-focus'))return false;$(".selectBox.selectBox-focus").removeClass("selectBox-focus");$("#selectBox-dropdown").remove();$(control).addClass('selectBox-focus');$(document).bind('mousedown',{select:select,control:control},_blur);$(document).bind('keydown',{select:select,control:control},_key);$(select).trigger('focus');$(control).focus()};var _blur=function(event){var select=event.data.select;var control=event.data.control;if(event.target.id==='selectBox-dropdown'||$(event.target).parents('#selectBox-dropdown').size()===1){$(control).trigger('focus');return false}if($(control).hasClass('selectBox-focus')){$(control).removeClass('selectBox-focus');$(document).unbind('mousedown',_blur);$(document).unbind('keydown',_key);$(select).trigger('blur');_hide(event)}};var _key=function(event){var select=event.data.select;var control=event.data.control;var dropdown=$("#selectBox-dropdown");if($(control).hasClass('selectBox-disabled'))return false;switch(event.keyCode){case 9:_blur(event);break;case 13:if($(dropdown).size()===0)return false;var siblings=$(dropdown).find('.selectBox-option');var currentIndex=-1;$.each(siblings,function(index,option){if($(option).hasClass('selectBox-current')){currentIndex=index;return}});if(currentIndex>=0){_select(event,$(siblings).eq(currentIndex))}return false;break;case 27:_hide(event,true);break;case 38:case 37:case 33:var interval=event.keyCode===33?20:1;if($(dropdown).size()===0){if(event.altKey){_show(event);return false}var totalIndexes=$(select).find('OPTION').size(),oldSelectedIndex=$(select)[0].selectedIndex,newSelectedIndex=$(select)[0].selectedIndex-interval;while($(select).find('OPTION').eq(newSelectedIndex).attr('disabled')===true&&newSelectedIndex>=0){newSelectedIndex--}if(newSelectedIndex<0){newSelectedIndex=$(select).find('OPTION:not([disabled]):first').index()}$(select)[0].selectedIndex=newSelectedIndex;if($(select)[0].selectedIndex===-1){newSelectedIndex=0;$(select)[0].selectedIndex=newSelectedIndex}var label=$(select).find('OPTION:selected').text();if(label==='')label='\u00A0';$(control).find('.selectBox-label').text(label);if(newSelectedIndex!==oldSelectedIndex)$(select).trigger('change');return false}var siblings=$(dropdown).find('.selectBox-option');var currentIndex=-1;$.each(siblings,function(index,option){if($(option).hasClass('selectBox-current')){currentIndex=index;return}});currentIndex=currentIndex-interval;if(currentIndex<0)currentIndex=0;$(siblings).removeClass('selectBox-current');$(siblings).eq(currentIndex).addClass('selectBox-current');_dropdownScrollFix();return false;break;case 40:case 39:case 34:var interval=event.keyCode===34?20:1;if($(dropdown).size()===0){if(event.altKey){_show(event);return false}var totalIndexes=$(select).find('OPTION').size(),oldSelectedIndex=$(select)[0].selectedIndex,newSelectedIndex=$(select)[0].selectedIndex+interval;while($(select).find('OPTION').eq(newSelectedIndex).attr('disabled')===true&&newSelectedIndex<=$(select).find('OPTION').size()){newSelectedIndex++}if(newSelectedIndex>totalIndexes-1){newSelectedIndex=$(select).find('OPTION:not([disabled]):last').index()}$(select)[0].selectedIndex=newSelectedIndex;if($(select)[0].selectedIndex===-1){newSelectedIndex=$(select).find('OPTION').size()-1;$(select)[0].selectedIndex=newSelectedIndex}var label=$(select).find('OPTION:selected').text();if(label==='')label='\u00A0';$(control).find('.selectBox-label').text(label);if(newSelectedIndex!=oldSelectedIndex)$(select).trigger('change');return false}var siblings=$(dropdown).find('.selectBox-option');var currentIndex=-1;$.each(siblings,function(index,option){if($(option).hasClass('selectBox-current')){currentIndex=index;return}});currentIndex=currentIndex+interval;if(currentIndex>$(siblings).size()-1)currentIndex=$(siblings).size()-1;$(siblings).removeClass('selectBox-current');$(siblings).eq(currentIndex).addClass('selectBox-current');_dropdownScrollFix();return false;break;case 36:case 35:if($(dropdown).size()===0){if(event.altKey){_show(event);return false}var oldSelectedIndex=$(select)[0].selectedIndex,newSelectedIndex;if(event.keyCode===36){newSelectedIndex=0}else{newSelectedIndex=$(select).find('OPTION').size()-1}if($(select).find('OPTION').eq(newSelectedIndex).attr('disabled')===true){if(event.keyCode===36){newSelectedIndex=$(select).find('OPTION:not([disabled]):first').index()}else{newSelectedIndex=$(select).find('OPTION:not([disabled]):last').index()}}$(select)[0].selectedIndex=newSelectedIndex;var label=$(select).find('OPTION:selected').text();if(label==='')label='\u00A0';$(control).find('.selectBox-label').text(label);if(newSelectedIndex!=oldSelectedIndex)$(select).trigger('change');return false}$(dropdown).find('.selectBox-current').removeClass('selectBox-current');if(event.keyCode===36){$(dropdown).find('.selectBox-option:first').addClass('selectBox-current')}else{$(dropdown).find('.selectBox-option:last').addClass('selectBox-current')}_dropdownScrollFix();return false;break}};var _dropdownScrollFix=function(centerSelection){var dropdown=$("#selectBox-dropdown");if($(dropdown).size()===0)return false;var target=$(dropdown).find('.selectBox-current');if($(target).size()===0)return false;var targetTop=parseInt($(target).offset().top-$(dropdown).position().top);var targetBottom=parseInt(targetTop+$(target).outerHeight());if(centerSelection){$(dropdown).scrollTop($(target).offset().top-$(dropdown).offset().top+$(dropdown).scrollTop()-($(dropdown).height()/2))}else{if(targetTop<0){$(dropdown).scrollTop($(target).offset().top-$(dropdown).offset().top+$(dropdown).scrollTop())}if(targetBottom>$(dropdown).height()){$(dropdown).scrollTop(($(target).offset().top+$(target).outerHeight())-$(dropdown).offset().top+$(dropdown).scrollTop()-$(dropdown).height())}}};var _disableSelection=function(selector){$(selector).css('MozUserSelect','none').bind('selectstart',function(){return false}).bind('mousedown',function(){return false});return true};var _htmlspecialchars=function(string){return(string.replace(/&/g,'&').replace(//g,'>').replace(/"/g,'"').replace(/'/g,'''))};switch(o){case'destroy':$(this).each(function(){var select=$(this);var control=$(this).next('.selectBox');if($(select)[0].tagName.toLowerCase()==='select'){$(control).remove();$(select).removeData('selectBox-options').show()}});return $(this);break;case'disable':$(this).each(function(){var select=$(this);var control=$(this).next('.selectBox');$(select).attr('disabled',true);$(control).addClass('selectBox-disabled')});return $(this);break;case'enable':$(this).each(function(){var select=$(this);var control=$(this).next('.selectBox');$(select).attr('disabled',false);$(control).removeClass('selectBox-disabled')});return $(this);break;case'setOptions':if(!data)return $(this);$(this).each(function(){var select=$(this);var control=$(this).next('.selectBox');switch(typeof(data)){case'string':$(select).html(data);break;case'object':$(select).html('');for(var i in data){if(data[i]===null)continue;if(typeof(data[i])==='object'){var optgroup=$('');for(var j in data[i]){$(optgroup).append('')}$(select).append(optgroup)}else{var option=$('');$(select).append(option)}}break}var options=$(select).data('selectBox-options');$(select).selectBox('destroy');$(select).selectBox(options)});return $(this);break;case'value':$("#selectBox-dropdown").remove();$(this).each(function(){var select=$(this);var control=$(this).next('.selectBox');$(select).val(data);var label=$(select).find(':selected').text();if(label==='')label='\u00A0';$(control).removeClass('ui-corner-top').addClass('ui-corner-all').find('.selectBox-label').text(label)});return $(this);break;default:$(this).each(function(){if(!o)o={};var options=$.extend({autoWidth:true},o);var select=$(this);if($(this).next('.selectBox').size()===0){var control=$('');$(control).addClass($(select).attr('class')).attr({style:($(select).attr('style')+'').replace(/inline/,'inline-block'),title:$(select).attr('title')});$(select).data('selectBox-options',options);if(options.autoWidth){var longestOption='';$(select).find('OPTION').each(function(){if($(this).text().length>longestOption.length)longestOption=$(this).text()});var div=$('');var li=$(''+_htmlspecialchars(longestOption)+'');$(div).append(li);$('BODY').append(div);$(control).width(li.outerWidth());$(div).remove()}if($(select)[0].tagName.toLowerCase()!=='select'||$(select).attr('multiple')===true)return;if($(select).attr('disabled')===true)$(control).addClass('selectBox-disabled');var label=$(select).find('OPTION:selected').text();if(label==='')label='\u00A0';$(control).append(''+_htmlspecialchars(label)+'');$(control).append('');$(select).hide().after(control);_disableSelection(control);$(control).bind('click',function(){return false}).bind('mousedown',{select:select,control:control},_show).bind('focus',{select:select,control:control},_focus).bind('blur',{select:select,control:control},_blur)}});return $(this);break}}})})(jQuery);