$(document).ready(function() {
	$('#songdur').keyup(function() {
		calculate();
	});
	
	$('#iPodsize').change(function() {
		calculate();
	});
	
	$('#bCus').keyup(function() {
		calculate();
	});
	
	function calculate() {
		var size 		= $('#iPodsize').val();
		var songlength 	= $('#songdur').val();
		var cus			= $('#bCus').val();
		var us 			= size*1000*1000*1000/1024/1024/1024;
		var amt			= 0;
		
		if (isNaN(songlength)) {
			window.alert("Error: Input a numeric song length.");
			$('#songdur').val('4');
		}
		
		if (isNaN(cus)) {
			window.alert("Error: Input a numeric custom bitrate.");
			$('#bCus').val('480');
		}
		
		$('input.result').each(function(index) {
			if (this.name == 'fCus') {
				amt = Number(cus);
			} else if (this.name == 'fLoss') {
				amt = 850;
			} else {
				amt = this.name.substr(1);
			}
			
			$(this).val(Math.floor(us*1024*1024*8/amt/(songlength*60)));
		});
		
		$('input[@name=usefulspace]').val(us.toFixed(2));
	}
});