/*
 * Filename: jquery.selfdescribinginput.js
 * Purpose: The purpose of this plugin is to support text inputs that contain a default value that isn't an actual
 * 			default value for the form, but instead a descriptive value for users to see.
 * Author: Dan P. Smith
 * Options (all are optional):
 * 			textClass - string - The classname to be used when the text input is in text entry mode
 * 			text - function - The callback to be called when the text input is in text entry mode
 * 			descriptionClass - string - The classname to be used when the text input is in its descriptive mode
 * 			description - function - The callback to be called when the text input is in descriptive mode
 * 			defaultTextSubmission - boolean - Specifies whether or not the form should be emptied if the default text
 * 				is submitted. 
 */

(function($) {
	$.fn.selfdescribinginput = function(settings) {
		var config = {
			defaultTextSubmission: false
		};
		if (settings) $.extend(config, settings);
		var descriptiveMode = function() {
			if (config.descriptionClass != undefined)
				$(this).addClass(config.descriptionClass);
			if (config.textClass != undefined)
				$(this).removeClass(config.textClass);
			if ($.isFunction(config.description))
				config.description.call(this);
		};
		var textMode = function() {
			if (config.textClass != undefined)
				$(this).addClass(config.textClass);
			if (config.descriptionClass != undefined)
				$(this).removeClass(config.descriptionClass);
			if ($.isFunction(config.text))
				config.text.call(this);
		};
		this.each(function() {
			$(this).data('defaultValue',$(this).val());
			descriptiveMode.call(this);
			if (!config.defaultTextSubmission) {
				var input = $(this);
				$(this).closest('form').submit(function() {
					if (input.val() == input.data('defaultValue'))
						input.val('');
				});
			}
		});
		this.focus(function(event) {
			if ($(this).data('defaultValue') == $(this).val())
				$(this).val('');
			textMode.call(this);
		});
		this.blur(function(event) {
			if ($(this).val() == '') {
				$(this).val($(this).data('defaultValue'));
				descriptiveMode.call(this);
			}
		});
		return this;
	};
})(jQuery);
