/*
Stylish Select 0.3 - jQuery plugin to replace a select drop down box with a stylable unordered list
http://scottdarby.com/

Copyright (c) 2009 Scott Darby

Requires: jQuery 1.3

Licensed under the GPL license:
http://www.gnu.org/licenses/gpl.html

-- -- --
USA Today Update:
	changed newUlPos fn to handle being at the top of a page
*/

(function(jQuery){jQuery('html').addClass('js');Array.prototype.indexOf=function(obj,start){for(var i=(start||0);i<this.length;i++){if(this[i]==obj){return i;}}}
jQuery.fn.extend({getSetSSValue:function(value){if(value){jQuery(this).val(value).change();return this;}else{return selText=jQuery(this).find(':selected').text();}},resetSS:function(){jQuerythis=jQuery(this);jQuerythis.next().remove();jQuerythis.unbind().sSelect();}});jQuery.fn.sSelect=function(options){return this.each(function(){var defaults={defaultText:'Please select',animationSpeed:0,ddMaxHeight:'370px'};var opts=jQuery.extend(defaults,options),jQueryinput=jQuery(this),jQuerycontainerDivText=jQuery('<div class="selectedTxt"></div>'),jQuerycontainerDiv=jQuery('<div class="newListSelected" tabindex="0"></div>'),jQuerynewUl=jQuery('<ul class="newList"></ul>'),itemIndex=-1,currentIndex=-1,keys=[],prevKey=false,newListItems='',prevented=false;jQuerycontainerDiv.insertAfter(jQueryinput);jQuerycontainerDivText.prependTo(jQuerycontainerDiv);jQuerynewUl.appendTo(jQuerycontainerDiv);jQueryinput.hide();if(jQueryinput.children('optgroup').length==0){jQueryinput.children().each(function(i){var option=jQuery(this).text();keys.push(option.charAt(0).toLowerCase());if(jQuery(this).attr('selected')==true){opts.defaultText=option;currentIndex=i;}
newListItems+='<li>'+option+'</li>';});jQuerynewUl.html(newListItems);newListItems='';var jQuerynewLi=jQuerynewUl.children();}else{jQueryinput.children('optgroup').each(function(i){var optionTitle=jQuery(this).attr('label'),jQueryoptGroup=jQuery('<li class="newListOptionTitle">'+optionTitle+'</li>');jQueryoptGroup.appendTo(jQuerynewUl);var jQueryoptGroupList=jQuery('<ul></ul>');jQueryoptGroupList.appendTo(jQueryoptGroup);jQuery(this).children().each(function(){++itemIndex;var option=jQuery(this).text();keys.push(option.charAt(0).toLowerCase());if(jQuery(this).attr('selected')==true){opts.defaultText=option;currentIndex=itemIndex;}
newListItems+='<li>'+option+'</li>';})
jQueryoptGroupList.html(newListItems);newListItems='';});var jQuerynewLi=jQuerynewUl.find('ul li');}
var newUlHeight=jQuerynewUl.height()+3,containerHeight=jQuerycontainerDiv.height()+3,newLiLength=jQuerynewLi.length;if(currentIndex!=-1){navigateList(currentIndex,true);}else{jQuerycontainerDivText.text(opts.defaultText);}
function newUlPos(){var containerPosY=jQuerycontainerDiv.offset().top,docHeight=jQuery(window).height(),scrollTop=jQuery(window).scrollTop();if(newUlHeight>parseInt(opts.ddMaxHeight)){newUlHeight=parseInt(opts.ddMaxHeight);}
containerPosY=containerPosY-scrollTop;if(containerPosY+newUlHeight>=docHeight){if((newUlHeight*-1)<0){jQuerynewUl.css({top:containerHeight+'px',height:(docHeight-containerHeight*2)});}
else{jQuerynewUl.css({top:'-'+newUlHeight+'px',height:newUlHeight});}
jQueryinput.onTop=true;}else{jQuerynewUl.css({top:containerHeight+'px',height:newUlHeight});jQueryinput.onTop=false;}}
newUlPos();jQuery(window).resize(function(){newUlPos();});jQuery(window).scroll(function(){newUlPos();});function positionFix(){jQuerycontainerDiv.css('position','relative');}
function positionHideFix(){jQuerycontainerDiv.css('position','static');}
jQuerycontainerDivText.click(function(){if(jQuerynewUl.is(':visible')){jQuerynewUl.hide();positionHideFix()
return false;}
jQuerycontainerDiv.focus();jQuerynewUl.slideDown(opts.animationSpeed);positionFix();jQuerynewUl.scrollTop(jQueryinput.liOffsetTop);});jQuerynewLi.hover(function(e){var jQueryhoveredLi=jQuery(e.target);jQueryhoveredLi.addClass('newListHover');},function(e){var jQueryhoveredLi=jQuery(e.target);jQueryhoveredLi.removeClass('newListHover');});jQuerynewLi.click(function(e){var jQueryclickedLi=jQuery(e.target);currentIndex=jQuerynewLi.index(jQueryclickedLi);prevented=true;navigateList(currentIndex);jQuerynewUl.hide();jQuerycontainerDiv.css('position','static');});function navigateList(currentIndex,init){var containerOffsetTop=jQuerycontainerDiv.offset().top,liOffsetTop=jQuerynewLi.eq(currentIndex).offset().top,ulScrollTop=jQuerynewUl.scrollTop();if(jQueryinput.onTop==true){jQueryinput.liOffsetTop=(((liOffsetTop-containerOffsetTop)-containerHeight)+ulScrollTop)+parseInt(opts.ddMaxHeight);}else{jQueryinput.liOffsetTop=((liOffsetTop-containerOffsetTop)-containerHeight)+ulScrollTop;}
jQuerynewUl.scrollTop(jQueryinput.liOffsetTop);jQuerynewLi.removeClass('hiLite').eq(currentIndex).addClass('hiLite');var text=jQuerynewLi.eq(currentIndex).text();if(init==true){jQueryinput.val(text);jQuerycontainerDivText.text(text);return false;}
jQueryinput.val(text).change();jQuerycontainerDivText.text(text);};jQueryinput.change(function(event){jQuerytargetInput=jQuery(event.target);if(prevented==true){prevented=false;return false;}
jQuerycurrentOpt=jQuerytargetInput.find(':selected');currentIndex=jQuerytargetInput.find('option').index(jQuerycurrentOpt);navigateList(currentIndex,true);});function keyPress(element){element.onkeydown=function(e){if(e==null){var keycode=event.keyCode;}else{var 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:jQuerynewUl.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);}
jQuerycontainerDiv.click(function(){keyPress(this);});jQuerycontainerDiv.focus(function(){jQuery(this).addClass('newListSelFocus');keyPress(this);});jQuerycontainerDiv.blur(function(){jQuery(this).removeClass('newListSelFocus');jQuerynewUl.hide();positionHideFix();});jQuerycontainerDivText.hover(function(e){var jQueryhoveredTxt=jQuery(e.target);jQueryhoveredTxt.parent().addClass('newListSelHover');},function(e){var jQueryhoveredTxt=jQuery(e.target);jQueryhoveredTxt.parent().removeClass('newListSelHover');});jQuerynewUl.css('left','0').hide();});};})(jQuery);