$(function() {

    Configurator = {
        initialize: function() {
            $('.textbox', Context.configurator_form).numeric();
        },
        update_monthly_items: function(id, description, value, suffix) {
            var entry = $(id, Context.monthly_payment_items);
            if (value > 0) {
                if (value != 1) description += 's';
                if (suffix != undefined) description += ' ' + suffix;
                entry.text(value + ' ' + description).show();
            } else entry.hide();
        },
        bounded_value: function(context, value) {
            if ($('.slider', context).size() > 0) {
                var min = $('.slider', context).slider('option', 'min');
                var max = $('.slider', context).slider('option', 'max');
                if (value > max) value = max;
                if (value < min) value = min;
            }
            return value;
        },
        recalculate_monthly_total: function() {
            var total = 0;
            $('.monthly .product',Context.configurator).each(function() {
                var data = $(this).find('tr').data('product');
                var rate = data ? data.recurring_fee : 0;
                var val = $(this).find('.textbox').val();
                var numItems = parseFloat(val);
                total += numItems*rate;
            });

            // Split apart decimal from dollar value
            s = String(total.toFixed(2));
            dec = s.indexOf('.');
            if(dec < 0) {
                s += '<small style="font-size:14px;">' + '00' + '</small>';
            } else {
                s = s.substring(0,dec) + '<small style="font-size:14px;">' + s.substring(dec + 1) + '</small>';
            }
            $('.monthly .value', Context.monthly_totals).html(s);
        },
        recalculate_one_time_total: function() {
            // TODO
        }
    };
});
