/************************************************************************
 *
 * prePopulate - 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:         DOM Class of fields to be populated
 *   populatedClass:    DOM Class applied to fields when they are populated
 *
 * See below for default option values.
 *
 ************************************************************************/

(function($) {
  $.prePopulate = function(userOptions) {
    var options = {
      className: 'prepopulate',
      populatedClass: 'populated'
    };
    if (userOptions) { $.extend(true, options, userOptions); }

    // Find all fields with the className
    var $fields = $('.'+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 fieldTitle = $field.attr("title");
      $field.val(fieldTitle);
      $field.addClass(options.populatedClass);
      $field.focus(function(){
        $f = $(this);
        if ($.trim($f.val()) == $f.attr("title")) {
          $f.val('');
          $field.removeClass(options.populatedClass);
        }
      });
      $field.blur(function(){
        $f = $(this);
        if ($.trim($f.val()) == '') {
          $f.val($f.attr("title"));
          $field.addClass(options.populatedClass);
        }
      });
    });
  };
})(jQuery);