(function($) {
    $.fn.overtitle = function(){

        var hasBindedAction = false;

        return $(this).each(function(){
            $(this).bind('focus', function(){
                if ($(this).val() == $(this).attr('title'))
                {
                    $(this).removeClass('overtitle-active');
                    $(this).val('');
                }
            });

            $(this).bind('blur', function(){
              //alert($(this).attr('name') + "\n" + $(this).val());
                if ($(this).val() == '')
                {
                    $(this).addClass('overtitle-active');
                    $(this).val($(this).attr('title'));
                }
            });

            if (!hasBindedAction)
            {
                hasBindedAction = true;
              
                var myForm = $(this).parents('form');
                // Catch form submit for removing title text
                myForm.bind('submit', function(){
                    $(this).find(':input.overtitle').each(function(){         
                        if ($(this).val() == $(this).attr('title'))
                        {
                            $(this).removeClass('overtitle-active');
                            $(this).val('');
                        }
                    });

                    return true;
                });

                // Also catch Ajax form submit for removing title text
                /*$(myForm).ajaxForm({
                    beforeSubmit: function(event, request, options){
                        myForm.find('.overtitle').each(function(){
                            if ($(this).val() == $(this).attr('title'))
                            {
                                $(this).removeClass('overtitle-active');
                                $(this).val('');
                            }
                        });

                        return true;
                    },
                    complete: function() {
                        $(this).addClass('overtitle-active');
                        $(this).val($(this).attr('title'));
                    }
                });*/
            }

            /*$(this).ajaxComplete(function(request, settings){
              alert($(this).attr('name') + "\n" + $(this).val());
                $(this).addClass('overtitle-active');
                $(this).val($(this).attr('title'));
            });*/
            
            // Init
            $(this).blur();
        });
    };
    
    $.fn.overtitleCheck = function(){   
        if ($(this).val() == $(this).attr('title'))
            return true;
        else
            return false;
    };  

    afterSubmit = function()
    {
      $('input.overtitle').overtitle();
    }

    /* Just like overtitle, but user overtitle attribute instead of title */
    $.fn.overtitle2 = function(){
        var hasBindedAction = false;

        return $(this).each(function(){
            $(this).bind('focus', function(){
                if ($(this).val() == $(this).attr('overtitle'))
                {
                    $(this).removeClass('overtitle-active');
                    $(this).val('');
                }
            });

            $(this).bind('blur', function(){
              //alert($(this).attr('name') + "\n" + $(this).val());
                if ($(this).val() == '')
                {
                    $(this).addClass('overtitle-active');
                    $(this).val($(this).attr('overtitle'));
                }
            });

            if (!hasBindedAction)
            {
                hasBindedAction = true;
              
                var myForm = $(this).parents('form');

                // Catch form submit for removing title text
                myForm.bind('submit', function(){
                    $(this).find(':input.overtitle2').each(function(){
                        if ($(this).val() == $(this).attr('overtitle'))
                        {
                            $(this).removeClass('overtitle-active');
                            $(this).val('');
                        }
                    });

                    return true;
                });
               
            }

            // Init
            $(this).blur();
        });
    };
})(jQuery);