﻿/* 
* Simple watermark looks for all input elements of type text store their initial
* value on dom load. When element gets focus remove the text if it is the same as 
* the initial text.  Replace the watermark on lostfocus if the element is an empty 
* string
*/
(function ($) {
    $.waterMark = function () {
        $.each($('.watermark'), function () {
            var current = this;
            var currentStyle = $(this).attr('style');
            var currentClass = $(this).attr('class');
            var currentId = $(this).attr('id');
            $(current).attr('data-val-watermark', $(current).val());
            if (current.type.toLowerCase() == "passwordbroken") {
                //alert(current.outerHTML);
                var newTextBox = current.outerHTML;
                newTextBox = newTextBox.replace('type="password"', 'type="text"');
                $(current).replaceWith(newTextBox);
                var jqNewTextbox = $('#' + currentId);
                $('#' + currentId).live('focus', function (event) {
                    //alert('in the password switch');
                    newTextBox = current.outerHTML;
                    newTextBox = newTextBox.replace('type="text"', 'type="password"');
                    $(jqNewTextbox).replaceWith(newTextBox);
                    //alert(currentId);
                    //$('#' + currentId).val('').focus();
                    setTimeout($('#' + currentId).focus(), 200);
                });
            }
            else if (current.type.toLowerCase() == "text" || current.type.toLowerCase() == "password") {
                $(current).bind(
                    'focus', function (event) {
                        if ($.trim($(current).val()) == '') {
                            $(current).val($(current).attr('data-val-watermark'));
                        }
                        else if ($.trim($(current).val()) == $(current).attr('data-val-watermark')) {
                            $(current).val('');
                        }
                        else {
                            $(current).select();
                        }
                    });
                $(current).bind(
                    'blur', function (event) {
                        if ($.trim($(current).val()) == '') {
                            $(current).val($(current).attr('data-val-watermark'));
                        }
                    }
                );
            }
        });
    };
    //
    // Plugin definition goes here
    //
})(jQuery);
