jQuery(document).ready(function($){
	(function($){
		$.fn.getTotal = function(id) {
			var total = 0;
			$(".input-"+id).each(function() {
				var val = parseInt($(this).val());
				var max = parseInt($(this).data("max"));
				if(isNaN(val)) {
					val = 0;
				}
				if(val > max) {
					val = max;
					$(this).val(val);
				}
				total += val;
			})
			var grades = $("#grade-"+id).data("grades");
			for (var key in grades) {
				if(total >= grades[key].min && total <= grades[key].max) {
					$("#grade-"+id).html(grades[key].grade);
				}
			}
			$("#total-"+id).html(total);
		}
	})(jQuery);

	$(document).on("keyup", ".gradebook-input", function(){
		var id = $(this).data("studentId");
		$("#total-"+id).getTotal(id);
	})
})