(function($) {
  $.fn.extend({
    watermark: function(emptyText) {
      function addWatermark(input) {
        if (input.val() == "") {
          input.addClass("empty").val(emptyText);
        }
      }

      function removeWatermark(input) {
        if (input.val() == emptyText) {
          input.removeClass("empty").val("");
        }
      }

      this.filter("input[type=text]").each(function() {
        var $this = $(this)
          .focus(function() { removeWatermark($this); })
          .blur(function() { addWatermark($this); });

        // Remove the watermark before the form submits.
        $this.closest("form").submit(function() { removeWatermark($this); });

        addWatermark($this);
      });
    }
  });
})(jQuery);
