/*
 * jQuery Plugin: clearOnFocus
 * Version 1.0
 *
 * Copyright (c) 2009 David Comeau (http://www.davecomeau.net)
 * Licensed jointly under the GPL and MIT licenses.
 *
 */

(function($) {
	$.fn.clearOnFocus = function() {

		function clearField(el) {
			if($(el).val() == $(el).data('clearOnFocus'))
			{
				$(el).val('');
			}
		}

		function clearOnFocusFocus(event)
		{
			clearField(this);

		}

		function clearOnFocusBlur(event)
		{
			if($.trim($(this).val()) == '')
			{	
				$(this).val($(this).data('clearOnFocus'));
			}
			else
			{
				$(this).data('clearOnFocus', $(this).val());
				$(this).val($(this).data('clearOnFocus'));
			}
		}

		return this.each(function()
			{
				$(this).data('clearOnFocus', $(this).attr('title'));

				//	unbind any previous listeners
				$(this).unbind('focus', clearOnFocusFocus);
				$(this).unbind('blur', clearOnFocusBlur);

				//	bind listeners to the functions
				$(this).bind('focus', clearOnFocusFocus);
				$(this).bind('blur', clearOnFocusBlur);

			}
		);
	};
})(jQuery);
