/**
 * Field hint jQuery plugin for text fields and text areas.
 *
 * The field hint plugin allows text fields to be occupied by text that can
 * indicate a specific action when it's not focused or contains user-submitted
 * text.
 *
 * @author Matthew Kingston <matthew.kingston88@gmail.com>
 * @link http://matthewkingston.com.au
 */
(function($) {
	
	
	var settings = {
		inactiveClass: 'inactive',
		activeClass: 'active'
	};
	
	$.fieldhint = {};
	
	$.fieldhint.val = function(element)
	{
		if(element.val != undefined)
		{
			if(element.val() == element.attr('rel'))
			{
				element.val('');
			}
			
			return element.val();
		}
		
		return '';
	}
	
	$.fn.fieldhint = function(options)
	{
		settings = jQuery.extend(settings, options);
		
		return $(this).each(
			function()
			{
				var element = $(this);
				
				// Checks to see if the "rel" attribute has even been set
				// If it hasn't
				if(element.attr('rel') == undefined)
				{
					// Set the "rel" attribute to the value of the "value" field
					element.attr('rel', element.val()).addClass(settings.blurClass);
				}
				// If it has
				else
				{
					// Check to see if the "value" attribute has anything in it.
					// If it hasn't
					if(!element.val())
					{
						// Set the "value" attribute of the field to the value of the "rel" attribute
						element.addClass(settings.inactiveClass);
						element.val(element.attr('rel'));
					}
				}
				
				
				
				// Add an event handler to the the "focus" event of the field
				element.bind('focus', function()
				{
					// Check to see if the "value" attribute is the same as the "rel" attribute
					// If it is...
					if(element.val() == element.attr('rel'))
					{
						// Set the "value" attribute to be empty,
						// Remove the inactive class
						// Add the active class
						element.val('');
						element.removeClass(settings.inactiveClass);
						element.addClass(settings.activeClass);
					}
				});
				
				
				
				// Add an event handler to the "blur" event of the field
				element.bind('blur', function()
				{
					// Check to see if the "value" attribute is empty
					// If it is...
					if(element.val() == '')
					{
						// Set the "value" attribute to be the same value as the "rel" attribute
						// Remove the active class
						// Add the inactive class
						element.val(element.attr('rel'));
						element.removeClass(settings.activeClass);
						element.addClass(settings.inactiveClass);
					}
				});
				
				
				
				element.parents('form').bind('submit', function()
				{
					$.fieldhint.val(element);
				});
			
			}
		);
		
	}
	
})(jQuery);

