(function($) {
	$.fn.extend({
		setFormInstructions: function(className)	{
			var className = className || '';

			this.parents('form').submit(function()	{
				$(this).find('.formInstructionsExtra')
					.prev().show()
					.end().remove();
			});

			return this.each(function() {
				
				// Store the instructions, remove the default title attribute
				$.data(this, 'instruction', $(this).attr('title'));
				$(this).removeAttr('title');
	
				// If the input is of type 'password' then inject a standard input field in afterwards
				if($(this).is('input[type=password]'))	{
					// Clone the password field as a text field, inject afterwards
					// Doing it manually because jQuery doesn't do it for you with .clone().....
					newInput = $('<input type="text" />');
					if($(this).attr('class'))
						newInput.attr('class', $(this).attr('class'));
					newInput.addClass('formInstructionsExtra').insertAfter($(this));
				}

				// Add the blur event and trigger it	
				$(this).blur(function()	{
					// If it's a standard input field
					if($(this).attr('type') != 'password')	{
						// Remove the active class if set
						if(className != '')	$(this).removeClass(className);
						// Set instructions on blur
						if($.trim($(this).val()) == '')	$(this).val($.data(this, 'instruction'));   
					}
					// If it's a password field
					else	{
						// Remove the active class if set
						if(className != '')	$(this).removeClass(className);
						// Set instructions on blur
						if($.trim($(this).val()) == '')	$(this).hide().next().show().val($.data(this, 'instruction'));  
					}
				}).blur();
	
				// Add the focus event
				$(this).focus(function()	{
					// Only if it's a standard input field - password focus events get added to the clone
					if($(this).attr('type') != 'password')	{
						// Add the active class if set
						if(className != "")	$(this).addClass(className);
						// Clear instructions on focus
						if($.trim($(this).val()) == $.data(this, 'instruction'))	$(this).val('');   
					}
					else	{
						// Just add the active class (if set)
						if(className != "")	$(this).addClass(className);
					}
				});
	
				// If it is a password field bind the focus event to the cloned element instead
				if($(this).is('input[type=password]'))	{
					$(this).next().focus(function()	{
						// Add the active class if set
						if(className != "")	$(this).prev().addClass(className);
						$(this).hide().prev().show().focus();
					});
				}
				// Bind an event to the parent form so that on submission you don't get the instructions passed
				else	{
					// Save a reference to this;
					var thisField = this;
					// Bind submit function
					$(this).parents('form').submit(function()	{
						if($.trim($(thisField).val()) == $.data(thisField, 'instruction'))	$(thisField).val('');   
					});
				}
	
			});
		}
	});
})(jQuery);