// styledDropdown plugin
jQuery.fn.styledDropdown = function(settings) {
	settings = jQuery.extend({
						animation: 'none',
						animationSpeed: 'fast',
						containerID: '',
						onChangeSubmit: false
					}, settings);
	return this.each(
		function()
		{
			var options = jQuery('option', this);
			if(!options.length || options.length == 0)
			{
				return;
			}
			var id = jQuery(this).attr('id');
			var name = jQuery(this).attr('name');
			var value = jQuery(this).attr('value');
			var parentForm = jQuery(this).parents('form:eq(0)');
			var changefunc = function() { }
			if(settings.onChangeSubmit && parentForm.length > 0)
			{
				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">';
			if (jQuery(this).children('optgroup').length == 0)
			{
				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>';
					}
				);
			}
			else
			{
				jQuery(this).children('optgroup').each(
					function()
					{
						var optionTitle = jQuery(this).attr('label');
						theHtml += '<div class="styledDropdown-optgroup">'+optionTitle+'</div>';
						
						jQuery('option', this).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');
			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');
					changefunc();
					theDropdown.hide();
					return false;
				}
			).blur(
				function()
				{
					if(!isOver)
					{
						theDropdown.hide();
						theTextbox.removeClass('styledDropdown-active');
					}
				}
			);
			return;
		}
	);
};