function getSegundos(dato){
	fecha = Math.floor(dato / 1000000);
	fecha2 = Math.floor(dato / 10000);
	hh = Math.floor((dato / 10000) - fecha * 100);
	mm = Math.floor((dato / 100) - fecha2 * 100);
	ss = Math.floor(dato % 100);
											
	return 3600 * hh + 60 * mm + ss;
}
							
function getDia(dato){
	fecha = Math.floor(dato / 100000000);
	dia = Math.floor((dato / 1000000) - fecha * 100);
	return dia;
}
							
function getFecha(dato){
	fecha = Math.floor(dato / 1000000);
	return fecha;
}

function getSegundosFecha(fecha) {
	return 3600 * fecha.getHours() + 60 * fecha.getMinutes() + fecha.getSeconds();
}

function setMinutos(tiempo){
	return Math.floor(tiempo / 60);
}
function setSegundos(tiempo){
	return tiempo % 60;
}

var horainiciocliente = getSegundosFecha(new Date());

function actualizarTiempo(ahoraserver,horacomienzo){
	var ahoracliente = getSegundosFecha(new Date());
										
	if (getDia(horacomienzo) == getDia(ahoraserver)) { 
		var tiempojuego = (getSegundos(ahoraserver) - getSegundos(horacomienzo)) + (ahoracliente - horainiciocliente);
	} else {
		var tiempojuego = (getSegundos(ahoraserver) + (getSegundos(getFecha(horacomienzo)+'240000') - getSegundos(horacomienzo))) + (ahoracliente - horainiciocliente);
	}
	return tiempojuego;
}

var contadorminutos = null; 

function escribeTiempo(tiemposerver,horainicio,eldiv) {
	contadorminutos = document.getElementById(eldiv);
	var transcurrido = actualizarTiempo(tiemposerver, horainicio);
	if (transcurrido >= 0) {
		minutos = setMinutos(transcurrido);
		segundos = setSegundos(transcurrido) <= 9 ? "0" + setSegundos(transcurrido) : setSegundos(transcurrido);
		if(minutos <= 55){
			contadorminutos.innerHTML = minutos + ":" + segundos;
			setTimeout('escribeTiempo(' + tiemposerver + ',' + horainicio + ',"' + eldiv + '")',1000);
		}else{
			contadorminutos.innerHTML= "-";
		}
	} else {
		contadorminutos.innerHTML= "--:--";
	}
}


function actualizarTiempoRefresh(lapso){
	var ahoracliente = getSegundosFecha(new Date());
	var tiempotranscurrido = lapso - (ahoracliente -  horainiciocliente);
	return tiempotranscurrido;
}

function refreshpagina(tiemporefresh,timer,eldiv) {
	contadorminutos = document.getElementById(eldiv);
	var transcurrido = actualizarTiempoRefresh(tiemporefresh);
	if (timer == 'on'){
		if (transcurrido >= 1) {
			minutos = setMinutos(transcurrido) <= 9 ? "0" + setMinutos(transcurrido) :setMinutos(transcurrido);
			segundos = setSegundos(transcurrido) <= 9 ? "0" + setSegundos(transcurrido) : setSegundos(transcurrido);
			if (minutos >= 1){
				contadorminutos.innerHTML = minutos + ":" + segundos;
			} else {
				contadorminutos.innerHTML = "00:" + segundos;
			}
			setTimeout('refreshpagina(' + tiemporefresh + ',"' + timer + '","' + eldiv + '")',1000);
		} else {
			window.location.href = window.location.pathname;
		}
	}
}

function refreshpaginasincontador(tiemporefresh,timer) {
	var transcurrido = actualizarTiempoRefresh(tiemporefresh);
	if (timer == 'on' && tiemporefresh > 0){
		if (transcurrido >= 1) {
			segundos = setSegundos(transcurrido) <= 9 ? "0" + setSegundos(transcurrido) : setSegundos(transcurrido);
			setTimeout('refreshpaginasincontador(' + tiemporefresh + ',"' + timer + '")',1000);
		} else {
			window.location.href = window.location.pathname;
		}
	}
}
