(function($) {
    "use strict"; // Start of use strict

    $(document).ready(function() {
        function fetch_pricing() {
            var url = '/pricing/' + $('#pricing_name').val() + '/calculate/';
            var cores = $('#cores').val();
            var memory = $('#memory').val();
            var storage = $('#storage').val();
            $.ajax({ 
                type: 'GET', 
                url: url, 
                data: { cores: cores, memory: memory, storage: storage}, 
                dataType: 'json',
                success: function (data) { 
                    if (data && data['subtotal']) {
                        $('#subtotal').text(data['subtotal']);
                        $('#total').text(data['total']);
                    }
                }
            });
        };

        function incrementValue(e) {
            var valueElement = $(e.target).parent().parent().find('input');
            var step = $(valueElement).attr('step');
            var min = parseInt($(valueElement).attr('min'));
            var max = parseInt($(valueElement).attr('max'));
            var new_value = 0;
            if (e.data.inc == 1) {
                new_value = Math.min(parseInt($(valueElement).val()) + parseInt(step) * e.data.inc, max);
            } else {
                new_value = Math.max(parseInt($(valueElement).val()) + parseInt(step) * e.data.inc, min);
            }
            $(valueElement).val(new_value);
            fetch_pricing();
            return false;
        };
        if ($('#pricing_name') != undefined) {
            fetch_pricing();
        }
    
        $('.fa-plus-circle.right').bind('click', {inc: 1}, incrementValue);
    
        $('.fa-minus-circle.left').bind('click', {inc: -1}, incrementValue);
    });
})(jQuery);
