/*
 * jQuery DefaultValue plugin v1.0
 * File Date: 11/12/2008
 *
 * Copyright (c) 2008 Jim Salyer
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 */

$.fn.extend({
  defaultValue: function(locals)
  {
    // create the global properties and extend them with the given ones
    var globals = {
      value: "",
      reset: true
    };
    $.extend(globals, locals);
    
    // loop through all of the given elements
    return $(this).each(function()
    {
      // if the current element is an input field, process it
      var tag = this.tagName.toLowerCase(); 
      if ((tag == "input" && $(this).attr("type") == "text") || tag == "textarea")
      {
        // set up the field's default value
        var el = $(this);
        var value = typeof(globals.value) == "string" && globals.value != "" ? globals.value : el;
        
        // make sure we remove the default value's before submitting the parent form
        $(this.form).submit(function()
        {
          if (el.val() == value) el.val("");
        });
        
        el.val(value).focus(function()
        {
          // blank out the field's value when the field is focused (if applicable)
          if (el.val() == value) el.val("");
        }).blur(function()
        {
          // reset the field's value to the default when blurring the field (if applicable)
          if (globals.reset && el.val() == "") el.val(value);
        });
      }
    });
  }
});