// EZPZ Hint v1.1.1; Copyright (c) 2009 Mike Enriquez, http://theezpzway.com; Released under the MIT License
(function($) {
	$.fn.ezpz_hint = function(options) {
		var defaults = {
			hintClass: 'ezpz-hint',
			hintName: 'ezpz_hint_dummy_input'
		};
		var settings = $.extend(defaults, options);

		return this.each(function() {
			var hint;
			var dummy_input;
			var real_input;

			// grab the input's title attribute
			text = $(this).attr('title');

			// create a dummy input and place it before the input
			real_input = $(this);
			if (real_input.attr("type") == "password") {
				dummy_input = $('<input type="text" name="temp" value="" />');
				dummy_input.attr('class', real_input.attr('class'));
				dummy_input.attr('size', real_input.attr('size'));
			}
			else {
				dummy_input = real_input.clone();
				dummy_input.attr("id","temp");
			}
			dummy_input.insertBefore(real_input);

			// set the dummy input's attributes
			dummy_input.attr('name', settings.hintName);
			dummy_input.attr('autocomplete', 'off');
			dummy_input.attr('tabIndex', real_input.attr('tabIndex'));
			dummy_input.addClass(settings.hintClass);
			dummy_input.val(text);

			// hide the input
			real_input.hide();

			// don't allow autocomplete (sorry, no remember password)
			real_input.attr('autocomplete', 'off');

			// bind focus event on the dummy input to swap with the real input
			dummy_input.focus(function() {
				real_input.show();
				real_input.focus();
				real_input.unbind('blur').blur(function() {
					if ($(this).val() == '') {
						$(this).hide();
						dummy_input.show();
					}
				});
				$(this).hide();
			});

			// swap if there is a default value
			if ($(this).val() != '') {
				dummy_input.focus();
			};

			// remove the dummy inputs so that they don't get submitted
			$('form').submit(function() {
				$('.' + settings.hintName).remove();
			});
		});

	};
})(jQuery);
