/*
	jQuery plugin inputFieldHelper by Jakob Givoni
	
	PURPOSE:
	- Helps the user easily identify input fields and what to type in them.
	- Visual hint: Prefills your input or textarea field with the value of your title attribute (the tooltip becomes the initial value)
	- Removes the hint on focus
	- Reenters the hint on blur of field is left empty
	- Styling: Adds a class when the field contains the hint. Use it to i.e. style the text dimmed, lightening the color
	- Hint not submitted: Removes the name attribute while the hint is displayed so that the hint won't be submitted if case of form submission
	
	USAGE:
	jQuery('input').inputFieldHelper(options); // options is an optional object
	- optional options: 
		- dimmedClass: the class name to add when the field is showing the hint

	TEST PAGE:
	- http://testsite.devsite-1.com/jg/templates/main/inputFieldHelper.html
*/

jQuery.fn.inputFieldHelper = function(options) {

	settings = jQuery.extend({
		dimmedClass: 'dimmed'
	}, options);
	
	this.each(function(){
		var orig_name = jQuery(this).attr('name');
		// Copying the title value to input value
		jQuery(this).val(jQuery(this).attr('title')).addClass(settings.dimmedClass).removeAttr('name');
		
		// On focus, remove the default text
		jQuery(this).focus(function(){
			if (jQuery(this).val() == jQuery(this).attr('title')) {
				jQuery(this).val('').removeAttr('readonly').removeClass(settings.dimmedClass).attr('name', orig_name);
			}
		});
		
		// On blur, add default text if field is empty
		jQuery(this).blur(function(){
			if (jQuery(this).val() == '') {
				jQuery(this).val(jQuery(this).attr('title')).addClass(settings.dimmedClass).removeAttr('name');
			}
		});
	});
	
	return this;
}