// Se deshabilitan todos los divs y se habilita el que se va a mostrar
function habilitar_opcion(div,path) {
	var refresh = $("#busqfecha").data("refresh_inicio");
	clearInterval(refresh);
	$("#busqfecha").data("refresh_inicio",0);
	var refresh = $("#busqfecha").data("refresh_loteria");
	clearInterval(refresh);
	$("#busqfecha").data("refresh_loteria",0);
	var refresh = $("#div_tablero_vivo").data("refresh_vivo");
	clearInterval(refresh);
	$("#div_tablero_vivo").data("refresh_vivo",0);
	var refresh = $("#div_tablero_vivo").data("refresh_sorteo");
	clearInterval(refresh);
	$("#div_tablero_vivo").data("refresh_sorteo",0);

	var id = $(div);
	$(".opciones").hide();
	$(id).show();
	$(id).html('<IFRAME width="100%" height="100%" id="ifr" name="ifr"  allowTransparency="true" FRAMEBORDER="0" SRC="'+path+'" scrolling="no" topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0" marginwidth="0" marginheight="0"></IFRAME>')

}

// ***************************************************************************************
//                    TABLERO EN VIVO
// ***************************************************************************************

function traer_tablero_vivo() {
	$("#div_quinielas").hide();
	$("#div_estadisticas").hide();
	$("#div_tablero_vivo").show();
	var refresh = $("#busqfecha").data("refresh_inicio");
	clearInterval(refresh);
	$("#busqfecha").data("refresh_inicio",0);
	var refresh = $("#busqfecha").data("refresh_loteria");
	clearInterval(refresh);
	$("#busqfecha").data("refresh_loteria",0);

    if($("#div_tablero_vivo").data("refresh_vivo")==0)
    {
		var refresh = setInterval("busq_sorteando()",15000);
		$("#div_tablero_vivo").data("refresh_vivo",refresh);
	}

    busq_sorteando();
}

function busq_sorteando() {

	$.ajax({
		beforeSend:Procesando,
		url: 'busqsorteando.php',
		cache: false,
		type: "POST",
		success: function(datos){
			$("#div_sorteando").html(datos);
		},
        complete: function(objeto, exito){
			$("#msg_proc").fadeOut("slow",function() {

				var sorteando = $('.idsorteando').attr("value");
				if(sorteando==1)
				{					cargar_sorteo_vivo()
				} else
				{
					cargar_horarios()
				}
			});
        },
		error: function(objeto, quepaso, otroobj){
            alert("Sin conexion con el servidor");
        }

	});
}

function cargar_sorteo_vivo() {
	$("#div_tablero_vivo").css("height", 350+"px");

    if($("#div_tablero_vivo").data("refresh_sorteo")==0)
    {
		var refresh = setInterval("cargar_sorteo_vivo()",50000);
		$("#div_tablero_vivo").data("refresh_sorteo",refresh);
	}

	//$("#div_tablero_vivo").html('<IFRAME width="100%" height="350" id="ifr" name="ifr"  allowTransparency="true" FRAMEBORDER="0" SRC="sorteando-vivi.htm" scrolling="auto" topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0" marginwidth="0" marginheight="0"></IFRAME>')
	$("#div_tablero_vivo").html('<IFRAME width="100%" height="350" id="ifr" name="ifr"  allowTransparency="true" FRAMEBORDER="0" SRC="http://www.dejugadas.com/quinielas/sorteando-vivi.htm" scrolling="auto" topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0" marginwidth="0" marginheight="0"></IFRAME>')
}

function cargar_horarios() {
	$("#div_tablero_vivo").css("height", 200+"px");

	var refresh = $("#div_tablero_vivo").data("refresh_sorteo");
	clearInterval(refresh);
    $("#div_tablero_vivo").data("refresh_sorteo",0);

	$("#div_tablero_vivo").load("horarios.php");
}




// ***************************************************************************************
//                    INICIO CABEZAS
// ***************************************************************************************

