function getSegundos(dato){
	hh = Math.floor(dato / 10000);
	mm = Math.floor((dato / 100) - hh * 100);
	ss = Math.floor(dato % 100);
	return 3600 * hh + 60 * mm + ss;
}

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());
	var deltacliente = ahoracliente - horainiciocliente;
	if (deltacliente < 0)
		deltacliente += 86400; //24 * 60 * 60;
		
	var segsahoraserver = getSegundos(ahoraserver);
	var segshoracomienzo = getSegundos(horacomienzo);
	deltaget = segsahoraserver - segshoracomienzo;
	if (deltaget < 0 &&
	    segsahoraserver < 3600 && segshoracomienzo > 82800) //son menos de la 1 y empezo despues de las 23
		//Cambio de dia
		deltaget += 86400;
	//else
		//Todavia no empezo
		
	var tiempojuego = deltaget + deltacliente;

	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;
		}
	}
}

function Popup(url,name,width,height,resize,scroll) {
	var dialogWin = new Object();
	dialogWin.width = width;
	dialogWin.height = height;
	dialogWin.left = (screen.width - dialogWin.width) / 2;
	dialogWin.top = (screen.height - dialogWin.height) / 2;
	var attr = 'left=' + dialogWin.left + ',top=' + dialogWin.top + ',resizable=' + resize + ',width=' + dialogWin.width + ',height=' + dialogWin.height + ',scrollbars=' + scroll + ',menubar=no,location=no,toolbar=no,status=no,directories=no';
	window.open(url,name,attr);
}
