﻿(function($) {
    // Author: Daniel E. Ulfe @ daniel@ulfe.info
    // Known "issue"
    // If you use document.form[0].submit() the fields do not get "clear"
    // Reason: For some reason the browser does not fire the "submit" event when "submiting" using javascript
    // http://blogs.vertigosoftware.com/snyholm/archive/2006/09/27/3788.aspx

    $.watermarks = new Array();


    $.watermark = function(elem, options) {
        $(elem).watermark(options);
    }

    $.watermark.ToggleStatusType = { Focus: 0, Blur: 1 };

    $.watermark.toggleStatus = function(obj, statusType) {
        var cssAdd, cssRemove, valCompare, valAssign, dom = obj[0];

        switch (statusType) {
            case $.watermark.ToggleStatusType.Focus:
                cssAdd = dom.normalCss;
                cssRemove = dom.options.css;
                valCompare = dom.options.text;
                valAssign = "";
                break;
            case $.watermark.ToggleStatusType.Blur:
                cssAdd = dom.options.css;
                cssRemove = dom.normalCss;
                valCompare = "";
                valAssign = dom.options.text;
                break;
            default:
                throw ("ArgumentException: ToggleStatusType");
                break;
        }


        if (obj.val() == valCompare) {
            obj.val(valAssign);
            obj.removeClass(cssRemove);
            obj.addClass(cssAdd);
        }
    };

    $.watermark.clearFields = function() {
        for (var item, i = 0; i < $.watermarks.length; i++) {
            item = $.watermarks[i];
            if (item[0].options.text == item.val())
                item.val("");
        }
    };

    $.watermark.restoreFields = function() {
        for (var item, i = 0; i < $.watermarks.length; i++) {
            item = $.watermarks[i];
            if (item.val() == "")
                item.val(item[0].options.text);
        }
    };

    $.fn.watermark = function(options) {
        return this.each(function() {
            // Shortcut to the current object
            $this = $(this);
            // Shortcut to "enum"
            var type = $.watermark.ToggleStatusType;

            // Inserting the current object to the watermarks' list
            $.watermarks.push($this);

            // Merging "parameters" with "default values"
            this.options = $.extend({}, $.fn.watermark.defaults, options);

            // Captuting the "default" css
            this.normalCss = $this.attr("class");

            // Making sure we have a watermark css
            if (this.options.css == "")
                this.options.css = this.normalCss;
                
            // Inserting watermark if needed
            $.watermark.toggleStatus($this, type.Blur);

            $this.focus(function() {
                // Removing watermark if needed
                $.watermark.toggleStatus($(this), type.Focus);
            });
            $this.blur(function() {
                // Inserting watermark if needed
                $.watermark.toggleStatus($(this), type.Blur);
            });
        });
    };

    $.fn.watermark.defaults = {
        css: '',
        text: ''
    };

    $(document).ready(function() {
        // Is the current page an ASP.net page with ASP Validators?
        if (typeof (Page_ClientValidate) == 'function') {
            // Capture Main ASP Validator Function
            $.watermark.Page_ClientValidate = Page_ClientValidate;
            // Rewrite that function
            Page_ClientValidate = function(validationGroup) {
                // Clear "watermarks" so the validations can work as normal
                $.watermark.clearFields();

                // Calling original ASP validators
                var result = $.watermark.Page_ClientValidate(validationGroup);

                // If "not valid" then restore watermarks
                if (!result)
                    $.watermark.restoreFields();

                return result;
            }
        }
        else {
            // Is the current page an ASP.net page and the Validators are inactive? 
            if (typeof (__doPostBack) == 'function') {
                // The "onsubmit" event handler does not get called when the "submit" method is called
                // ASP generates the function "__doPostBack" to "fix" that... so we need to attach our
                // cleaning function to it.
                $.watermark.__doPostBack = __doPostBack;
                __doPostBack = function(eventTarget, eventArgument) {
                    $.watermark.clearFields();
                    $.watermark.__doPostBack(eventTarget, eventArgument);
                }
            }
        }

        if (typeof (WebForm_OnSubmit) == 'function') {
            $.watermark.WebForm_OnSubmit = WebForm_OnSubmit;
            WebForm_OnSubmit = function() {
                var result = $.watermark.WebForm_OnSubmit();
                if (result)
                    $.watermark.clearFields();

                return result;
            }
        }

    });
})(jQuery);
