(function($){
	var text, color = "#AAA";
	
	// public exposed watermark extension
	$.fn.watermark = function(optionalText) {
		text = optionalText;
		return this.each(applyWatermarkHandlers);
	}
	
	// applies the basic watermark handlers to the element
	function applyWatermarkHandlers() {
		var $this = $(this);
		var currentText = text || $this.attr("title");
		
		if(!currentText || currentText.length == 0)
			throw "jQuery.watermark() Error: Watermarked elements must at least have a title attribute if no watermarked text is provided to the method.";
		if(!$this.is("textarea, input[type=text]"))
			throw "jQuery.watermark() Error: Watermarked elements must be a form field that accepts text input.";
		
		$this.attr("title", "").data("text", currentText).data("w", false).blur(watermarkOn).focus(watermarkOff).focus().blur();
	}
	
	// focus handler for watermarked elements
	function watermarkOn() {
		var $this = $(this);
		if($this.val().length == 0 && !$this.data("w"))
			$this.data("w", true).css("color", color).val($this.data("text"));
	}
	
	// blur handler for watermarked elements
	function watermarkOff() {
		var $this = $(this);
		if($this.data("w"))
			$this.data("w", false).css("color", "").val("");
	}	
	
})(jQuery);