/*!
 * jquery.droplet.
 *
 * Copyright (c) 2010 Garrett Bjerkhoel
 * http://garrettbjerkhoel.com
 */
(function($){jQuery.fn.extend({scrollTo:function(speed,easing){return this.each(function(){var targetOffset=$(this).offset().top;$('html,body').animate({scrollTop:targetOffset},speed,easing);});}});jQuery.fn.droplet=function(options){return this.each(function(){if(!$.nodeName(this,'select'))return;var defaults={fadeSpeed:150,parseSelectedOffset:true,wrapperClass:'droplet_wrapper',listClass:'droplet_list',placeholderClass:'droplet_placeholder',scrollToSelect:true,scrollSpeed:500},opts=$.extend(defaults,options),$this=$(this);var getItemHeight=function(item,index){var pTop=parseInt(item.css('padding-top')),pBottom=parseInt(item.css('padding-bottom')),mTop=parseInt(item.css('margin-top')),mBottom=parseInt(item.css('margin-bottom')),height=parseInt(item.css('height'));mTop=(isNaN(mTop))?0:mTop;mBottom=(isNaN(mBottom))?0:mBottom;return'-'+Math.round(index*(pTop+pBottom+mTop+mBottom+height))+'px';}
var menuFadeout=function(event){var span=$(event.target).parent().prev(),button=$(event.target).prev();if(!span.hasClass(opts.listClass)&&!button.hasClass(opts.listClass)){dropletList.fadeOut(opts.fadeSpeed);}}
var moveToNextInput=function(event){event.preventDefault();$(this).closest('select,input[type="text"]').focus();return false;}
var selectEvent=function(event){var selected=dropletList.find('.selected'),offset=selected.parent().prevAll().size(),maxOffset=selected.parents('ul:eq(0)').find('li').size();
selected.removeClass('selected');switch(event.keyCode){case 38:offset--;break;case 40:offset++;break;}
if(offset<=0){offset=0;}else if(offset>=maxOffset){offset--;}
dropletList.find('a:eq('+offset+')').addClass('selected');}
var toggleDropletList=function(event){event.preventDefault();if(event.type=='blur'){$(document).unbind('click',menuFadeout);dropletList.fadeOut(opts.fadeSpeed).removeAttr('id');}else{if(dropletList.is(':hidden')){$('#open_droplet').fadeOut(opts.fadeSpeed).removeAttr('id');dropletList.attr('id','open_droplet').fadeIn(opts.fadeSpeed,function(){$(document).bind('click',menuFadeout);});if(opts.scrollToSelect&&event.type=='focus'){dropletList.scrollTo(opts.scrollSpeed);}}else{dropletList.removeAttr('id').fadeOut(opts.fadeSpeed);}}}
var updateHiddenValue=function(event){event.preventDefault();var selected=$(this),openDroplet=$('#open_droplet'),index=selected.parent().prevAll().size();if($('a.selected',openDroplet)!=selected){$('a.selected',openDroplet).removeClass('selected');openDroplet.removeAttr('id');}
selected.addClass('selected');dropletValue.val(selected.attr('href'));dropletPlaceholder.html('<span>'+selected.text()+'</span>');dropletList.fadeOut(opts.fadeSpeed,function(){if(opts.parseSelectedOffset){$(this).css('margin-top',getItemHeight(selected,index));}});selected.parents('form:eq(0)').trigger('change');if($.isFunction(opts.change)){opts.change.call(dropletPlaceholder,{name:selected.text(),value:selected.attr('href')},index);}}
var dropletContainer=$('<div></div>',{'class':opts.wrapperClass+' '+$(this).attr('class')}),dropletList=$('<ul></ul>',{'class':opts.listClass}).hide(),dropletPlaceholder=$('<button></button>',{'class':opts.placeholderClass,html:'<span>'+$(':selected',this).text()+'</span>',click:toggleDropletList,focus:moveToNextInput}),dropletFocusElement=$('<input/>',{type:'text',value:'',id:$(this).attr('name'),focus:toggleDropletList,blur:toggleDropletList}).css({opacity:'0',width:'1px',height:'1px'}),dropletValue=$('<input/>',{type:'hidden',focus:moveToNextInput,value:$(':selected',this).val(),name:$(this).attr('name')});if($.browser.msie){dropletFocusElement.css('width','0px');}
$('option',this).each(function(i){dropletList.append($('<li></li>',{'class':$(this).val().replace(/\s+/g,'_').replace(/[^a-zA-Z0-9_\-]/g,'').toLowerCase()}).html($('<a></a>',{'class':($this.val()===$(this).val())?'selected':'',href:$(this).val(),text:$(this).text(),click:updateHiddenValue})));});$(this).before(dropletContainer.append(dropletList).append(dropletPlaceholder).append(dropletFocusElement).append(dropletValue)).remove();var selectedIndex=$this.attr('selectedIndex');if(selectedIndex!=0&&opts.parseSelectedOffset){dropletList.css('margin-top',getItemHeight(dropletList.find('a:eq('+selectedIndex+')'),selectedIndex))}});}})(jQuery);