function traer_inicio_cabezas() {
	$("#div_quinielas").show();
	$("#div_estadisticas").show();
	$("#div_tablero_vivo").hide();
	$("#div_quinielas").css("height", 700+"px");

	var refresh = $("#div_tablero_vivo").data("refresh_vivo");
	clearInterval(refresh);
    $("#div_tablero_vivo").data("refresh_vivo",0);
	var refresh = $("#div_tablero_vivo").data("refresh_sorteo");
	clearInterval(refresh);
    $("#div_tablero_vivo").data("refresh_sorteo",0);
	var refresh = $("#busqfecha").data("refresh_loteria");
	clearInterval(refresh);
    $("#busqfecha").data("refresh_loteria",0);

    if($("#busqfecha").data("refresh_inicio")==0)
    {
		var refresh = setInterval("actualizar_pizarra()",50000);
		$("#busqfecha").data("refresh_inicio",refresh);
	}

    actualizar_pizarra();
    traer_estadisticas();
}

function actualizar_pizarra() {

	$.ajax({
		beforeSend:Procesando,
		url: 'busqfecha.php',
		cache: false,
		type: "POST",
		success: function(datos){
			$("#busqfecha").html(datos);
		},
        complete: function(objeto, exito){
			$("#msg_proc").fadeOut("slow",function() {
			    var textoFecha = $("#fecha").attr("value");
			    var nuevafecha = convertir_fecha(textoFecha);
				traer_pizarra("A",nuevafecha);
				$("#fecha").data("opcion","cabeza");
			});
        },
		error: function(objeto, quepaso, otroobj){
            alert("Sin conexion con el servidor");
        }

	});
}

function traer_pizarra(opcion,fecha) {

	$.ajax({
		//beforeSend:,
		url: 'datospizarra.php',
		cache: false,
		type: "POST",
		data: "fecha="+fecha,
		success: function(datos){
			$("#div_pizarra").html(datos);
		},
        complete: function(objeto, exito){
        },
		error: function(objeto, quepaso, otroobj){
            alert("Sin conexion con el servidor");
        }

	});
}

function pizarra_loteria(celda) {
	$("#div_estadisticas").hide();
	$("#div_quinielas").css("height", 380+"px");

	var refresh = $("#busqfecha").data("refresh_inicio");
	clearInterval(refresh);
    $("#busqfecha").data("refresh_inicio",0);

	$("#id_comboloterias").css("display", "inline");

    $("#fecha").data("opcion","pizarra");

    var textoFecha = $("#fecha").attr("value");
    var fecha = convertir_fecha(textoFecha);

	var loteria = $(celda).parent().find('.idloteria').attr("value");
	$("#id_loteria option[value="+loteria+"]").attr("selected",true);

    if($("#busqfecha").data("refresh_loteria")==0)
    {
		var refresh = setInterval("pizarra_loteria_refresh()",30000);
		$("#busqfecha").data("refresh_loteria",refresh);
	}

	$.ajax({
		beforeSend:Procesando,
		url: 'datospizarra_loteria.php',
		cache: false,
		type: "POST",
		data: "fecha="+fecha+"&loteria="+loteria,
		success: function(datos){
			$("#div_pizarra").html(datos);
		},
        complete: function(objeto, exito){
			$("#msg_proc").fadeOut("slow",function() {
            });
        },
		error: function(objeto, quepaso, otroobj){
            alert("Sin conexion con el servidor");
        }

	});
}

function pizarra_loteria_refresh() {

	var loteria = $("#id_loteria option:selected").attr("value");
    var textoFecha = $("#fecha").attr("value");
    var fecha = convertir_fecha(textoFecha);

	$.ajax({
		beforeSend:Procesando,
		url: 'datospizarra_loteria.php',
		cache: false,
		type: "POST",
		data: "fecha="+fecha+"&loteria="+loteria,
		success: function(datos){
			$("#div_pizarra").html(datos);
		},
        complete: function(objeto, exito){
			$("#msg_proc").fadeOut("slow",function() {
            });
        },
		error: function(objeto, quepaso, otroobj){
            alert("Sin conexion con el servidor");
        }

	});
}

function traer_pizarra_loteria(loteria,fecha) {

	$.ajax({
		beforeSend:Procesando,
		url: 'datospizarra_loteria.php',
		cache: false,
		type: "POST",
		data: "fecha="+fecha+"&loteria="+loteria,
		success: function(datos){
			$("#div_pizarra").html(datos);
		},
        complete: function(objeto, exito){
			$("#msg_proc").fadeOut("slow",function() {
            });
        },
		error: function(objeto, quepaso, otroobj){
            alert("Sin conexion con el servidor");
        }

	});
}

