(function($){$('html').addClass('stylish-select');if(!$.browser.msie){Array.prototype.indexOf=function(obj,start){for(var i=(start||0);i<this.length;i++){if(this[i]==obj){return i;}}
return true;}}
$.fn.extend({getSetSSValue:function(value){if(value){$(this).val(value).change();return this;}else{return $(this).find(':selected').val();}},resetSS:function(){var oldOpts=$(this).data('ssOpts');$this=$(this);$this.next().remove();$this.unbind().sSelect(oldOpts);}});$.fn.sSelect=function(options){return this.each(function(){var defaults={defaultText:'Select',animationSpeed:0,ddMaxHeight:'300'};var opts=$.extend(defaults,options),$input=$(this),$containerDivText=$('<div class="selected-option"></div>'),$containerDivOuter=$('<div class="select-outer" tabindex="0" style="width:'+($(this).width())+'px;"></div>'),$containerDivInner=$('<div class="select-inner" style="width:'+($(this).width()-29)+'px;"></div>'),$newUl=$('<ul class="select-list" style="width:'+($(this).width())+'px;"></ul>'),itemIndex=-1,currentIndex=-1,keys=[],prevKey=false,prevented=false,$newLi,cI=0;$(this).data('ssOpts',options);$containerDivOuter.insertAfter($input);$containerDivInner.prependTo($containerDivOuter);$containerDivText.prependTo($containerDivInner);$newUl.appendTo($containerDivOuter);$input.hide();if($input.children('optgroup').length==0){$input.children().each(function(i){var option=$(this).text();var key=$(this).val();keys.push(option.charAt(0).toLowerCase());if($(this).attr('checked')===true){console.log(option);opts.defaultText=option;currentIndex=i;}
$newUl.append($('<li><a href="javascript: void(0);">'+option+'</a></li>').data('key',key));});$newLi=$newUl.children().children();}else{$input.children('optgroup').each(function(){var optionTitle=$(this).attr('label'),$optGroup=$('<li class="select-list-option-title">'+optionTitle+'</li>');$optGroup.appendTo($newUl);var $optGroupList=$('<ul></ul>');$optGroupList.appendTo($optGroup);$(this).children().each(function(){++itemIndex;var option=$(this).text();var key=$(this).val();keys.push(option.charAt(0).toLowerCase());if($(this).prop('selected')===true){opts.defaultText=option;currentIndex=itemIndex;}
$optGroupList.append($('<li><a href="javascript: void(0);">'+option+'</a></li>').data('key',key));})});$newLi=$newUl.find('ul li a');}
var newUlHeight=$newUl.height(),containerHeight=$containerDivOuter.height(),newLiLength=$newLi.length;if(currentIndex!=-1){navigateList(currentIndex,true);}else{$containerDivText.text(opts.defaultText);}
function newUlPos(){var containerPosY=$containerDivOuter.offset().top,newUlPadding=parseInt($newUl.css('padding-top'))+parseInt($newUl.css('padding-bottom')),docHeight=jQuery(window).height(),scrollTop=jQuery(window).scrollTop();if(newUlHeight>parseInt(opts.ddMaxHeight)){newUlHeight=parseInt(opts.ddMaxHeight);}
containerPosY=containerPosY-scrollTop;if(containerPosY+newUlHeight>=docHeight){$newUl.css({top:'-'+(newUlHeight+newUlPadding)+'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(){$containerDivOuter.css('position','relative');}
function positionHideFix(){$containerDivOuter.css('position','static');}
$containerDivOuter.click(function(event){event.stopPropagation();$('.select-list').not($('.select-inner',this).next()).hide().parent().removeClass('select-outer-focus');newUlPos();$('.select-list').css({'z-index':'10'});var cI=Math.max.apply(null,$.map($('.select-outer'),function(e,n){return parseInt($(e).css('z-index'))||10;}));cI+=25;$containerDivOuter.css({'z-index':cI});$newUl.css({'z-index':cI});cI++;$containerDivInner.css({'z-index':cI});$newUl.toggle();positionFix();$newLi.eq(currentIndex).focus();});$newLi.click(function(e){var $clickedLi=$(e.target);currentIndex=$newLi.index($clickedLi);prevented=true;navigateList(currentIndex);$containerDivOuter.css('position','static');});$newLi.hover(function(e){var $hoveredLi=$(e.target);$hoveredLi.addClass('select-list-hover');},function(e){var $hoveredLi=$(e.target);$hoveredLi.removeClass('select-list-hover');});function navigateList(currentIndex,init){$newLi.removeClass('hi-lite').eq(currentIndex).addClass('hi-lite');if($newUl.is(':visible')){$newLi.eq(currentIndex).focus();}
var text=$newLi.eq(currentIndex).text();var val=$newLi.eq(currentIndex).parent().data('key');if(init==true){$input.val(val);$containerDivText.text(text);return false;}
$input.val(val)
$input.change();$containerDivText.text(text);return true;}
$input.bind('change',function(event){$targetInput=$(event.target);$currentOpt=$targetInput.find(':selected');currentIndex=$targetInput.find('option').index($currentOpt);navigateList(currentIndex,true);return true;});function keyPress(element){element.onkeydown=function(e){var keycode;if(e==null){keycode=event.keyCode;}else{keycode=e.which;}
prevented=true;switch(keycode)
{case 40:case 39: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:$newUl.hide();positionHideFix();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 incrementList(){if(currentIndex<(newLiLength-1)){++currentIndex;navigateList(currentIndex);}}
function decrementList(){if(currentIndex>0){--currentIndex;navigateList(currentIndex);}}
function gotoFirst(){currentIndex=0;navigateList(currentIndex);}
function gotoLast(){currentIndex=newLiLength-1;navigateList(currentIndex);}
$containerDivOuter.click(function(){keyPress(this);});$containerDivOuter.hover(function(){$(this).addClass('select-outer-hover');},function(){$(this).removeClass('select-outer-hover');});$containerDivOuter.focus(function(){$(this).addClass('select-outer-focus');keyPress(this);});$containerDivOuter.blur(function(){$(this).removeClass('select-outer-focus');});$('body').click(function(){$containerDivOuter.removeClass('select-outer-focus');$containerDivOuter.removeClass('select-opened');$newUl.hide();positionHideFix();});$containerDivText.hover(function(e){var $hoveredTxt=$(e.target);$hoveredTxt.parent().addClass('select-list-select-hover');},function(e){var $hoveredTxt=$(e.target);$hoveredTxt.parent().removeClass('select-list-select-hover');});$newUl.css('left','0').hide();});};})(jQuery);
