/**
 * jQuery Initial input value replacer
 * 
 * Sets input value attribute to a starting value  
 * @author Marco "DWJ" Solazzi - hello@dwightjack.com
 * @license  Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * @copyright Copyright (c) 2008 Marco Solazzi
 * @version 0.1
 * @requires jQuery 1.2.x
 */
(function (jQuery) {
	/**
	 * Setting input initialization
	 *  
	 * @param {String|Object|Bool} text Initial value of the field. Can be either a string, a jQuery reference (example: $("#element")), or boolean false (default) to search for related label
	 * @param {Object} [opts] An object containing options: 
	 * 							color (initial text color, default : "#666"), 
	 * 							e (event which triggers initial text clearing, default: "focus"), 
	 * 							force (execute this script even if input value is not empty, default: false)
	 * 							keep (if value of field is empty on blur, re-apply initial text, default: true)  
	 */
	jQuery.fn.inputLabel = function(text,opts) 
	{
		o = jQuery.extend({ color: "#999", e:"focus", force : false, keep : true, innerText: []}, opts || {});
		var clearInput = function (e) 
		{
			var target = jQuery(e.target);
			var value = jQuery.trim(target.val());
			var id = target.attr("id");
			
			if (e.type == e.data.obj.e && value == e.data.obj.innerText[id]) 
			{
				jQuery(target).css("color", "").val("");
				if (!e.data.obj.keep) {
					jQuery(target).unbind(e.data.obj.e+" blur",clearInput);
				}
			} 
			else if (e.type == "blur" && value == "" && e.data.obj.keep) 
			{
				jQuery(this).css("color", e.data.obj.color).val(e.data.obj.innerText[id]);
			}
			
		};
		return this.each(function () 
		{
			var id = jQuery(this).attr("id");
			var jthis = jQuery(this);

			//Use the text that was passed in, or use false
			o.innerText[id] = (text || false);
			
			//If no text was passed in then we get the text from the label for this input item
			if (!o.innerText[id]) 
			{
				o.innerText[id] = jthis.parents("form").find("label[for=" + id + "]").hide().text();
			}
			//If for some reason the one given isn't text then convert it with jQuery
			else if (typeof o.innerText[id] != "string") 
			{
				o.innerText[id] = jQuery(o.innerText[id]).text();
			}
			
			//Trim off spaces and shizzle
			o.innerText[id] = jQuery.trim(o.innerText[id]);
			
			
			//Bind this fool to a clearInput fffffffunction
			if(jthis.attr('type') != 'password')
			{
				//Fill your input with either the label text if you are either forcing it, or if it's empty
				if (o.force || jthis.val() == "") 
				{
					jthis.val(o.innerText[id]).css("color", o.color);//.css("border", "2px solid red");
				}
				
				jthis.bind(o.e+" blur",{obj:o},clearInput);
			}
			//Okay, so,  for passwords we need to do things a bit differently
			//You cant' change the "type" attribute in IE so you have to make a clone
			else
			{
				var passclone_id = id + '_clone';
				jthis.hide();
				jthis.before('<input type="text" id="' + passclone_id + '" value="' + o.innerText[id] + '" />');
				jQuery('#' + passclone_id).css("color", o.color).focus(function (e)
					{
						jQuery('#' + id).show().val("").focus();
						jQuery(this).hide();
					});
				
				jthis.blur(function (e)
					{
						//Only turn on the label if they have entered nothing into the password thing
						if(jQuery(this).val() == "")
						{
							jQuery(this).hide();
							jQuery('#' + passclone_id).show();
						}
					});
			}
		});
	};
})(jQuery);
