var tiempo= new Array();
sumSesion=0;
minSesion=0;
maxSesion=0;

function guardaTiempo(tiemSesion){
	tiempo[tiempo.length]= tiemSesion;
	return muestraCalculos();
}

function calculaMin(cuantos){
	inicio=tiempo.length-1;
	if (inicio == 0){
		return inicio;
	}
	ultimo=0;
	minSession=inicio;
	if (cuantos>0 && cuantos<=tiempo.length) ultimo=tiempo.length-cuantos;
	for (e=inicio-1; e >= ultimo; e--){
		if (tiempo[e] <  tiempo[minSession]){
			minSession=e;
		}
	}
	return minSession;
}

function calculaMax(cuantos){
	inicio=tiempo.length-1;
	if (inicio == 0){
		return inicio;
	}
	ultimo=0;
	maxSession=inicio;
	if (cuantos>0 && cuantos<=tiempo.length) ultimo=tiempo.length-cuantos;
	for (e=inicio-1; e >= ultimo; e--){
		if (tiempo[e] >  tiempo[maxSession]){
			maxSession=e;
		}
	}
	return maxSession;
}

function calculaAvg(cuantos,minSession,maxSession){
	sumSesion=0;
	if (tiempo.length<3) return 0;
	inicio=0;
	if (cuantos>tiempo.length) return 0; 
	if (cuantos>0 && cuantos<=tiempo.length) inicio=tiempo.length-cuantos;
	for (e=inicio; e <= tiempo.length-1; e++){
		if ((e != minSession) && (e != maxSession) ){
			sumSesion+=tiempo[e];
		}
	}
	if (cuantos > 0)
		return sumSesion / (cuantos-2);
	else
		return sumSesion / (tiempo.length-2);
}

function calculaDesviacion(avg, minSession, maxSession){
	if (avg == 0) return 0;
	return ((tiempo[minSession] + tiempo[maxSession]) / 2) - avg;
}

function formatDesvia(time) {
	signo='';
    time *= 1;
	if (time<0) {
		time *= -1;
		signo='-';
	}
	
        timer.time_segments[0] = lpad(Math.floor(time / 60000), 2, "0"); time %= 60000;
        timer.time_segments[1] = lpad(Math.floor(time / 1000), 2, "0"); time %= 1000;
        timer.time_segments[2] = lpad(Math.floor(time / 10), 2, "0", true);

        return signo  + timer.time_segments[0] + ":" + timer.time_segments[1] + "." + timer.time_segments[2];
      }

function muestraCalculos(){
	str="";  
	str+=  "<table cellspacing='0' >";
	str+="<tr height='25'>";
	str+=" <td width='68' class='titInfo'>de 5</td>";
	minSesion=calculaMin(5);
	maxSesion=calculaMax(5);
	avg=calculaAvg(5,minSesion,maxSesion);
	str+="<td width='68' class='prom'>"+ format_time(avg)+"</td>";
	str+="<td width='74' class='desvia'>"+ formatDesvia(calculaDesviacion(avg,minSesion,maxSesion))+"</td>";
	str+="<td width='95' class='mejor'>"+format_time(tiempo[minSesion])+"</td>";
	str+="<td width='86' class='peor'>"+format_time(tiempo[maxSesion])+"</td>";
	str+="</tr>";
	str+="<tr height='25'>";
	str+="<td class='titInfo' >de 12</td>";
	minSesion=calculaMin(12);
	maxSesion=calculaMax(12);
	avg=calculaAvg(12,minSesion,maxSesion);
	str+="<td class='prom'>"+ format_time(avg)+"</td>";
	str+="<td class='desvia'>"+ formatDesvia(calculaDesviacion(avg,minSesion,maxSesion))+"</td>";
	str+="<td class='mejor'>"+format_time(tiempo[minSesion])+"</td>";
	str+="<td class='peor'>"+format_time(tiempo[maxSesion])+"</td>";
	str+="</tr>";
	str+="<tr height='25'>";
	str+="<td class='titInfo'>Total</td>";
	minSesion=calculaMin(0);
	maxSesion=calculaMax(0);
	avg=calculaAvg(0,minSesion,maxSesion);
	str+="<td class='prom'>"+ format_time(avg)+"</td>";
	str+="<td class='desvia'>"+formatDesvia(calculaDesviacion(avg,minSesion,maxSesion))+"</td>";
	str+="<td class='mejor'>"+format_time(tiempo[minSesion])+"</td>";
	str+="<td class='peor'>"+format_time(tiempo[maxSesion])+"</td>";
	str+="</tr>";
	str+="</table>";
	return str;
}