/**
 * FormHelpers - defaultValuedInput plugin for jQuery 1.3.2+
 * 
 * Author: Johannes Wüller
 * Created On: 16.04.2010
 *
 * Ensures that a given value is restored if an <input> loses focus and the
 * default value is removed if the <input> gains focus. This function can be
 * chained.
 *
 * Usage:
 *    jQuery("input.username").defaultValuedInput('username');
 *
 * Parameters:
 *    default_value   value to be maintained as the default
 *    active_class    class used to indicate if the input is not default-valued.
 *                    default: "active"
 */
(function() {

   jQuery.fn.defaultValuedInput = function(default_value, active_class) {
      active_class = active_class || 'active';
      
      jQuery(this).each(function() {
         var focusAction = function() {
            if (jQuery(this).val() == default_value) {
               jQuery(this).val('');
            }
            jQuery(this).addClass(active_class);
         };
         var blurAction = function() {
            if (jQuery(this).val() == default_value || jQuery(this).val() == '') {
               jQuery(this).val(default_value).removeClass(active_class);
            }
         };

         // install everything
         focusAction.call(this);
         blurAction.call(this);
         jQuery(this).focus(focusAction).blur(blurAction);
      });
      
      return jQuery(this);
   };

}());