﻿

//jQuery.fn.hint = function() {
//    return this.each(function() {
//        var t = jQuery(this);
//        var $form = jQuery(this.form);
//        var title = t.attr('title');
//        if (title) {
//            t.focus(function() {
//                if (t.val() == title) {
//                    t.val('');
//                    t.removeClass('blur');
//                }
//            })
//            t.blur(function() {
//                if (t.val() == '') {
//                    t.val(title);
//                    t.addClass('blur');
//                }
//            })
//            $form.submit(function() {
//                alert('entrou');
//                if (t.val() == title) {
//                    t.val('');
//                    t.removeClass('blur');
//                }
//            });
//            t.blur();
//        }
//    });
//}

//jQuery.fn.hint = function(blurClass) {
//    if (!blurClass) {
//        blurClass = 'blur';
//    }

//    return this.each(function() {
//        // get jQuery version of 'this'
//        var $input = jQuery(this),

//        // capture the rest of the variable to allow for reuse
//      title = $input.attr('title'),
//      $form = jQuery(this.form),
//      $win = jQuery(window);

//        function remove() {
//           
//            if ($input.val() === title && $input.hasClass(blurClass)) {
//                $input.val('').removeClass(blurClass);
//            }
//        }

//        // only apply logic if the element has the attribute
//        if (title) {
//            // on blur, set value to title attr if text is blank
//            $input.blur(function() {
//                if (this.value === '') {
//                    $input.val(title).addClass(blurClass);
//                }
//            }).focus(remove).blur(); // now change all inputs to title

//            // clear the pre-defined text when form is submitted
//            $("form[name=aspnetForm]").submit(function() {
//                alert('alala');
//                return remove
//            });
//            $win.unload(remove); // handles Firefox's autocomplete
//        }
//    });
//};

//jQuery.fn.hint = function() {
//    return this.each(function() {
//        var t = $(this);
//        var title = t.attr('title');
//        if (title) {
//            t.focus(function() {
//                if (t.val() == title) {
//                    t.val('');
//                    t.removeClass('blur');
//                }
//            });
//            t.blur(function() {
//                if (t.val() == '') {
//                    t.val(title);
//                    t.addClass('blur');
//                }
//            });
//            t.parents('form:first').submit(function() {
//                if (t.val() == title) {
//                    t.val('');
//                    t.removeClass('blur');
//                }
//            });
//            t.blur();
//        }
//    });
//}

(function($) {

    $.fn.hint = function(blurClass) {
        if (!blurClass) {
            blurClass = 'blur';
        }

        return this.each(function() {
            // get jQuery version of 'this'
            var $input = $(this),
            // capture the rest of the variable to allow for reuse
            title = $input.attr('title'),
            $form = $(this.form),
            $win = $(window);
            
            //crackeando o dopostback
            if (typeof (__doPostBack) != undefined && typeof (__doPostBack) != 'undefined') {
                var __oldDoPostBack = __doPostBack;
                __doPostBack = AlwaysFireBeforeFormSubmit;
            }

            // clear the pre-defined text when form is submitted
            function remove() {
                if ($input.realval() === title && $input.hasClass(blurClass)) {
                    $input.val('').removeClass(blurClass);
                }
            }

            function AlwaysFireBeforeFormSubmit(eventTarget, eventArgument) {

                remove();
                return __oldDoPostBack(eventTarget, eventArgument);
            }

            // only apply logic if the element has the attribute
            if (title) {
                // on blur, set value to title attr if text is blank
                $input.blur(function() {
                    if (this.value === '') {
                        $input.val(title).addClass(blurClass);
                    }
                }).focus(remove).blur(); // now change all inputs to title
                $form.submit(remove);
                $win.unload(remove); // handles Firefox's autocomplete
            }
        });
    };

    $.fn.realval = $.fn.val;

    $.fn.val = function(value) {
        var i = $(this);
        if (value === undefined) {
            return (i.realval() === i.attr('title')) ? '' : i.realval();
        } else {
            return i.realval(value);
        }
    }

})(jQuery);

//jQuery.fn.hint = function() {
//    return this.each(function() {
//        var t = $(this);
//        var title = t.attr('title');
//        if (title) {
//            t.focus(function() {
//                if (t.val() == title) {
//                    t.val('');
//                    t.removeClass('blur');
//                }
//            })
//            t.blur(function() {
//                if (t.val() == '') {
//                    t.val(title);
//                    t.addClass('blur');
//                }
//            })
//            t.parents('form:first()').submit(function() {
//                if (t.val() == title) {
//                    t.val('');
//                    t.removeClass('blur');
//                }
//            });
//            t.blur();
//        }
//    });
//}


$(document).ready(function() {
    //$('input:text').hint();
    $('input[title!=""]').hint();
    $('textarea').hint();
});
