(function ($) {
    var jgDonateTabs = {
        joincontrol: function (textinput, radioinput) {
            $(textinput)
                .focus(function () { $(radioinput).attr('checked', 'checked'); })
                .blur(function () { $(radioinput).attr('checked', ''); });
            $(radioinput).click(function () { $(textinput).focus(); });
        }
    };

    var logAnalyticsTrackEventOnDonateButtonClick = function (settings) {
        $('#' + settings.tabId + ' input[type=submit]').click(function () {
            var amount;
            $('#' + settings.tabId + ' input[type=radio]:checked').each(function () {
                amount = $(this).val();
            });

            amount = amount || $('#' + settings.otherAmountId).val();
            if (amount) {
                JustGiving.Analytics.push(['_trackEvent', 'Direct Donation', settings.donationType, [amount, settings.donationSuffix].join(' ')]);
            }
        });
    };

    $(document).ready(function () {
        $("#tabs").tabs({ selected: 1 });
        jgDonateTabs.joincontrol('#otherMonthlyAmount', '#otherMonthly');
        jgDonateTabs.joincontrol('#otherOneOffAmount', '#otherOneOff');

        logAnalyticsTrackEventOnDonateButtonClick({ tabId: 'monthly-tab', otherAmountId: 'otherMonthlyAmount', donationType: 'Regular', donationSuffix: 'DD' });
        logAnalyticsTrackEventOnDonateButtonClick({ tabId: 'one-off-tab', otherAmountId: 'otherOneOffAmount', donationType: 'One off', donationSuffix: 'CC' });
    });
})(jQuery);
