/*
jQuery styledDropdown plugin ver 0.91
by Gabe Rankin
2008

Replaces selected <select> dropdowns (depending on the selector you pass jquery)

Requires jQuery 1.2 or higher

*/
// styledDropdown plugin
jQuery.fn.styledDropdown = function(settings) {
	settings = jQuery.extend({
						animation: 'none',			//supported values are 'height', 'opacity', and 'none'
						animationSpeed: 'fast',		//supported values are 'fast', 'slow', or a number
						containerID: '',			//allows user to add an id attribute to the containing div
													//	generated by the plugin
						onChangeSubmit: false,		//classically, submit(); submits the containging form of a
													//	FORM INPUT.  things like <a> and <div> tags do not get to
													//	call submit(); directly, which necessitated the addition 
													//	of this setting
						callBack: false				//Do not pass submit(); to this attribute.
													//	use onChangeSubmit true/false if you want
													//	the dropdown to submit the containing form on change
					}, settings);
	return this.each(
		function()
		{
			var options = jQuery('option', this);
			if(!options.length || options.length == 0)
			{
				return;		//do nothing if there are no options
			}
			var id = jQuery(this).attr('id');						// the id of the select box gets added to the new hidden input
			var name = jQuery(this).attr('name');					// the name of the original select box gets added to the new hidden input so forms submissions go smoothly
			var value = jQuery(this).attr('value');				// store this so that the currently selected value of the <select> gets carried over into the new element
			var callback = (typeof settings.callBack == 'Function') ? settings.callBack : false;
			var changefunc = false;
			
			if(settings.onChangeSubmit)
			{
				var parentForm = jQuery(this).parents('form:eq(0)');	// get the parent form in case we need to submit();
				changefunc = function() { parentForm[0].submit(); };
			}

			var text = jQuery('option:selected', this).text();
			if(!text)
			{
				text = jQuery('option:eq(0)', this).text();
			}
			
			var theHtml = '<div class="styledDropdown"'+(settings.containerID ? ' id="'+settings.containerID+'"' : '')+'><input type="hidden"'+(id ? ' id="'+id+'"' : '')+(name ? ' name="'+name+'"' : '')+' value="'+(value ? value : '')+'" /><div class="styledDropdown-visibleText">'+(text ? text : '')+'</div><a href="#" class="styledDropdown-dropbutton">drop</a><div style="clear:both;"></div><div class="styledDropdown-options">';
			options.each(
				function()
				{
					var val = jQuery(this).attr('value');
					var text = jQuery(this).text();
					theHtml += '<div class="styledDropdown-option"><a href="#" rel="'+(val ? val : '')+'">'+(text ? text : '')+'</a></div>';
				}
			);
			theHtml += '</div></div>';
			jQuery(this).after(theHtml);
			var theDropdownContainer = jQuery(this).next('div');
			jQuery(this).remove();

			var theDropdown = jQuery('.styledDropdown-options', theDropdownContainer);
			var theDropButton = jQuery('.styledDropdown-dropbutton', theDropdownContainer);
			var theInput = jQuery('input', theDropdownContainer);
			var theTextbox = jQuery('.styledDropdown-visibleText', theDropdownContainer);
			var isOver = false;
			
			theDropdownContainer.
				css('position', 'relative').
				hover(
					function()
					{
						if(!theDropdownContainer.hasClass('styledDropdown-over'))
						{
							theDropdownContainer.addClass('styledDropdown-over');
						}
					},
					function()
					{
						if(theDropdownContainer.hasClass('styledDropdown-over'))
						{
							theDropdownContainer.removeClass('styledDropdown-over');
						}
					}
				);
			theDropdown.
				css('position','absolute').
				hide().
				hover(
					function()
					{
						isOver = true;
					},
					function()
					{
						isOver = false;
					}
				);
			theDropButton.
				blur(
					function()
					{
						if(!isOver) theDropdown.hide();
					}
				).
				click(
					function()
					{
						if(theDropdown.filter(':visible').length > 0)
						{
							switch(settings.animation)
							{
								case 'height':
									theDropdown.animate({height:'hide'}, ((settings.animationSpeed == 'fast' || settings.animationSpeed == 'slow')?settings.animationSpeed:'fast'));
								break;
								case 'opacity':
									theDropdown.animate({opacity:'hide'}, ((settings.animationSpeed == 'fast' || settings.animationSpeed == 'slow')?settings.animationSpeed:'fast'));
								break;
								case 'none':
								default:
									theDropdown.hide();
								break;
							}
							theTextbox.removeClass('styledDropdown-active');
						}
						else
						{
							switch(settings.animation)
							{
								case 'height':
									theDropdown.animate({height:'show'}, ((settings.animationSpeed == 'fast' || settings.animationSpeed == 'slow')?settings.animationSpeed:'fast'));
								break;
								case 'opacity':
									theDropdown.animate({opacity:'show'}, ((settings.animationSpeed == 'fast' || settings.animationSpeed == 'slow')?settings.animationSpeed:'fast'));
								break;
								case 'none':
								default:
									theDropdown.show();
								break;
							}
							theTextbox.addClass('styledDropdown-active');
						}
						return false;
					}
				);
			jQuery('.styledDropdown-option a', theDropdown).click(
				function()
				{
					var val = jQuery(this).attr('rel');
					var text = jQuery(this).text();
					theInput.attr('value', val ? val : '');
					theTextbox.text(text);
					theTextbox.removeClass('styledDropdown-active');
					if(changefunc) changefunc();
					if(callback) callback();
					theDropdown.hide();
					return false;
				}
			).blur(
				function()
				{
					if(!isOver) theDropdown.hide();
				}
			);
			
			return;
		}
	);
};