/*
 * Copyright : (c) 2010 Webfish IT Services
 * Website   : http://www.webfish.nl
 * Email     : info@webfish.nl
 * -------------------------------------------------------------------------
 *   $Id: jquery.hint.js 37 2010-09-21 12:34:06Z caspar $
 * -------------------------------------------------------------------------
 */

(function($){

	$.fn.hint = function(){

		return this.each(function(){
			var $input = $(this);
			var title  = $input.attr('title');
			var $form  = $(this.form);
			var $win   = $(window);

			function inputFocus()
			{
				if ($input.val() === title)
				{
					$input.val('');
				}
			}

			function inputBlur()
			{
				if ($input.val() === '')
				{
					if ($input.attr('type') == 'password')
					{
						var $new_input = $('#'+$input.attr('id')+'_text');
						if ($new_input)
						{
							$input.hide();
							$new_input.show();
						}
					}
					else
					{
						$input.val(title);
					}
				}
			}
			if (title)
			{
				if ($input.attr('type') == 'password')
				{
					var $new_input = $('<input type="text" />')
						.attr('id', $input.attr('id')+'_text')
						.attr('title', $input.attr('title'))
						.val($input.attr('title'))
						.attr('className', $input.attr('className'))
						.focus(function(){
							input_id = this.id.substr(0, this.id.length - 5);
							var $input_password = $('#'+input_id);
							if ($input_password)
							{
								$input_password.show().focus();
								$(this).hide();
							}
						});

					$new_input.insertBefore($input);
				}
				$input.blur(inputBlur).focus(inputFocus).blur();
				$form.submit(inputFocus);
				$win.unload(inputFocus); // handles Firefox's autocomplete
			}
		});
	};

})(jQuery);
