/*
 * jQuery taperici plugin
 *
 * version 1.0 (8/20/2007)
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 */

/**
 * The taperici() method moves a text input's label inside the input.
 * This label disappears when the user clicks inside the input.
 * This label reappears when the user leaves the input and no text was written.
 *
 * We assume the input and its label are well-written
 * There is 2 ways to write a form input :
 *	- input inside the label :
 *		<label>
 *			<span>My label</span>
 *			<input type="text" name="myInput" />
 *		</label>
 *	- input outside the label :
 *		<label for="myId">My label</label>
 *		<input type="text" name="myInput" id="myId" />
 *
 * Configuration parameters are :
 * - defaultDisplayClass : (default "defaultDisplay")
 * 		defines the css class for the input when the default value is displayed
 * - defaultValueAttribute : (default "searchDefaultLabel")
 *		the input's attribute containing the default value
 * - inputFocusCallback : [function]
 *		function called when the user enters the input
 * - inputBlurCallback : [function]
 *		function called when the user leaves the input
 * - formSubmitCallback : [function]
 *		function called when the user submits the form
 *
 * @name taperici
 * @type jQuery
 * @return jQuery
 * @author Romain Gonord (romain.gonord.opensource@neteyes.org)
 */
(function($) {
	$.fn.taperici = function(settings) {
		var defaults =  {
			defaultDisplayClass: "defaultDisplay",
			defaultValueAttribute: "searchDefaultLabel",
			textContainer: "span",
			inputFocusCallback: function(){
				var currentElem = $(this);
				currentElem.removeClass(defaults.defaultDisplayClass);
				if (currentElem.attr("value") == currentElem.attr(defaults.defaultValueAttribute)){
					currentElem.attr("value", "");
				}
			},
			inputBlurCallback: function(){
				var currentElem = $(this);
				if (currentElem.attr("value") == null || currentElem.attr("value") == currentElem.attr(defaults.defaultValueAttribute) || currentElem.attr("value") == ""){
					currentElem.addClass(defaults.defaultDisplayClass);
					currentElem.attr("value", currentElem.attr(defaults.defaultValueAttribute));
				}
			},
			formSubmitCallback: function(){
				$("." + defaults.defaultDisplayClass, this).attr("value", "");
			}
		};
		$.extend(defaults, settings);
		return this.each(function(i,n){
			/* variable's initialization */
			var currentElem = $(this);
			var targetInput;
			var text2Display;
			if (currentElem.attr("for") != null){
				targetInput = $("#" + currentElem.attr("for"));
				text2Display = currentElem.html();
				currentElem.hide();
			} else {
				targetInput = $(":input", this);
				var textContainer = $(defaults.textContainer, this);
				text2Display = textContainer.html();
				textContainer.hide();
			}
			/* save default value */
			targetInput.attr(defaults.defaultValueAttribute, text2Display);
			/* input'svalue initialization */
			if (targetInput.attr("value") == null || targetInput.attr("value") == targetInput.attr(defaults.defaultValueAttribute) || targetInput.attr("value") == ""){
				targetInput.attr("value", text2Display);
				targetInput.addClass(defaults.defaultDisplayClass);
			}
			/* event's binding */
			targetInput
				.focus(defaults.inputFocusCallback)
				.blur(defaults.inputBlurCallback)
			;
			$(targetInput[0].form)
				.submit(defaults.formSubmitCallback)
			;
		});
	};
})(jQuery);
