/**
 * jQuery toggletextfieldvalue Plugin
 * 
 * Licensed under The MIT License
 * 
 * @version     0.1
 * @since       14.04.2011
 * @author      Naden Badalgogtapeh <n.b@naden.de>
 * @link        http://www.naden.de/blog Demonstration
 * @link        http://www.naden.de/blog/toggletextfieldvalue-jquery-plugin Blogpost about this plugin (german)
 * @license     http://www.opensource.org/licenses/mit-license.php MIT 
 * @package     jQuery Plugins
 * @subpackage  toggletextfieldvalue
 */ 
(function($) {
/**
 * Usage: $('your selector').toggletextfieldvalue({options});
 *
 * @param options All possible attributes
 *
 * Options:
 * ---------------------------------------
 * NAME:        DEFAULT   DESCRIPTION                     POSSIBLE VALUES
 *
 * autoselect:  false     auto select text on focus       true or false
 * mouseover:   null      mouse over callback function    function
 * mouseout:    null      mouse out callback function     function 
 *
 */
$.fn.toggletextfieldvalue = function() {
  
  var settings = $.extend({}, $.fn.toggletextfieldvalue.defaults, arguments[0] || {});
  
  this.each(function() {

    var text = $(this).val();
    
    var over = function() {
      this.focus();
      if(this.value == text) {
        this.value = '';
      }
      else if(settings.autoselect) {
        this.select();
      }
      if($.isFunction(settings.mouseover)) {
        settings.mouseover(this);
      }  
    }; 
  
    $(this).click(over).mouseover(over).mouseout(function() {
      if(this.value.length == 0) {
        this.value = text;
      }
      
      if($.isFunction(settings.mouseout)) {
        settings.mouseout(this);
      }
    });
  
  });
}

// default settings
$.fn.toggletextfieldvalue.defaults = {
  autoselect: false,
  mouseover: null,
  mouseout: null
};
  
})(jQuery);
