/*!
 * defaultText - v1.0 - 2/21/2009
 * http://benalman.com/
 * 
 * Copyright (c) 2009 "Cowboy" Ben Alman
 * Licensed under the MIT license
 * http://benalman.com/about/license/
 */

(function($) {
  '$:nomunge'; // Used by YUI compressor.
  
  var class_name = 'intext-help',
    p_defaultTextClass,
    defaultText = 'defaultText';
  
  $.defaultTextClass = p_defaultTextClass = function( c ) {
    class_name = c;
  };
  
  $.fn[defaultText] = function( default_text ) {
    var e_ns = '.' + defaultText,
      focus = 'focus' + e_ns,
      blur = 'blur' + e_ns;
    
    return this.each(function(){
      if ( default_text ) {
        $(this)
          .unbind( e_ns )
          .bind( focus, function(){
            var that = $(this);
            if ( that.val() === default_text ) {
              that
                .val( '' )
                .removeClass( class_name );
            }
          })
          .bind( blur, function(){
            var that = $(this);
            if ( $.trim( that.val() ) === '' || that.val() === default_text ) {
              that
                .addClass( class_name )
                .val( default_text );
            }
          })
          .trigger( blur );
      } else {
        $(this)
          .trigger( focus )
          .unbind( e_ns );
      }
    });
  };
  
})(jQuery);

// About:
// 
// Set default text for an input that disappears when focused.. but comes back
// on blur (with a vengeance) if the user enters nothing or just whitespace.
// 
// A class of 'default' (can be changed via $.defaultTextClass method) is added
// to the input when its value is set to the default_text value. 
// 
// Sample Usage:
// 
// $('input[name=search]').defaultText('Enter Search Terms');    // set
// $('input[name=search]').defaultText(false);                   // unset


