(function($) {

	$.fn.initDefaults = function (settings) {
		
		if (settings) $.extend(options, settings);
		
		this.each(function() {
		
			var $input = null;
			var $form = null;
			
			if ($(this).is(':input.' + options.defaultClass)) {
				$input = $(this);
			} else {
				if ($(this)[0].nodeName == 'FORM') $form = $(this);
				$input = $(this).find(':input.' + options.defaultClass);
			}
			
			$input.each(function () {
			
				$form = $form ? $form : $(this).closest('form');
				var $label = $form.find('label[for=' + $(this).attr('id') + ']');
				var title = $(this).attr('title');
				var defaultVal = null;
				
				if (title)
					defaultVal = title;
				else if ($label.length)
					defaultVal = $label.hide().text();
				
				if (defaultVal) {
				
					// Password fields get a matching text field element to display the default value
					if ($(this).is(':password')) {
					
						// Create the sister field and style it after the password field
						$sister = $('<input value="' + defaultVal + '" />');
						if ($(this).attr('style')) $sister.attr('style', $(this).attr('style'));
						if ($(this).attr('class')) $sister.attr('class', $(this).attr('class'));
						$sister.addClass('blur');
						
						// Insert the sister directly after the password field
						$sister.insertAfter($(this));
						
						// Show the sister if there's no password value
						if (!$(this).val())
							$(this).hide();
						else
							$sister.hide();
						
						
						// Link the two fields together
						$(this).data('sister', $sister);
						$sister.data('sister', $(this));
						
						// Bind events to the fields
						$(this).blur(passBlur);
						$sister.focus(passFocus);
					
					// Text fields just need a simple function to change their style
					} else {
					
						$(this).data('default', defaultVal);
			
						if (!$(this).val() || $(this).val() == defaultVal) {
							$(this).val(defaultVal);
							$(this).addClass(options.blurClass);
						}
						
						$(this).focus(textFocus).blur(textBlur);
										
					}
				}
			});
		});
		
		return this;
	
	};

	var options = {
		blurClass: 'blur',
		defaultClass: 'defaultvalue'
	};
	
	function textFocus () {
		var defaultVal = $(this).data('default');
		$(this).removeClass(options.blurClass)
		if (!$(this).val() || $(this).val() == defaultVal) $(this).val('');
	}
	
	function textBlur () {
		var defaultVal = $(this).data('default');
		if (!$(this).val() || $(this).val() == defaultVal) {
			$(this).addClass(options.blurClass).val(defaultVal);
		}
	}
	
	function passBlur () {
		var $sister = $(this).data('sister');
		
		if (!$(this).val()) {
			$(this).hide();
			$sister.show();
		}
	}
	
	function passFocus () {
		$(this).hide();
		$(this).data('sister').show().focus();
	}
})(jQuery);