/**
 * Paweł "Tre^" Korzeniewski
 * Usage: jQuery('#element').clearOnFocus();
 */

(function($) {

    var settings = {};

    var methods = {
        _init: function (options) {
            
            return this.each(function() {
                
                var $this = $(this);

                if (options) {
                    $.extend(settings, options);
                }

                $this.data('defval', $this.val());
                $this
                .focus(function() {
                    if ($this.val() == $this.data('defval')) {
                        $this.val('');
                        if (settings.onfocus) {
                            settings.onfocus($this);
                        }
                    }
                })
                .blur(function() {
                    if ($this.val() == '') {
                        $this.val($this.data('defval'));
                        if (settings.onblur) {
                            settings.onblur($this);
                        }
                    }
                });
            });
        }
    };

    $.fn.clearOnFocus = function(method, options) {
    
        if (typeof method === 'string' && method.substr(0, 1) != '_' && methods[method]) {
            return methods[method].apply(this, [options]);
        } else if (typeof method === 'object' || !method) {
            return methods._init.apply(this, [method]);
        } else {
            $.error('Method ' + method + ' does not exist on plugin');
        }
        
    };
    
})(jQuery);

