/*
	Invoca la funzione PHP che restituisce i dati relativi alla progressione dell'upload.
	Cristian Zuddas, 2007-12-04
	
	Params:
		v_uber_upload__url		URL assoluto e relativo del file "v_uber_upload.php"
		session_id				ID di sessione dell'upload (temp_sid)
		tmp_dir					path completo della cartella tmp dell'upload
*/
function uu_get_upload_status(v_uber_upload__url, session_id, tmp_dir) {
	document.getElementById('uu_progress_upload').style.display = '';
	//window.location = v_uber_upload__url+'?action=getstatus&tmp_dir='+tmp_dir+'&temp_sid='+session_id;
	$.ajax({
		type: "POST",
		url: v_uber_upload__url,
		data: 'action=getstatus&tmp_dir='+tmp_dir+'&temp_sid='+session_id,
		dataType: "txt",
		success:	function(oResponse) {
						if (oResponse.length>0) {
							var oResponse_arr = oResponse.split('|');
							
							if (oResponse_arr.length>0) {
								if (parseInt(oResponse_arr[0])>0) {
									document.getElementById('uu_upload_bytes').innerHTML = uu__filesize_h(parseInt(oResponse_arr[1]), 1)+' di '+uu__filesize_h(parseInt(oResponse_arr[0]), 1);
									document.getElementById('uu_upload_percent').innerHTML = Math.floor((parseInt(oResponse_arr[1])*100)/parseInt(oResponse_arr[0]))+'%';
									document.getElementById('uu_upload_remain').innerHTML = uu__time2text(parseInt(oResponse_arr[2]));
									document.getElementById('uu_upload_bitrate').innerHTML = uu__filesize_h(parseInt(oResponse_arr[1])/parseInt(oResponse_arr[3]))+'/secondo';
								}
							}
						}
					},
		error:		function() {},
		complete:	function() {
						setTimeout("uu_get_upload_status('"+v_uber_upload__url+"', '"+session_id+"', '"+tmp_dir+"');", 300);
					}
	});
}

/*
	Restituisce la dimensione di un file in formatto leggibile.
	
	Autore:	Cristian Zuddas
	Vers.:	1.0
	Data: 	2007-10-03
*/
function uu__filesize_h(size, dec) {
	var sizes = new Array('B', 'KB', 'MB', 'GB');
	var i = 0;
	
	while (size>=1024 && (i<sizes.length-1)) {
		size /= 1024;
		i++;
	}
	
	return uu__round_prec(size, dec).toString()+' '+sizes[i];
}

/*
	Simula un arrotondamento di un numero decimale ad un massimo di cifre decimali
	(non esiste una funzione JS che lo faccia nativamente; le funzioni JS arrotondano
	soltanto ad un numero intero). Si comporta come la funzione "round()" del PHP.
	
	Autore:	Cristian Zuddas
	Vers.:	1.0
	Data: 	2007-10-03
*/
function uu__round_prec(val, precision) {
	if (val>0) {
		var val_parts = val.toString().split('.');
		if (val_parts.length==2 && parseInt(val_parts[0])>0) {		// se sono presenti delle cifre decimali
			var val_rounded = val_parts[0];
			var dec_digits = '';
			
			if (precision>0) {
				dec_digits = val_parts[1].substr(0, precision);
				
				/*
					controlla la cifra decimale immediatamente successiva a quella alla quale
					il numero e' stato tgliato. se e' maggiore o uguale a 5 arrotonda i
					decimali per eccesso
				*/
				if (val_parts[1].length>precision && parseInt(val_parts[1].substr(precision, 1))>=5) {
					dec_digits = parseInt(dec_digits)+1;
				}
			}
			else {		// controlla la prima cifra decimale. se e' maggiore o uguale a 5 arrotonda il numero intero per eccesso
				if (parseInt(val_parts[1].substr(0, 1))>=5) {
					val_rounded = (parseInt(val_rounded)+1).toString();
				}
			}
			
			if (dec_digits>0)	{	return parseFloat(val_rounded+'.'+dec_digits);	}
			else				{	return parseFloat(val_rounded);					}
		}
		else {return val;}				// in tal caso era un numero intero
	}
	else {return val;}
}

/*
	Converte un orario passato come timestamp (numero intero di secondi) in una forma
	testuale leggibile.
	Cristian Zuddas, 2007-12-04
	
	Parametri:
		timestamp	INT		timestamp che rappresenta l'orario da convertire (obbligatorio)
		labels		ARRAY	etichette testuali tradotte nella lingua desiderata. Se non passato,
							viene usato l'italiano. E' un array bidimensionale che contiene le
							etichette in forma singolare e plurale.
	
	Esempio: 25447   --->   "7 ore, 4 minuti, 7 secondi"
*/
function uu__time2text(timestamp, labels) {
	if (typeof labels!='array' || labels.length==0) {
		labels = new Array();
		labels[0] = new Array('ora', 'ore');
		labels[1] = new Array('minuto', 'minuti');
		labels[2] = new Array('secondo', 'secondi');
	}
	
	if (typeof timestamp=='number' && timestamp>0) {
		var text = '';
		if (timestamp>=3600) {		// ore
			var this_val = parseInt(timestamp/3600);
			text = this_val.toString()+' ';
			
			if (this_val==1)	{	text += labels[0][0];	}		// singolare
			else				{	text += labels[0][1];	}		// plurale
			
			timestamp = timestamp % 3600;
		}
		
		if (timestamp>=60) {		// minuti
			if (text.length>0) {
				text += ', ';
			}
			
			var this_val = parseInt(timestamp/60);
			text += this_val.toString()+' ';
			
			if (this_val==1)	{	text += labels[1][0];	}		// singolare
			else				{	text += labels[1][1];	}		// plurale
			
			timestamp = timestamp % 60;
		}
		
		if (timestamp>0) {		// secondi
			if (text.length>0) {
				text += ', ';
			}
			
			text += timestamp.toString()+' ';
			
			if (timestamp==1)	{	text += labels[2][0];	}		// singolare
			else				{	text += labels[2][1];	}		// plurale
		}
		
		return text;
	}
	else {	return '0 '+labels[2][1];	}
}