/*
Stylish Select 0.1b - 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
*/
Array.prototype.indexOf=function(b,c){for(var a=(c||0);a<this.length;a++){if(this[a]==b){return a}}};jQuery.fn.sSelect=function(a){return this.each(function(){var j={defaultText:"Please select"};var m=jQuery.extend(j,a);var d=jQuery(this);var k=jQuery('<div class="selectedTxt"></div>');var x=jQuery('<ul class="newList"></ul>');var q=jQuery('<div class="newListSelected" tabindex="0"></div>');var s=-1;var c=-1;var n=[];var u=false;var g="";q.insertAfter(d);k.prependTo(q);x.appendTo(q);d.hide();if(d.children("optgroup").length==0){d.children().each(function(y){var z=jQuery(this).text();n.push(z.charAt(0).toLowerCase());if(jQuery(this).attr("selected")==true){m.defaultText=z;c=y}g+="<li>"+z+"</li>"});x.html(g);var v=x.children()}else{d.children("optgroup").each(function(A){var y=jQuery(this).attr("label");var B=jQuery('<li class="newListOptionTitle">'+y+"</li>");B.appendTo(x);var z=jQuery("<ul></ul>");z.appendTo(B);jQuery(this).children().each(function(){++s;var C=jQuery(this).text();n.push(C.charAt(0).toLowerCase());if(jQuery(this).attr("selected")==true){m.defaultText=C;c=s}g+="<li>"+C+"</li>"});z.html(g)});var v=x.find("ul li")}if(c!=-1){h(c)}else{k.text(m.defaultText)}var w=v.length;function o(){var A=q.offset().top;var B=q.height()+3;var C=$(window).scrollTop();A=A-C;var z=jQuery(window).height();var y=x.height()+3;if(A+y>=z){x.css("top","-"+y+"px")}else{x.css("top",B+"px")}}o();jQuery(window).resize(function(y){o(y)});jQuery(window).scroll(function(y){o(y)});function r(){q.css("position","relative")}function b(){q.css("position","static")}k.click(function(){if(x.is(":visible")){x.hide();b();return false}q.focus();x.slideDown("fast");r();document.onkeydown=function(z){if(z==null){var y=event.keyCode}else{var y=z.which}if(y==13||y==27){x.hide();b();return false}}});q.blur(function(){x.hide();b()});k.hover(function(z){var y=jQuery(z.target);y.addClass("newListSelHover")},function(z){var y=jQuery(z.target);y.removeClass("newListSelHover")});v.hover(function(z){var y=jQuery(z.target);y.addClass("newListHover")},function(z){var y=jQuery(z.target);y.removeClass("newListHover")});v.click(function(z){var y=jQuery(z.target);c=v.index(y);v.removeClass("hiLite");y.addClass("hiLite");var A=y.text();i(A);x.hide();q.css("position","static")});function i(y){d.val(y);k.text(y)}function p(y){y.onkeydown=function(B){if(B==null){var A=event.keyCode}else{var A=B.which}switch(A){case 40:case 39:t();return false;break;case 38:case 37:l();return false;break;case 33:case 36:f();return false;break;case 34:case 35:e();return false;break}keyPressed=String.fromCharCode(A).toLowerCase();var z=n.indexOf(keyPressed);if(typeof z!="undefined"){B.preventDefault();++c;c=n.indexOf(keyPressed,c);if(c==-1||c==null||u!=keyPressed){c=n.indexOf(keyPressed)}h(c);u=keyPressed}}}function t(){if(c<(w-1)){++c;h(c)}}function l(){if(c>0){--c;h(c)}}function f(){c=0;h(c)}function e(y){c=w-1;h(c)}function h(y){v.removeClass("hiLite").eq(y).addClass("hiLite");var z=v.eq(y).text();i(z)}q.focus(function(){p(this)});q.click(function(){p(this)})})};
