/**
* Written by: Nathan Newport
* Set the default value for an input field and if the input is empty on blur, restore the default value.
* Jan 20, 2010
*/
(function( $ )
{    
    var methods = {
    
            init : function( options )
            {//specify the default settings
                var settings = {
						value: ''
                }
                //override defaults if options are provided
                if( options )
                {
					if(typeof(options) == 'string')
						settings.value = options;
					else
	                    $.extend( settings, options);
                }
				
                var $this = $(this),
				data = $this.data('defaultValue');
				
                 // If the plugin hasn't been initialized yet
                if ( ! data )
                 {
					 data = { }; //set default values
					 $.extend(data, settings);	// copy the settings into data
                   $(this).data('defaultValue', data );	//assign it to the plugin's data array
                }
				
				//set the field's value to the default value if it is currently empty.
				if($(this).val() == '') 
					$(this).val(data.value); 
				
				//reset the value to the default on blur (if empty)
				$(this).bind('blur.defaultValue', function()
				{
					if($(this).val() == '')
						$(this).val(data.value); 
				});
				
				//clear the value on focus
				$(this).bind('focus.defaultValue', function()
				{
					if($(this).val() == data.value)
						$(this).val(''); 
				});
                return $(this);
            },
			
            stop: function( options )
            {   
                return $(this);
            }
    }
    
    //declare the plugin and allow method calling
     $.fn.defaultValue = function( method )
     {
            // Method calling logic
            if ( methods[method] ) {
              return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
            } else if ( typeof method === 'object' || ! method ) {
              return methods.init.apply( this, arguments );
            } else if ( typeof method === 'string') {
              return methods.init.apply( this, arguments );
            } 
			else {
              $.error( 'Method ' +  method + ' does not exist on jQuery.defaultValue' );
            }
     };
    
})( jQuery );
