﻿///////////////////////////////////////////////////////////////////////////////////////////
// DefaultValue 1.0
// Version 1.0
// @requires jQuery v1.4.2
// Sets a default value for form fields and removes on focus
// 
// Copyright (c) 2010 Mark Ashley Bell
// Examples and docs at: http://markashleybell.com/jquery/jquery.defaultvalue.html
// 
// Dual licensed under the MIT and GPL licenses:
// http://www.opensource.org/licenses/mit-license.php
// http://www.gnu.org/licenses/gpl.html
///////////////////////////////////////////////////////////////////////////////////////////

(function($){

    $.fn.defaultValue = function(settings) {
        
        var config = { 'text': 'Enter your text' };

        if (settings) $.extend(config, settings);

        // Add a defaultval() function so we can easily compare the field's
        // current value (val()) to it's initial default at any point.
        $.fn.extend({
            defaultval: function (value) {
                
                var field = $(this);

                if(field.is('input[type=text]'))
                {
                    if (typeof value != 'undefined') {
                        field.data('defaultval') = value;
                    }
                    
                    return field.data('defaultval');
                }
            }
        });

        // Initially set the default property, then bind blur and focus events. Focus clears the
        // field value if it's still the default text, blur sets value back to default text if the 
        // value is an empty string.
        this.each(function() {
        
            var field = $(this);
            
            if(field.val() == '') field.val(config['text']);
            field.data('defaultval', config['text']);

            field.bind('focus', function(){

                var f = $(this);
                if(f.val() == f.defaultval()) $(this).val('');

            }).bind('blur', function(){
                
                var f = $(this);
                if(f.val() == '') f.val(f.defaultval());   
            
            });
            
        });

    };

})(jQuery);
