(function($) {
  
  $.fn.example = function(text, args) {
    
    /* Only calculate once whether a callback has been used. */
    var isCallback = $.isFunction(text);
    
    var options = $.extend({}, args, {example: text});
    
    return this.each(function() {
      
      /* Reduce method calls by saving the current jQuery object. */
      var $this = $(this);
      
      if ($.metadata) {
        
        /* If the Metadata plugin is being used merge in the options. */
        var o = $.extend({}, $.fn.example.defaults, $this.metadata(), options);
      } else {
        var o = $.extend({}, $.fn.example.defaults, options);
      }
      
      if (!$.fn.example.boundClassNames[o.className]) {
      
        $(window).unload(function() {
          $('.' + o.className).val('');
        });
      
        $('form').submit(function() {
        
          /* Clear only the fields inside this particular form. */
          $(this).find('.' + o.className).val('');
        });
      
        /* Add the class name to the array. */
        $.fn.example.boundClassNames[o.className] = true;
      }
    
      if ($.browser.msie && !$this.attr('defaultValue') && (isCallback || $this.val() == o.example))
        $this.val('');

      if ($this.val() == '' && this != document.activeElement) {
        $this.addClass(o.className);

        $this.val(isCallback ? o.example.call(this) : o.example);
      }
      
      $this.focus(function() {
        
        /* jQuery 1.1 has no hasClass(), so is() must be used instead. */
        if ($(this).is('.' + o.className)) {
          $(this).val('');
          $(this).removeClass(o.className);
        }
      });
    
      /* Make the example text reappear if the input is blank on blurring. */
      $this.blur(function() {
        if ($(this).val() == '') {
          $(this).addClass(o.className);
          
          $(this).val(isCallback ? o.example.call(this) : o.example);
        }
      });
    });
  };
  
  $.fn.example.defaults = {
    className: 'example'
  };
  
  $.fn.example.boundClassNames = [];
  
})(jQuery);
