var download = {
	start: function() {
		$('form-files').submit();
	},
	selectFiles: function(type) {
		$ES('input.file', 'form-files').each(function(element) {
			if (type) {
				element.checked = element.hasClass(type);
			} else {
				element.checked = element.checked ? false : true;
			}
		});
		download.updateSelectedFiles();
	},
	updateSelectedFiles: function() {
		files = 0;
		bytes = 0;
		$ES('input.file', 'form-files').each(function(element) {
			if (element.checked) {
				bytes+=Number(element.getNext().value);
				files++;
			}
		});
		//$('btn-download').setStyle('display', (files == 0) ? 'none' : '');
		$('files-count').setHTML(files);
		$('bytes-count').setHTML(download.byteSize(bytes));
	},
	byteSize: function(bytes) {
		if (bytes >= 1073741824) {
			bytes = download.numberFormat(bytes / 1073741824, 2, '.', '') + ' GB';
		} else {
			if (bytes >= 1048576) {
				bytes = download.numberFormat(bytes / 1048576, 2, '.', '') + ' MB';
			} else {
				if (bytes >= 1024) {
					bytes = download.numberFormat(bytes / 1024, 0) + ' KB';
				} else {
					bytes = download.numberFormat(bytes, 0) + ' B';
				};
			};
		};
		return bytes;
	},
	submitComment: function(button) {
		var button = $(button);
		new Ajax('/download.php', {
			method: 'post',
			postBody: $('form-comment').toQueryString(),
			onComplete: function(response) {
				var json = GigaFlat.parseJson(response);
				if (json.success) {
					var html = json.html + $('comments').innerHTML;
					$('comments').setHTML(html);
					$('comment').value = '';
				} else {
					alert(json.error);
				}
				button.disabled = false;
			}
		}).request();
		button.disabled = true;
	},
	deleteComment: function(id) {
		new Ajax('/download.php', {
			method: 'post',
			postBody: {'deleteComment' : id},
			onComplete: function(response) {
				var json = GigaFlat.parseJson(response);
				if (json.success) {
					$('comment-'+id).remove();
				} else {
					alert(json.error);
				}
			}
		}).request();
	},
	numberFormat: function(number, decimals, dec_point, thousands_sep) {
	  	var exponent = "";
	  	var numberstr = number.toString ();
	  	var eindex = numberstr.indexOf ("e");
	  	if (eindex > -1) {
	    	exponent = numberstr.substring (eindex);
	    	number = parseFloat (numberstr.substring (0, eindex));
	  	}
	  	if (decimals != null) {
	    	var temp = Math.pow (10, decimals);
	    	number = Math.round (number * temp) / temp;
	  	}
	  	var sign = number < 0 ? "-" : "";
	  	var integer = (number > 0 ? Math.floor (number) : Math.abs (Math.ceil (number))).toString ();
	  	var fractional = number.toString ().substring (integer.length + sign.length);
	  	dec_point = dec_point != null ? dec_point : ".";
	  	fractional = decimals != null && decimals > 0 || fractional.length > 1 ? (dec_point + fractional.substring (1)) : "";
	  	if (decimals != null && decimals > 0) {
	    	for (i = fractional.length - 1, z = decimals; i < z; ++i)
	    		fractional += "0";
	  	}
	  	thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ? thousands_sep : null;
	  	if (thousands_sep != null && thousands_sep != "") {
			for (i = integer.length - 3; i > 0; i -= 3)
	      		integer = integer.substring (0 , i) + thousands_sep + integer.substring (i);
	  	}
		return sign + integer + fractional + exponent;
	},
	submitReport: function() {
		new Ajax('/download.php', {
			method: 'post',
			postBody: $('form-report-download').toQueryString(),
			onComplete: function(response) {
				var json = GigaFlat.parseJson(response);
				if (json.success) {
					$('report-message').value = '';
					Layer.hide();
				} else {
					alert(json.error);
				}
			}
		}).request();
	}
}