
function agregarTableroFutbol(idPartido, codigoSolicitud)
{
    if (tableros != null)
    {
        objTablero = new tableroFutbol({ cc: { id: idPartido, valor: codigoSolicitud} });
    }
    tableros.push(objTablero);
}

/* objeto Tablero Futbol */
var tableroFutbol =
function(value, isActive) 
{
    this._value = value;
    this._estaActivo = isActive || true;
    this._cambioEncuentro = false;
    this._cambioExtras = false;
    this._cambioFormaciones = false;
    this._tieneTiempo = true;

    /* TAG A ACTUALIZAR */
    if (this._value.cc.hasOwnProperty('tipoTablero')) 
    {
        this.TAG_RESULTADOS = this._value.cc.tipoTablero + "res";
		this.TAG_RESULTADOS_PENALES = this._value.cc.tipoTablero + "resp";
        this.TAG_INCIDENCIA = this._value.cc.tipoTablero + "i";
        this.TAG_ESTADO = this._value.cc.tipoTablero + "st";
        this.TAG_PERIODO = this._value.cc.tipoTablero + "p";
        this.TAG_TIEMPO = this._value.cc.tipoTablero + "t";
        this.TAG_RESUMEN = this._value.cc.tipoTablero + "resumen";
    }
    else 
    {
        this.TAG_RESULTADOS = "res";
		this.TAG_RESULTADOS_PENALES = "resp";
        this.TAG_INCIDENCIA = "i";
        this.TAG_ESTADO = "st";
        this.TAG_PERIODO = "p";
        this.TAG_TIEMPO = "t";
        this.TAG_RESUMEN = "resumen";
    }

    /* ******************** */


    this.setValue = function(valorActualizado) 
    {
        try 
        {
            if (valorActualizado.hasOwnProperty('cc')) { this._value.cc = valorActualizado.cc };
            if (valorActualizado.hasOwnProperty('encuentro')) { this._value.encuentro = valorActualizado.encuentro; this._cambioEncuentro = true; } else { this._cambioEncuentro = false; };
            if (valorActualizado.hasOwnProperty('ins')) { this._value.ins = valorActualizado.ins; this._cambioEncuentro = true; } else { this._cambioEncuentro = false; };
            if (valorActualizado.hasOwnProperty('fors')) { this._value.fors = valorActualizado.fors; this._cambioFormaciones = true; } else { this._cambioFormaciones = false; };
            if (valorActualizado.hasOwnProperty('extras')) { this._value.tablero = valorActualizado.extras; this._cambioExtras = true; } else { this._cambioExtras = false; };
        }
        catch (err) 
        {
            if (isDebugging) { showError('tableroFutbol.setValue', err) };
        }
    }
    this.cambiarActivo = function(isActive) 
    {
        try 
        {
            this._estaActivo = isActive;
        }
        catch (err) 
        {
            if (isDebugging) { showError('tableroFutbol.estaActivo', err) };
        }
    }
    this.tieneTiempo = function() 
    {
        return this._tieneTiempo;
    }

    this.estaActivo = function() 
    {
        return this._estaActivo;
    }
    
    this.actualizarResultado = function() 
    {
        try 
        {
            var local, visitante;
            if ((this._value.encuentro.local.id != undefined) && (this._value.encuentro.visitante.id))
			{
                this.actualizarResultado90Minutos();            
                if (this._value.encuentro.tiempo == "5")
                {
                    this.actualizarResultadoPenales();                        
                }
            }
        }
        catch (err) 
        {
            if (isDebugging) { showError('tableroFutbol.actualizarResultado', err) };
        }

    }

    this.actualizarResultado90Minutos = function()
    {
        var local = $("#" + this.TAG_RESULTADOS + this._value.encuentro.local.id);
        var visitante = $("#" + this.TAG_RESULTADOS + this._value.encuentro.visitante.id);

        if ((local.length) && (visitante.length))
        {
            if (this._value.encuentro.estado == "1")
            {
                local.html("-");
                visitante.html("-");
            }
			else
			{
				local.html(this._value.encuentro.local.goles);
				visitante.html(this._value.encuentro.visitante.goles);

				if((this._value.encuentro.local.goles == "") || (this._value.encuentro.local.goles == "NaN"))
				{
        	        	  local.html("0");
				}
	
				if((this._value.encuentro.visitante.goles == "") || (this._value.encuentro.visitante.goles == "NaN"))
				{
        	        	  visitante.html("0");
				}
			}
        }                    
    }
    
    this.actualizarResultadoPenales = function()
    {
        var local = $("#" + this.TAG_RESULTADOS_PENALES + this._value.encuentro.local.id);
        var visitante = $("#" + this.TAG_RESULTADOS_PENALES + this._value.encuentro.visitante.id);
        if ((local.length) && (visitante.length))
        {
			var golesPenalLocal = ((this._value.encuentro.local.golesPenal == undefined) ? 0 : this._value.encuentro.local.golesPenal);
			var golesPenalVisitante = ((this._value.encuentro.visitante.golesPenal == undefined) ? 0 : this._value.encuentro.visitante.golesPenal);
			this.actualizarGolesPenal(local, golesPenalLocal);
			this.actualizarGolesPenal(visitante, golesPenalVisitante);
        }        
    }

	this.actualizarGolesPenal = function(obj, goles)
	{
	    if ($("#resultadoPenales", obj).length) 
	    {
	        $(obj).removeClass("hidePenales");
	        if ($("#e" + this._value.encuentro.id).length)
	        {
	            if (!($("#e" + this._value.encuentro.id).attr("class").indexOf("conPenales") > 0))
	            {
	                $("#e" + this._value.encuentro.id).addClass("conPenales");
	            }
	        }
		    $("#resultadoPenales", obj).html(goles);
	    }	
	}

    this.actualizarEstado = function() 
    {
        try 
        {
            var estado;
            if (this._value.encuentro.estado != undefined) 
            {
                estado = $("#" + this.TAG_ESTADO + this._value.cc.id);
                if (estado.length) 
                {
                    switch (this._value.encuentro.estado) 
                    {
                        case "1":
                            estado.html(this.ESTADO_SIN_COMIENZO);
                            break;
                        case "2":
                            estado.html(this.ESTADO_EN_JUEGO);
                            break;
                        case "3":
                            estado.html(this.ESTADO_FINALIZADO);
                            this._estaActivo = false;
                            break;
                        case "4":
                            estado.html(this.ESTADO_SUSPENDIDO);
                            break;
                        case "5":
                            estado.html(this.ESTADO_ENTRETIEMPO);
                            break;
                    }
                }
            }
        }
        catch (err) 
        {
            if (isDebugging) { showError('tableroFutbol.actualizarEstado', err) };
        }

    }
    this.actualizarPeriodo = function() 
    {
        try 
        {
            if (this._value.encuentro.tiempo != undefined) 
            {
                var oPeriodo = $("#" + this.TAG_PERIODO + this._value.cc.id);
                if (oPeriodo.length) 
				{
                    if (this._value.encuentro.estado == "2") 
					{
						oPeriodo.html("");
						switch (this._value.encuentro.tiempo) 
						{
							case "1":
								oPeriodo.html(this.PRIMER_TIEMPO);
								break;
							case "2":
								oPeriodo.html(this.SEGUNDO_TIEMPO);
								break;
							case "3":
								oPeriodo.html(this.PRIMER_TIEMPO_SUPLEMENTARIO);
								break;
							case "4":
								oPeriodo.html(this.SEGUNDO_TIEMPO_SUPLEMENTARIO);
								break;
							case "5":
								oPeriodo.html(this.PENALES);
								break;
						}
                    } 
					else 
					{
						switch (this._value.encuentro.estado) 
						{
							case "1":
								oPeriodo.html(this.ESTADO_SIN_COMIENZO);
								break;
							case "3":
							    var oTiempo = $("#" + this.TAG_TIEMPO + this._value.cc.id);
							    if (oTiempo.length)
							    {
								    oTiempo.html(this.ESTADO_FINALIZADO);
								}
								oPeriodo.html("");
								this._estaActivo = false;
								break;
							case "4":
								oPeriodo.html(this.ESTADO_SUSPENDIDO);
								break;
							case "5":
								oPeriodo.html(this.ESTADO_ENTRETIEMPO);
								break;
						}
                    }
                }
            }

        } 
        catch (err) 
        {
            if (isDebugging) { showError('tableroFutbol.actualizarPeriodo', err) };
        }

    }
    
    this.actualizarIncidencia = function()
    {
        try
        {
            if (this._value.ins[0].inc.id != undefined)
            {
                var oIncidencia = $("#" + this.TAG_INCIDENCIA + this._value.cc.id);
                if (oIncidencia.length)
                {
                    var clase = "";
                    switch (this._value.ins[0].inc.tp.toLowerCase())
                    {
                        case "amarilla":
                            clase = "amarilla";
                            break;
                        case "roja":
                            clase = "roja";
                            break;
                        case "gol":
                            clase = "gol";
                            break;
                        case "cambio":
                            clase = "cambio";
                            break;
                        case "lesion":
                            clase = "lesion";
                        default:
                            clase = "fallo";
                            break;
                    }
                    oTextoIncidencia = $("#textoIncidencia", oIncidencia);
                    oTextoIncidencia.html(this._value.ins[0].inc.cm);
                    oIncidencia.addClass(clase).slideDown('slow').delay(7000).hide('fast', function(){$(this).removeClass(clase);});
                }
            }             
        }
        catch (err)
        {
            if (isDebugging) { showError('tableroFutbol.actualizarIncidencia', err)};
        }
    }
    
    this.isTypeOf = function(value) 
    {
        var _isTypeOf = false;
        if (value == "tableroFutbol")
            _isTypeOf = !_isTypeOf;
        return _isTypeOf;
    }
    this.PRIMER_TIEMPO = "1er tiempo";
    this.SEGUNDO_TIEMPO = "2do tiempo";
    this.PRIMER_TIEMPO_SUPLEMENTARIO = "1er tiempo suplementario";
    this.SEGUNDO_TIEMPO_SUPLEMENTARIO = "2do tiempo suplementario";
    this.PENALES = "Penales";
    this.ESTADO_SIN_COMIENZO = "Sin comienzo";
    this.ESTADO_EN_JUEGO = "En juego";
    this.ESTADO_FINALIZADO = "Finalizado";
    this.ESTADO_SUSPENDIDO = "Suspendido";
    this.ESTADO_ENTRETIEMPO = "Entretiempo";

    this.calcularDelta = function() 
    {
        var horaPC = new Date();
        var toReturn = 0;

        if (fechaServidor != null) 
        {
            toReturn = (horaPC - fechaServidor) / 1000;
        }
        return (toReturn);
    }

    this.tiempoTranscurrido = function(horaInicio) 
    {
        var elReloj = new Date();
        var deltaTiempo = this.calcularDelta();

        if (horaInicio != null) 
        {
            // Armo el dia con la horacomienzo
            var relojComienzo = new Date(elReloj.getFullYear(), elReloj.getMonth(), elReloj.getDate(), Number(horaInicio.substr(0, 2)), Number(horaInicio.substr(3, 2)), 0);
            if (elReloj > relojComienzo) 
            {
                var miliSegundos = (elReloj - deltaTiempo) - relojComienzo;
                var Hora = Math.floor(miliSegundos / (3600000)); // Horas
                var Minuto = Math.floor(miliSegundos / (60000)) - (Hora * 60); // Minutos
                var Segundo = Math.floor(miliSegundos / (1000)) - (Minuto * 60) - (Hora * 3600); // Segundos

                if (Hora != 0 && Hora > 0)
                    Hora += ":";
                else
                    Hora = "";

                if (typeof (Segundo) == "undefined")
                    Segundo = SegundoAnt;
                else
                    var SegundoAnt = Segundo;

                if (typeof (Minuto) == "undefined")
                    Minuto = MinutoAnt;
                else
                    var MinutoAnt = Minuto;

                if (Minuto < 10)
                    Minuto = "0" + Minuto.toString();

                if (Segundo < 10)
                    Segundo = "0" + Segundo.toString();

                return Minuto + ":" + Segundo;
            }
            else 
            {
                return "";
            }
        }
        else 
        {
            return "";
        }
    }
    this.actualizarReloj = function() 
    {
        this.renderReloj();
    }

    this.renderReloj = function() 
    {
        var oTiempo;
        try 
        {
            oTiempo = $("#" + this.TAG_TIEMPO + this._value.cc.id);
            if (oTiempo.length) 
            {
                if (this._value.encuentro.estado == "2"  && this._value.encuentro.tiempo != "5")
				{
                    oTiempo.css("display", "inline");
                    oTiempo.html(this.tiempoTranscurrido(this._value.encuentro.inicio_tiempo));
                }
                else 
                {
                    if (this._value.encuentro.estado != "3")
                    {
                        oTiempo.html(" ");
                    }
                    this._tieneTiempo = false;                    
                }
            }
            else 
            {
                    this._tieneTiempo = false;
            }
            //oTiempo = null;
        }
        catch (err) 
        {
            if (isDebugging) { showError('tableroFutbol.renderReloj', err) };
            this._tieneTiempo = false;
        }

    }

    // implementa diferente para cada tipo de tablero: tennis, futbol
    this.execute = function() 
    {
        try 
        {
            if (this._cambioEncuentro) 
            {
                this.actualizarEstado();
                this.actualizarPeriodo();
                this.actualizarResultado();
            }
            this.actualizarIncidencia();            
        }
        catch (err) 
        {
            if (isDebugging) { showError('tableroFutbol.execute', err) };
        }
    }
};
