(function($){$("html").addClass("js");$.fn.extend({getSetSSValue:function(value){if(value){$(this).val(value).change();return this}else return selText=$(this).find(":selected").text()},resetSS:function(options){$this=$(this);$this.next().remove();$this.unbind().sSelect($.extend($this.get(0).ssOptions,options))}});$.fn.sSelect=function(options){return this.each(function(){this.ssOptions=options;var defaults={defaultText:"Select one...",animationSpeed:0,ddMaxHeight:"",selectOne:false,forceUp:false,change:null},opts=$.extend(defaults,options),$input=$(this),$containerDivText=$('<div class="selectedTxt"></div>'),$containerDiv=$('<div class="newListSelected" tabindex="0"></div>'),$newUl=$('<ul class="newList"></ul>'),itemIndex=-1,currentIndex=-1,keys=[],prevKey=false,newListItems="",prevented=false,ignoreFirstItem=false,disabled=this.disabled;keys.indexOf=function(obj,start){for(var i=start||0;i<this.length;i++)if(this[i]==obj)return i};$containerDiv.insertAfter($input);$containerDivText.prependTo($containerDiv);$newUl.appendTo($containerDiv);$input.hide();if($input.children("optgroup").length==0){$input.children().each(function(i){var option=$(this).text();if(opts.selectOne&&i==0&&option=="")ignoreFirstItem=true;if(ignoreFirstItem&&i==0)keys.push("\n");else keys.push(option.charAt(0).toLowerCase());if($(this).attr("selected")==true){opts.defaultText=option;currentIndex=i}if(ignoreFirstItem&&i==0)newListItems+='<li style="display:none"></li>';else{if(option=="")option="&nbsp;";newListItems+="<li>"+option+"</li>"}});$newUl.html(newListItems);newListItems="";var $newLi=$newUl.children();$newLi.each(function(i,v){v.optValue=$input[0].options[i].value})}else{$input.children("optgroup").each(function(i){var optionTitle=$(this).attr("label"),$optGroup=$('<li class="newListOptionTitle">'+optionTitle+"</li>");$optGroup.appendTo($newUl);var $optGroupList=$("<ul></ul>");$optGroupList.appendTo($optGroup);$(this).children().each(function(){++itemIndex;var option=$(this).text();keys.push(option.charAt(0).toLowerCase());if($(this).attr("selected")==true){opts.defaultText=option;currentIndex=itemIndex}newListItems+="<li>"+option+"</li>"});$optGroupList.html(newListItems);newListItems=""});var $newLi=$newUl.find("ul li")}var newUlHeight=$newUl.height()+3,containerHeight=$containerDiv.height()+3,newLiLength=$newLi.length;if(currentIndex!=-1)navigateList(currentIndex,true);else $containerDivText.text(opts.defaultText).addClass("newListEmpty");disabled&&$containerDiv.addClass("newListDisabled");function newUlPos(){var containerPosY=$containerDiv.offset().top,containerHeight=$containerDiv.height()+3,docHeight=jQuery(window).height(),scrollTop=jQuery(window).scrollTop();if(newUlHeight>parseInt(opts.ddMaxHeight))newUlHeight=parseInt(opts.ddMaxHeight);containerPosY=containerPosY-scrollTop;if(containerPosY+newUlHeight>=docHeight||opts.forceUp){$newUl.css({top:"-"+newUlHeight+"px",height:newUlHeight});$input.onTop=true}else{$newUl.css({top:containerHeight+"px",height:newUlHeight});$input.onTop=false}}newUlPos();$(window).resize(function(){newUlPos()});$(window).scroll(function(){newUlPos()});function positionFix(){$containerDiv.css("position","relative")}function positionHideFix(){document.all&&$.browser.version<7&&$containerDiv.css("position","static")}$containerDivText.click(function(){if(disabled)return;if($newUl.is(":visible")){$containerDiv.blur();return false}else{$containerDiv.focus();$newUl.slideDown(opts.animationSpeed);positionFix();$newUl.scrollTop($input.liOffsetTop)}});$newLi.hover(function(e){var $hoveredLi=$(e.target);$hoveredLi.addClass("newListHover")},function(e){var $hoveredLi=$(e.target);$hoveredLi.removeClass("newListHover")});$newLi.click(function(e){var $clickedLi=$(e.target);currentIndex=$newLi.index($clickedLi);prevented=true;navigateList(currentIndex);$containerDiv.blur()});function navigateList(currentIndex,init){var containerOffsetTop=$containerDiv.offset().top,liOffsetTop=$newLi.eq(currentIndex).offset().top,ulScrollTop=$newUl.scrollTop();if($input.onTop==true)$input.liOffsetTop=liOffsetTop-containerOffsetTop-containerHeight+ulScrollTop+parseInt(opts.ddMaxHeight);else $input.liOffsetTop=liOffsetTop-containerOffsetTop-containerHeight+ulScrollTop;$newUl.scrollTop($input.liOffsetTop);$newLi.removeClass("hiLite").eq(currentIndex).addClass("hiLite");var text=$newLi.eq(currentIndex).text(),val=$newLi.eq(currentIndex)[0].optValue,newText=$newLi.eq(currentIndex).html();if(init==true)$input.val(val);else $input.val(val).change();if(ignoreFirstItem&&currentIndex==0)$containerDivText.text("Select one...").addClass("newListEmpty");else $containerDivText.text(text).html(newText).removeClass("newListEmpty");opts.change&&opts.change($input);if(init==true)return false}$input.change(function(event){$targetInput=$(event.target);if(prevented==true){prevented=false;return false}$currentOpt=$targetInput.find(":selected");currentIndex=$targetInput.find("option").index($currentOpt);navigateList(currentIndex,true)});this.updateDisabled=function(){disabled=this.disabled;if(disabled){$containerDiv.addClass("newListDisabled");$containerDivText.css("cursor","default")}else{$containerDiv.removeClass("newListDisabled");$containerDivText.css("cursor","pointer")}};function keyPress(element){if(disabled)return;element.onkeydown=function(e){if(e==null)var keycode=event.keyCode;else var keycode=e.which;prevented=true;switch(keycode){case 32:$newUl.slideDown(opts.animationSpeed);positionFix();$newUl.scrollTop($input.liOffsetTop);return false;break;case 40:case 39:if(isAltKeyPressed(e)){$newUl.slideDown(opts.animationSpeed);positionFix();$newUl.scrollTop($input.liOffsetTop);return false;break}else{incrementList();return false;break}case 38:case 37:decrementList();return false;break;case 33:case 36:gotoFirst();return false;break;case 34:case 35:gotoLast();return false;break;case 13:case 27:$containerDiv.blur();return false;break}keyPressed=String.fromCharCode(keycode).toLowerCase();var currentKeyIndex=keys.indexOf(keyPressed);if(typeof currentKeyIndex!="undefined"){++currentIndex;currentIndex=keys.indexOf(keyPressed,currentIndex);if(currentIndex==-1||currentIndex==null||prevKey!=keyPressed)currentIndex=keys.indexOf(keyPressed);navigateList(currentIndex);prevKey=keyPressed;return false}}}function isAltKeyPressed(e){try{if(typeof e=="undefined")return window.event.altKey;else return e.altKey}catch(ex){return false}}function incrementList(){if(currentIndex<newLiLength-1){++currentIndex;navigateList(currentIndex)}}function decrementList(){if(currentIndex>0){--currentIndex;navigateList(currentIndex)}}function gotoFirst(){currentIndex=ignoreFirstItem&&newLiLength>1?1:0;navigateList(currentIndex)}function gotoLast(){currentIndex=newLiLength-1;navigateList(currentIndex)}$containerDiv.click(function(){if(disabled)return;keyPress(this)});$containerDiv.focus(function(){if(disabled)return;$(this).addClass("newListSelFocus");$(this).css("zIndex","1");keyPress(this)});$containerDiv.blur(function(){$(this).removeClass("newListSelFocus");$newUl.fadeOut("fast");positionHideFix();$("div.newListSelected").css("zIndex","0")});$containerDivText.hover(function(e){if(disabled)return;var $hoveredTxt=$(e.target);$hoveredTxt.parent().addClass("newListSelHover")},function(e){var $hoveredTxt=$(e.target);$hoveredTxt.parent().removeClass("newListSelHover")});$newUl.css("left","0").hide()})}})(jQuery)