/************************************************************************
 *
 * formFieldHints - jQuery plugin 1.0 
 * Copyright (c) 2008 Alistair Holt (koopd.com)
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 *
 * Options:
 *
 *   className:          The plugin will look for text fields with
 *                       this class. Defaults to 'hintable'
 *   hintClass:          The class that gets applied to text fields that are
 *                       showing a hint. Defaults to 'hinted'
 *
 ************************************************************************/

(function($) {
  $.formFieldHints = function(userOptions) {
    var options = {
      className: 'hintable',
      hintClass: 'hinted'
    };
    if (userOptions) { $.extend(true, options, userOptions); }

    // Find all text fields with the className
    var $fields = $('input[type="text"].'+options.className);
    
    // Iterate over the fields and set their title attributes
    // as their values. We'll also assign focus and blur functions.
    $fields.each(function(){
      var $field = $(this);
      var $fieldLabel = $("label[for="+$field.attr("id")+"]");
      if ($fieldLabel) {
        $fieldLabel.show();
        $field.focus(function(){
          $fieldLabel.hide();
        });
        $field.click(function(){
          $fieldLabel.hide();
        });
        $field.blur(function(){
          $f = $(this);
          if ($.trim($f.val()) == '') {
            $fieldLabel.show();
          }
        });
      }
    });
  };
})(jQuery);