(function($) {

    function FormFiller(element, options) {
        this.$el = $(element);
        this.options = options;
        this.init();
    }

    FormFiller.prototype = {
        init:function(){
            var self = this;
            this.$el.val(this.options.text);
            
            //on focus
            this.$el.focus(function(){
                if($(this).val() == self.options.text){
                    $(this).val('');
                }
            });
            
            //on blur
            this.$el.blur(function(){
                if($(this).val() == ''){
                    $(this).val(self.options.text);
                }
            });
            
            //on form submit
            this.$el.parents("form").submit(function(){
                if(self.$el.val() == self.options.text){
                    self.$el.val('');
                }
            });
        }
    };

    $.fn.formFiller = function(options) {

        if (options === true) {
            return this.data('formFiller');
        } else if (typeof options == 'string') {
            return this.each(function(){
                var formFiller = $(this).data('formFiller');
                if (formFiller) {
                    formFiller[options]();
                }
            });
        }
        else if(typeof options == "object" || typeof options == "undefined"){
            options = $.extend({}, $.fn.formFiller.defaults, options);
            return this.each(function(){
                if(!$(this).data('formFiller')){
                    var formFiller = new FormFiller(this,options);
                    $(this).data('formFiller',formFiller);
                }
            });
        }

        return this;
    };

    $.fn.formFiller.defaults = {
        
    };
})(jQuery);

