﻿/*
    Basic plugin for watermarking text inputs, with options.
    
    Options:
        activeCss:      CSS class for text which user has entered.
        watermarkCss:   CSS class for the watermark text.
        clearObj:       jQuery object whose submit(for forms) or click(for all others)
                        causes the field's value to be cleared if it matches the watermark text.
*/
$.fn.watermark = function(message, options) {

    var defaults = {
        watermarkCss: "watermark",
        clearObj: $(this).parents("form:last")
    };

    var options = $.extend(defaults, options);

    return this.each(function() {
        var field = $(this);

        if (field.val() == '') {
            field.val(message);
            field.addClass(options.watermarkCss);
        }

        field.focus(function() {
            if (field.val() == message) { field.val(''); field.removeClass(options.watermarkCss); }
        });
        field.blur(function() {
            if (field.val() == '') { field.val(message); field.addClass(options.watermarkCss); }
        });

        if (options.clearObj.is("form")) {
            options.clearObj.submit(function() {
                if (field.val() == message) { field.val(''); }
            });
        } else {
            options.clearObj.click(function() {
                if (field.val() == message) { field.val(''); }
            });
        }

    });
}