﻿(function() {
    jQuery.fn.FieldValueLabel = function(options) {
        // Extend our defaults with the user's preferred options
        options = jQuery.extend({}, jQuery.fn.FieldValueLabel.defaults, options);

        this.each(function() {
            $this = jQuery(this);
            var currentValue = $this.val().replace(/^\s+|\s+$/gi, "");
            if (currentValue == "" || currentValue == options.label) {
                $this.val(options.label);
            }

            $this.bind("focus blur", function(event) {
                var field = jQuery(event.currentTarget);
                var value = field.val().replace(/^\s+|\s+$/gi, "");
                if (event.type == "focus") {
                    if (value == options.label) {
                        field.val("");
                    }
                } else {
                    if (value == "") {
                        field.val(options.label);
                    }
                }
            });

            $this.parents("form:first").bind("submit", function(event) {
                var currentValue = $this.val().replace(/^\s+|\s+$/gi, "");
                if (currentValue == options.label) {
                    $this.val("");
                }
            });
        });
    };

    jQuery.fn.FieldValueLabel.defaults = { label: "Search" };
})();
