Gratipay.tips = {};

Gratipay.tips.init = function() {

    Gratipay.forms.jsEdit({
        confirmBeforeUnload: true,
        hideEditButton: true,
        root: $('.your-tip.js-edit'),
        success: function(data) {
            Gratipay.notification(data.msg, 'success');
            Gratipay.tips.afterTipChange(data);
        }
    });

    $('.your-tip button.edit').click(function() {
        $('.your-tip input').focus();
    });

    $('.your-tip button.stop').click(function() {
        $('.your-tip input').val('0');
        $('.your-tip button.save').click();
    });

    $('.your-tip button.cancel').click(function() {
        $('.your-tip form').trigger('reset');
    });

    // Cancel if the user presses the Escape key
    $('.your-tip input').keyup(function(e) {
        if (e.keyCode === 27)
            $('.your-tip button.cancel').click();
    });

    // Restore the tip value if stored
    if (localStorage.tipAfterSignIn) {
        var data = JSON.parse(localStorage.tipAfterSignIn);
        localStorage.removeItem('tipAfterSignIn');

        if (window.location.pathname === '/'+data.tippee+'/')
            $('.your-tip input').val(data.val).change();
    }

    // Store the tip value if the user hasn't signed in
    if ($('.sign-in').length)
        $(window).on('unload.tips', function() {
            var tip = $('.your-tip input');
            if (tip.parents('form').hasClass('changed'))
                localStorage.tipAfterSignIn = JSON.stringify({
                    tippee: tip.data('tippee'), val: tip.val()
                });
        });
};


Gratipay.tips.initSupportGratipay = function() {
    $('.support-gratipay button').click(function() {
        var amount = parseFloat($(this).attr('data-amount'), 10);
        Gratipay.tips.set('Gratipay', amount, function() {
            Gratipay.notification("Thank you so much for supporting Gratipay! :D", 'success');
            $('.support-gratipay').slideUp();

            // If you're on your own giving page ...
            var tip_on_giving = $('.your-tip[data-tippee="Gratipay"]');
            if (tip_on_giving.length > 0) {
                tip_on_giving[0].defaultValue = amount;
                tip_on_giving.attr('value', amount.toFixed(2));
            }
        });
    });

    $('.support-gratipay .no-thanks').click(function(event) {
        event.preventDefault();
        jQuery.post('/ride-free.json')
            .success(function() { $('.support-gratipay').slideUp(); })
            .fail(function() { Gratipay.notification("Sorry, there was an error.", "failure"); })
    });
};


Gratipay.tips.afterTipChange = function(data) {
    $('.my-total-giving').text(data.total_giving_l);
    $('.total-receiving[data-tippee="'+data.tippee_id+'"]').text(data.total_receiving_tippee_l);
    $('#payment-prompt').toggleClass('needed', data.amount > 0);
    $('.npatrons[data-tippee="'+data.tippee_id+'"]').text(data.npatrons);

    var $your_tip = $('.your-tip[data-tippee="'+data.tippee_id+'"]');
    if ($your_tip) {
        var $input = $your_tip.find('input');
        $input[0].defaultValue = $input.val();
        $your_tip.find('span.amount').text(data.amount_l);
        $your_tip.find('.edit').toggleClass('not-zero', data.amount > 0);
        $your_tip.find('.stop').toggleClass('zero', data.amount == 0);
    }
};


Gratipay.tips.set = function(tippee, amount, callback) {

    // send request to change tip
    $.post('/' + tippee + '/tip.json', { amount: amount }, function(data) {
        if (callback) callback(data);
        Gratipay.tips.afterTipChange(data);
    })
    .fail(function(e) {
        Gratipay.notification('Sorry, something went wrong while changing your tip: ' + e.responseJSON.error_message_long + '. :(', 'error');
        console.log.apply(console, arguments);
    });
};

@@ -868,719 +868,8 @@
  %7D);
-%0A%0A    // Restore the tip value if stored%0A    if (localStorage.tipAfterSignIn) %7B%0A        var data = JSON.parse(localStorage.tipAfterSignIn);%0A        localStorage.removeItem('tipAfterSignIn');%0A%0A        if (window.location.pathname === '/'+data.tippee+'/')%0A            $('.your-tip input').val(data.val).change();%0A    %7D%0A%0A    // Store the tip value if the user hasn't signed in%0A    if ($('.sign-in').length)%0A        $(window).on('unload.tips', function() %7B%0A            var tip = $('.your-tip input');%0A            if (tip.parents('form').hasClass('changed'))%0A                localStorage.tipAfterSignIn = JSON.stringify(%7B%0A                    tippee: tip.data('tippee'), val: tip.val()%0A                %7D);%0A        %7D);
 %0A%7D;%0A

remove broken logic for restoring the tip amount after signin