﻿(function($) {
    $.fn.watermark = function(css, text) {
        return this.each(function() {
            var i = $(this), w;
            i.focus(function() {
                w = i.data('w');
                w && !(w = 0) && i.removeClass(css).data('w', 0).val('');
            })
			.blur(function() {
			    w = i.data('w');
			    !i.val() && (w = 1) && i.addClass(css).data('w', 1).val(text);
			})
			.closest('form').submit(function() {
			    w = i.data('w');
			    w && i.val('');
			});
            i.blur();
        });
    };
    $.fn.removeWatermark = function() {
        return this.each(function() {
            $(this).data('w') && $(this).val('');
        });
    };

    //use this method to set value to a watermark input.
    $.fn.disableWatermark = function(css, text) {
        return this.each(function() {
            (text) ? $(this).removeClass(css).data('w', 0).val(text) : $(this).blur();
        });
    };
    //use this method to reset value to inital watermark. 
    $.fn.resetWatermark = function() {
        return this.each(function() {
            $(this).val('') && $(this).blur();
        });
    };
})(jQuery);
