(function ($) {

    /**
    @class watermark
    @namespace $
    **/
    $.fn.watermark = function (options) {

        var element = this;

        if (this.length === 0) {
            return;
        }

        this.data("watermark", {

            init: function (options) {

                this._options = options;

                element.val(options.text);
                element.addClass("app-watermark");
                element.on("focus", $.proxy(this._onFocus, this));
                element.on("blur", $.proxy(this._onBlur, this));

            },

            _onFocus: function () {
                if (element.val() === options.text) {
                    element.val("");
                    element.select();
                }
                element.removeClass("app-watermark");
            },

            _onBlur: function () {
                if (element.val().toLowerCase() === options.text.toLowerCase()
                    || element.val() === "") {
                    element.val(this._options.text);
                    element.addClass("app-watermark");
                }
            },

            hasValue: function () {
                return this.value() !== "";
            },

            value: function () {
                if (element.val().toLowerCase() === options.text.toLowerCase() || element.val() === "") {
                    return "";
                }
                return element.val();
            },

            reset: function () {
                element.val(this._options.text);
                element.addClass("app-watermark");
            }

        });

        this.data("watermark").init(options);

    };

})(jQuery);
