/**
 * jQuery Plug-in - Compatible with jQuery-1.2.6.js
 *
 * Uses the input's label as initial value - i.e. a Watermark feature
 * 
 * Removes the value label on focus and replaces the label on blur if nothing was written in the field.
 *
 * SUPPORTS password fields by cloning them with a text input!
 *
 * @author David Lindkvist (Fi)
 * @version 1.0 
 * @version 1.1 JSLint verified 
 * @version 1.2 Added support for explicit labels. 
 * 
 * 
 * Example markup:
 * 
 * <form id="myForm">
 *
 *			<label for="usrinput">Username</label>
 *			<input type="text" id="usrinput" name="usrinput" />
 *
 *			<label for="pwdinput">Password</label>
 *			<input type="password" id="pwdinput" name="pwdinput" />
 *
 *	</form>
 * 
 * Usage: $('#myForm input').labelInput();
 * 
 * This will take the label text "Username" and place it inside the input field as an interactive label.
 * 
 * @TODO Add support for implicit labels
 * 
 */
(function ($) {
	
	//
	// plugin definition
	//
	$.fn.labelInput = function (pluginOptions) {
	  
		// build main options before element iteration
		var opts = $.extend({}, $.fn.labelInput.defaults, pluginOptions);

		// iterate and add each select element as a slider handle
		this.each(function (i) {
			
			var input = $(this);
				
			// find label for this input and hide it
			var label = $('label[for='+input.attr('id')+']').hide();

			// If input value is empty
			if (input.val() === "") {
				// set label text as input value
				input.val(label.text());
				
				input.addClass(opts.watermarkClass);
			}
			
			var pwd = false;
			var pwdClone = null;
			
			if (input.attr("type") === 'password') {
				
				pwd = true;
				
				//clone original input (cannot use actual clone since we can't modify type without IE throwing exception)
				pwdClone = $('<input type="text">');
				pwdClone.attr("class", input.attr("class"));
				pwdClone.attr("value", input.attr("value"));
				
				//hide original and show clone instea
				//input.hide(); not working on hidden elements in Webkit-based browsers, use css() instead.
				input.css({display: 'none'});
				input.before(pwdClone);		
			}
			
			// if password input, add focus listener to cloned text input
			if (pwd) {
				
				pwdClone.focus(function () {
					
					//hide clone and show real input
					input.focus();
				});
				
				/* listen for change event on original input */
				input.change(function () {
					//update classes
					pwdClone.attr("class", input.attr("class"));
				});
		
				/* listen for programmatic focus event */
				input.focus(function () {
					pwdClone.hide();
					input.show();
				});
			}
			
			//add listeners select element
			input.focus(function () {
				
				input.addClass(opts.focusClass).removeClass(opts.watermarkClass);
				
				if (input.val() === label.text()) {
					input.val("");
				}
				
			}).blur(function () {
				
				input.removeClass(opts.focusClass);

				//check to see if label should be displayed again
				if (jQuery.trim(input.val()).length === 0) {
					
					//if password input, show fake text input again
					if (pwd) {
						input.hide();
						input.val("");
						pwdClone.attr("class", input.attr("class")); /* update classes incase input has changed */
						pwdClone.show();
					}
					else {
						input.val(label.text());
						input.addClass(opts.watermarkClass);
					}
				}
					
			});
			
		});
	
	}; //end plugin def
		
	
	//
	// expose plugin defaults
	//
	$.fn.labelInput.defaults = {
		watermarkClass: 'watermark',	
		focusClass: 'focus'	
	};
	
})(jQuery);