function combo_traerpizarra(celda) {
	/* --- Determinar la loteria seleccionada en el combo --- */
	var loteria = $("#id_loteria option:selected").attr("value");

    var textoFecha = $("#fecha").attr("value");
    var fecha = convertir_fecha(textoFecha);

	traer_pizarra_loteria(loteria,fecha);

}



// ***************************************************************************************
//                    ESTADISTICAS ANTERIORES
// ***************************************************************************************

function traer_estadisticas() {

	$.ajax({
		//beforeSend:,
		url: 'estadisticas.php',
		cache: false,
		type: "POST",
		//data: "fecha="+fecha,
		success: function(datos){
			$("#div_estadisticas").html(datos);
		},
        complete: function(objeto, exito){
        },
		error: function(objeto, quepaso, otroobj){
            alert("Sin conexion con el servidor");
        }

	});
}

function traerdatos_nro(loteriaid) {

	var numero = $("#id_numero").attr("value");
    if (numero == ''){return;}

	$.ajax({
		//beforeSend:,
		url: 'datosnro.php',
		cache: false,
		type: "POST",
		data: "loteriaid="+loteriaid+"&numero="+numero,
		success: function(datos){
			$("#div_datosnro").html(datos);
		},
        complete: function(objeto, exito){
        },
		error: function(objeto, quepaso, otroobj){
            alert("Sin conexion con el servidor");
        }

	});
}


// ***************************************************************************************
//                    NUEVAS ESTADISTICAS
// ***************************************************************************************

function traer_nueva_estad(celda) {
	var refresh = $("#busqfecha").data("refresh_inicio");
	clearInterval(refresh);
    $("#busqfecha").data("refresh_inicio",0);

	$("#div_quinielas").hide();

    var loteriaid = $(celda).parent().find('.idloteria').attr("value");

	$.ajax({
		beforeSend:Procesando,
		url: 'estadisticas_new.php',
		cache: false,
		type: "POST",
		data: "loteriaid="+loteriaid,
		success: function(datos){
			$("#div_estadisticas").html(datos);
		},
        complete: function(objeto, exito){
			$("#msg_proc").fadeOut("slow",function() {
            });
        },
		error: function(objeto, quepaso, otroobj){
            alert("Sin conexion con el servidor");
        }

	});
}


// ***************************************************************************************
//                    VARIOS
// ***************************************************************************************

function convertir_fecha(fecha_in) {
	var pos1 = fecha_in.indexOf("/",0);
	var pos2 = fecha_in.indexOf("/",pos1+1);
	var dia = fecha_in.substring(0,pos1);
	var mes = fecha_in.substring(pos1+1,pos2);
	var anio = fecha_in.substring(pos2+1,pos2+5);
	if(parseInt(dia)<10){dia = "0"+dia;};
	if(parseInt(mes)<10){mes = "0"+mes;};
	var fecha_out = anio+"/"+mes+"/"+dia;

	return fecha_out;
}

function Procesando()
{
	//Centra el mensaje
	$("#msg_proc").css("left",(($(window).width()/2) - ($("#msg_proc").width()/2)) + "px");
	$("#msg_proc").css("top",(($(window).height()/2) - ($("#msg_proc").height()/2)) + "px");
	// Mostrar el mensaje
	$("#msg_proc").css("display", "inline");
}

/*
function actualizar_tablero_vivo() {

	$.ajax({
		beforeSend:Procesando,
		url: 'busqvivo.php',
		cache: false,
		type: "POST",
		success: function(datos){
			$("#div_tablero_vivo").html(datos);
		},
        complete: function(objeto, exito){
			$("#msg_proc").fadeOut("slow",function() {
				$("#div_quinielas").hide();
				$("#div_estadisticas").hide();
				$("#div_tablero_vivo").show();
			});
        },
		error: function(objeto, quepaso, otroobj){
            alert("Sin conexion con el servidor");
        }

	});
}
*/
