/**
 * 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: '#666', e:'focus', force: false, keep: true, placeholder: false}, opts || {});
		// Does our borwser have support for placeholder text?
		if (jQuery.browser.safari) o.placeholder = true;
		var clearInput = function(e) {
			var $target = jQuery(e.target);
			var value = jQuery.trim($target.val());
			if (e.type == e.data.obj.e && value == e.data.obj.innerText) {
				$target.css('color', '').val('');
				if (!e.data.obj.keep) {
					$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);
			}
		};
		return this.each(function () {
			var $input = jQuery(this);
			o.innerText = (text || false);
			if (!o.innerText) {
					var id = $input.attr('id');
					o.innerText = $input.parents("form").find("label[for=" + id + "]").hide().text();
			} else {
				if (typeof o.innerText != "string") {
					o.innerText = jQuery(o.innerText).text();
				}
			}
			o.innerText = jQuery.trim(o.innerText);
			if (o.placeholder) {
				$input.attr('placeholder', o.innerText);
				// Need to add special CSS
				// input::-webkit-input-placeholder,
				// isindex::-webkit-input-placeholder,
				// textarea::-webkit-input-placeholder {
				// 	color: inherit;
				// }
			} else {
				if ($input.attr('type') == 'password') {
					// Replace our object
					if (o.force) {
						$input.css('color', o.color).val(o.innerText);
					}
					
					$clone = $input.clone();
					try {
						$clone.attr('type', 'text')
						.attr('id', $input.attr('id') + '_clone')
						.attr('name', $input.attr('name') + '_clone')
						.css('color', o.color)
						.val(o.innerText);
					} catch(err) {
						// Can't change the type just create a new one
						// CONSIDER: Could of done better to include all attributes but may not be needed
						$clone = $('<input type="text" />');
						$clone.attr('id', $input.attr('id') + '_clone')
						.attr('class', $input.attr('class'))
						.attr('name', $input.attr('name') + '_clone')
						.css('color', o.color)
						.val(o.innerText);
					}
					$clone.insertAfter($input);
					$input.hide();
					
					$clone.focus(function() {
						$clone.hide();
						$input.show();
						$input.focus();
					});
					$input.blur(function() {
						if ($input.val() == '') {
							$input.hide();
							$clone.show();
						}
					});
				} else {
					if (o.force || $input.val() == '') {
						$input.css('color', o.color).val(o.innerText);
					}
					
					$input.bind(o.e + ' blur', {obj: o}, clearInput);
				}
			}
		});
	};
})(jQuery);
