(function($) {

	$.fn.watermark = function(watermark) {
		
		$(this).each(function() {
			var $box = $(this), altText = null;
			if (typeof(watermark) === 'string') {
				// We've been given an explicit watermark
				altText = watermark;
			} else {
				// We weren't given a watermark, attempt to guess
				var label = $('label[for=' + $box.attr('id') + ']');

				if (label.size()) {
					label.hide();
					altText = label.text();
				} else if ($box.attr('title')) {
					altText = $box.attr('title');
				} else {
					alert('Unable to determine watermark for field ' + $box.attr('id'));
					altText = '';
				}
			}

			$box.data('watermark-text', altText);

			if (!$box.val() || $box.hasClass('empty')) {
				$box.val(altText);
				$box.addClass('empty');
			} else if ($box.val() === altText) {
				$box.addClass('empty');
			}

			$box.attr('title', altText);

		}).focus(function() {
			if ($(this).hasClass('empty')) {
				$(this).removeClass('empty');
				$(this).val('');
			}
		}).blur(function() {
			var watermark_text = $(this).data('watermark-text');

			if (!$(this).val() || $(this).val() == watermark_text) {
				// no value specified on blur
				$(this).addClass('empty');
				$(this).val(watermark_text);
			} else {
				// We have a value
				$(this).removeClass('empty');
			}
		});
	

		return this;

	};

})(jQuery);

