$(document).ready(function(){

	var sum_filesize = 0;
	$(".select_file")
		.mousedown(function()
		{
			if($(this).attr('checked') == true)
			{
				//$(this).attr('checked', false)
				$(this).parent().parent().parent().removeClass("selected");
		
				sum_filesize -= parseFloat($(this).attr('size'));
				$("#zip > a> span").html(format_size(sum_filesize));
			}
			else
			{
				//$(this).attr('checked', true)
				
				$(this).parent().parent().parent().addClass("selected");
	
				sum_filesize += parseFloat($(this).attr('size'));
				$("#zip > a > span").html(format_size(sum_filesize));
			}	
			
			
		})
		.click(function()
		{
			$("#zip > a > i").html(parseInt($("#files").find(".select_file:checked").length));	
		});
		
		$("#zip a").click(function()
		{
			$("#files").submit();
		});
});

function format_size(bytes)
{ 
	// Setup some common file size measurements.
	var kb = 1024; // Kilobyte
	var mb = 1048576; // Megabyte
	var gb = 1073741824; // Gigabyte
	var tb = 1099511627776; // Terabyte
		 
	// If it's less than a kb we just return the size, otherwise we keep going until the size is in the appropriate measurement range.
	if (bytes < kb)
	{
		return bytes + ' B';
	}	
	else if (bytes < mb)
	{
		return Math.round(bytes / kb) + ' KB';
	}
	else if (bytes < gb)
	{
		return (Math.round((bytes / mb)*10)/10) + ' MB';
	}
	else if (bytes < tb)
	{
		return (Math.round((bytes / gb)*10)/10) + ' GB';
	}
	else
	{
		return (Math.round((bytes / tb)*10)/10) + ' TB';
	}
} 
