﻿/* objeto Tablero Futbol */
var tableroFutbol =
function(value, isActive)
{
    this._value = value;
    this._estaActivo = isActive || true;
    this._cambioEncuentro = false;
    this._cambioIncidencias = 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_GAMECAST = this._value.cc.tipoTablero + "gamec";
        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_GAMECAST = "gamec";
        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._cambioIncidencias = true; } else { this._cambioIncidencias = false; };
            if (valorActualizado.hasOwnProperty('fors')) { this._value.fors = valorActualizado.fors; this._cambioFormaciones = true; } else { this._cambioFormaciones = false; };
            if (valorActualizado.hasOwnProperty('extras')) { this._value.extras = 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.actualizarGamecast = function()
    {
        try
        {
            var oGamecast = jQuery("#" + this.TAG_GAMECAST + this._value.cc.id)[0];
            if ((oGamecast != undefined) && (this._value.encuentro.linkeable != undefined))
            {
                if (this._value.encuentro.linkeable == "1")
                {
                    oGamecast.style.display = "block";
                }
                else
                {
                    oGamecast.style.display = "none";
                }
            }
        }
        catch (err)
        {
            if (isDebugging) { showError('tableroFutbol.actualizarGamecast', err) };
        }
    }
    this.actualizarIncidencias = function() 
    {
        try 
        {
            if (this._value.ins[0].inc.id != undefined)
            {
                var tipoIncidencia = ((this._value.ins[0].inc.tp == 'amarilla' || this._value.ins[0].inc.tp == 'roja')? 'tarjeta' : this._value.ins[0].inc.tp);
                var oIncidencia = jQuery("#" + this.TAG_INCIDENCIA + tipoIncidencia + this._value.cc.id + "_" + this._value.ins[0].inc.eq);
                if (oIncidencia.length)
                {
                    for (var i = 0; i < this._value.ins.length; i++)
                    {
                        var oItemIncidencia = jQuery("#" + this.TAG_INCIDENCIA + tipoIncidencia + this._value.cc.id + "_" + this._value.ins[0].inc.eq + "_" + this._value.ins[i].inc.id);
                        if (oItemIncidencia.length)
                        {
                            oItemIncidencia.html(this.renderIncidenciaTablero(i));                        
                        }
                        else
                        {
                            oIncidencia.append(this.obtenerIncidenciaTablero(i, tipoIncidencia));
                        }    
                    }
                }
            }
        }
        catch (err) 
        {
            if (isDebugging) 
            { 
                showError('tableroFutbol.actualizarIncidencias', err) 
            };
        }
    }

    this.obtenerIncidenciaTablero = function(nroIncidencia, tipoIncidencia)
    {
        var strRetorno = "<li";
        if (tipoIncidencia == 'tarjeta')
        {
            strRetorno = strRetorno + " class='" + this._value.ins[nroIncidencia].inc.tp + "'";
        }
        strRetorno = strRetorno + ">" + this.renderIncidenciaTablero(nroIncidencia) + "</li>";
        return strRetorno;
    }

    this.renderIncidenciaTablero = function(nroIncidencia)
    {
        return this._value.ins[nroIncidencia].inc.jn + this.obtenerTiempoIncidencia(nroIncidencia);
    }
    
    this.obtenerTiempoIncidencia = function(nroIncidencia)
    {
        var strRetorno = "";
        if (this._value.ins[nroIncidencia].inc.tm == "5" && this._value.ins[nroIncidencia].inc.tp == "gol")
        {
            strRetorno += " (DP)";
        }
        else
        {
            var tiempoAMostrar = this._value.ins[nroIncidencia].inc.tm + "T";
            if (this._value.ins[nroIncidencia].inc.tm == "3")
            {
                tiempoAMostrar = "1T sup";
            }
            if (this._value.ins[nroIncidencia].inc.tm == "4")
            {
                tiempoAMostrar = "2T sup";
            }
            strRetorno += " (" + this._value.ins[nroIncidencia].inc.mm + "' " + tiempoAMostrar + ")";
        }
        return strRetorno;
    }
    
    this.actualizarResumen = function() 
    {
        try
        {
            var oResumen = jQuery("#" + this.TAG_RESUMEN + this._value.cc.id);
            if ((oResumen != undefined) && (this._value.ins[0].inc.cm != undefined))
            {
                for (var i = 0; i < this._value.ins.length; i++)
                {
                    if (jQuery("#incidencia" + this._value.ins[i].inc.id).length)
                    {
                        jQuery("#incidencia" + this._value.ins[i].inc.id).html(this.actualizarIncidenciaResumen(i));                    
                    }
                    else
                    {
                        oResumen.prepend(this.obtenerIncidenciaResumen(i));
                    }
                }
            }
        }
        catch (err)
        {
            if (isDebugging)
            {
                showError('tableroFutbol.actualizarResumen', err);
            }
        }
    }

    this.obtenerIncidenciaResumen = function(nroIncidencia)
    {
        var id = this._value.ins[nroIncidencia].inc.id;
        var tipo = this._value.ins[nroIncidencia].inc.tp;
        var minutos = this._value.ins[nroIncidencia].inc.mm;
        var tiempo  = this._value.ins[nroIncidencia].inc.tm;
        var comentario = this._value.ins[nroIncidencia].inc.cm;    
        return "<li id='incidencia" + id + "'>" + this.renderIncidenciaResumen(tipo, tiempo, minutos, comentario) + "</li>";
    }

    this.actualizarIncidenciaResumen = function(nroIncidencia)
    {
        var tipo = this._value.ins[nroIncidencia].inc.tp;
        var minutos = this._value.ins[nroIncidencia].inc.mm;
        var tiempo  = this._value.ins[nroIncidencia].inc.tm;
        var comentario = this._value.ins[nroIncidencia].inc.cm;    
        return this.renderIncidenciaResumen(tipo, tiempo, minutos, comentario);
    }

    this.renderIncidenciaResumen = function(tipo, tiempo, minutos, comentario)
    {
        var strRender = "";
        if (tipo != "1erTiempo" && tipo != "entretiempo" && tipo != "2doTiempo" && tipo != "final")
        {
            var minutoAMostrar = "0";
            if (tiempo == "1" || tiempo == "2")
            {
                minutoAMostrar = (tiempo * minutos);
            }
            else if (tiempo == "3")
            {
                minutoAMostrar = parseInt(minutos) + 90;
            }
            else if (tiempo == "4")
            {
                minutoAMostrar = parseInt(minutos) + 105;
            }
            else if (tiempo == "5")
            {
                minutoAMostrar = "(P)";
            }
            
            strRender = strRender + "<b>" + minutoAMostrar + "</b> ";
        }
        strRender = strRender + this.obtenerImgIncidencia(tipo) + comentario;
        return strRender;
    }

    this.obtenerImgIncidencia = function(tipo)
    {
        var imgRetorno = "";
        switch(tipo)
        {
            case "gol":
                imgRetorno = '<img height="24" border="0" width="24" alt="Gol" title="Gol" src="/_ui/desktop/imgs/layout/iconos/golNuevo.gif" />';            
                break;
            case "amarilla":
                imgRetorno = '<img height="24" border="0" width="24" alt="Tarjeta amarilla" title="Tarjeta amarilla" src="/_ui/desktop/imgs/layout/iconos/amarillaNuevo.gif" />';
                break;
            case "roja":
                imgRetorno = '<img height="24" border="0" width="24" alt="Tarjeta roja" title="Tarjeta roja" src="/_ui/desktop/imgs/layout/iconos/rojaNuevo.gif" />';
                break;
            case "cambio":
                imgRetorno = '<img height="24" border="0" width="24" alt="Cambio" title="Cambio" src="/_ui/desktop/imgs/layout/iconos/cambioNuevo.gif" />';
                break;
        }
        return imgRetorno;
    }

    this.actualizarFormaciones = function() 
    {
        try 
        {
        }
        catch (err) 
        {
            if (isDebugging) { showError('tableroFutbol.actualizarFormaciones', err) };
        }
    }

    this.actualizarMarquesina = function() 
    {
        try 
        {
            //TODO
            var oMarquesina = jQuery("#" + "mq" + this._value.cc.id);
            if ((oMarquesina.length) && (this._value.extras.mq != undefined)) 
            {
                oMarquesina.html(this._value.extras.mq.texto);
            }
        }
        catch (err) 
        {
            if (isDebugging) { showError('tableroFutbol.actualizarMarquesina', err) };
        }
    }
    this.actualizarResultado = function() 
    {
        try 
        {
            var local, visitante, localPenales, visitantePenales;
            if ((this._value.encuentro.local.id != undefined) && (this._value.encuentro.visitante.id != undefined))
            {
                this.actualizarResultado90Minutos();            
                if (this._value.encuentro.tiempo == "5")
                {
                    this.actualizarResultadoPenales();
                }
            }
        }
        catch (err)
        {
            if (isDebugging) { showError('tableroFutbol.actualizarResultado', err) };
        }

    }
    
    this.actualizarResultado90Minutos = function()
    {
        var local = jQuery("#" + this.TAG_RESULTADOS + this._value.encuentro.local.id)[0];
        var visitante = jQuery("#" + this.TAG_RESULTADOS + this._value.encuentro.visitante.id)[0];

        if ((local != undefined) && (visitante != undefined))
        {
            if (this._value.encuentro.estado == "1")
            {
                local.innerHTML = "-";
                visitante.innerHTML = "-";
            }
            else
            {        
                local.innerHTML = this._value.encuentro.local.goles;
                visitante.innerHTML = this._value.encuentro.visitante.goles;


		if((this._value.encuentro.local.goles == "") || (this._value.encuentro.local.goles == "NaN"))
		{
                  local.innerHTML = 0; 		  
		}

		if((this._value.encuentro.visitante.goles == "") || (this._value.encuentro.visitante.goles == "NaN"))
		{
                  visitante.innerHTML = 0; 		  
		}

                if (this._value.encuentro.local.goles > this._value.encuentro.visitante.goles)
                {
                    jQuery("#" + this.TAG_RESULTADOS + this._value.encuentro.local.id).addClass("ganador");
                    jQuery("#" + this.TAG_RESULTADOS + this._value.encuentro.visitante.id).removeClass("ganador");
                }
                else if (parseInt(this._value.encuentro.local.goles) < parseInt(this._value.encuentro.visitante.goles))
                {

                    jQuery("#" + this.TAG_RESULTADOS + this._value.encuentro.visitante.id).addClass("ganador");
                    jQuery("#" + this.TAG_RESULTADOS + this._value.encuentro.local.id).removeClass("ganador");
                }
                else
                {
                    jQuery("#" + this.TAG_RESULTADOS + this._value.encuentro.visitante.id).removeClass("ganador");
                    jQuery("#" + this.TAG_RESULTADOS + this._value.encuentro.local.id).removeClass("ganador");
                }
            }
        }                    
    }
    
    this.actualizarResultadoPenales = function()
    {
        var localPenales = jQuery("#" + this.TAG_RESULTADOS_PENALES + this._value.encuentro.local.id);
        var visitantePenales = jQuery("#" + this.TAG_RESULTADOS_PENALES + this._value.encuentro.visitante.id);
        if ((localPenales[0] != undefined) && (visitantePenales[0] != undefined))
        {
            this.renderPenales(this._value.encuentro.local.id, this._value.encuentro.local.golesPenal);
            if (localPenales.attr("class").indexOf("fdoPenales") <= 0 )
            {            
                localPenales.attr("class", localPenales.attr("class") + " fdoPenales");
            }
            this.renderPenales(this._value.encuentro.visitante.id, this._value.encuentro.visitante.golesPenal);
            if (visitantePenales.attr("class").indexOf("fdoPenales") <= 0 )
            {
                visitantePenales.attr("class", visitantePenales.attr("class") + " fdoPenales");            
            }
        }
    }
    
    this.actualizarResultadoPenalesCabezal = function()
    {
        var localPenales = jQuery("#" + this.TAG_RESULTADOS_PENALES + this._value.encuentro.local.id);
        var visitantePenales = jQuery("#" + this.TAG_RESULTADOS_PENALES + this._value.encuentro.visitante.id);        
        if ((localPenales[0] != undefined) && (visitantePenales[0] != undefined))
        {
            var goles = ((this._value.encuentro.local.golesPenal == undefined) ? 0 : this._value.encuentro.local.golesPenal);
            localPenales[0].innerHTML = "(" + goles + ")";
            goles = ((this._value.encuentro.visitante.golesPenal == undefined) ? 0 : this._value.encuentro.visitante.golesPenal);
            visitantePenales[0].innerHTML = "(" + goles + ")";
        }
    }
    
    this.renderPenales = function(idEquipo, golesPenal)
    {
        var goles = ((golesPenal == undefined) ? 0 : golesPenal);
        if (jQuery("#spresp" + idEquipo )[0] == undefined)
        {
            jQuery("#" + this.TAG_RESULTADOS_PENALES + idEquipo).prepend("<span id='spresp" + idEquipo + "' class='resultadoPenales'>" + goles + "</span>");
        }
        else
        {
            jQuery("#spresp" + idEquipo)[0].innerHTML = goles;
        }
    }
    
    this.actualizarEstado = function()
    {
        try
        {
            var estado;
            if (this._value.encuentro.estado != undefined)
            {
                estado = jQuery("#" + this.TAG_ESTADO + this._value.cc.id)[0];
                if (estado != undefined)
                {
                    switch (this._value.encuentro.estado)
                    {
                        case "1":
                            estado.innerHTML = this.ESTADO_SIN_COMIENZO;
                            break;
                        case "2":
                            estado.innerHTML = this.ESTADO_EN_JUEGO;
                            break;
                        case "3":
                            estado.innerHTML = this.ESTADO_FINALIZADO;
                            break;
                        case "4":
                            estado.innerHTML = this.ESTADO_SUSPENDIDO;
                            break;
                        case "5":
                            estado.innerHTML = this.ESTADO_ENTRETIEMPO;
                            break;
                    }
                }
            }
        }
        catch (err)
        {
            if (isDebugging) { showError('tableroFutbol.actualizarPeriodo', err) };
        }

    }
    this.actualizarPeriodo = function()
    {
        try
        {
            if (this._value.encuentro.tiempo != undefined)
            {
                var oPeriodo = jQuery("#" + this.TAG_PERIODO + this._value.cc.id)[0];
                if (oPeriodo != undefined)
                {
                    if (this._value.encuentro.estado == "2")
                    {
                        if (oPeriodo != undefined)
                        {
                            oPeriodo.innerHTML = "";
                            switch (this._value.encuentro.tiempo)
                            {
                                case "1":
                                    oPeriodo.innerHTML = this.PRIMER_TIEMPO;
                                    break;
                                case "2":
                                    oPeriodo.innerHTML = this.SEGUNDO_TIEMPO;
                                    break;
                                case "3":
                                    oPeriodo.innerHTML = this.PRIMER_TIEMPO_SUPLEMENTARIO;
                                    break;
                                case "4":
                                    oPeriodo.innerHTML = this.SEGUNDO_TIEMPO_SUPLEMENTARIO;
                                    break;
                                case "5":
                                    oPeriodo.innerHTML = this.PENALES;
                                    break;
                            }
                        }

                    }
                    else
                    {
                        if(this._value.encuentro.estado == "1" && this._value.encuentro.hora != undefined)
                        {
                            oPeriodo.innerHTML = this._value.encuentro.hora + " hs";
                        }
                        else
                        {
                            oPeriodo.innerHTML = "";
                        }
                    }
                }
            }

        } 
        catch (err)
        {
            if (isDebugging) { showError('tableroFutbol.actualizarEstado', err) };
        }

    }
    
    this.actualizarEstadoCabezal = function()
    {
        try
        {
            var estado;
            if (this._value.encuentro.estado != undefined)
            {
                estado = jQuery("#" + this.TAG_ESTADO + this._value.cc.id)[0];
                if (estado != undefined)
                {
                    switch (this._value.encuentro.estado)
                    {
                        case "2":
                            estado.innerHTML = this.ESTADO_EN_JUEGO;
                            break;
                        case "3":
                            estado.innerHTML = this.ESTADO_FINALIZADO;
                            break;
                        case "4":
                            estado.innerHTML = this.ESTADO_SUSPENDIDO;
                            break;
                        case "5":
                            estado.innerHTML = this.ESTADO_ENTRETIEMPO;
                            break;
                    }
                }
            }
            if (this._value.encuentro.cronica != undefined)
            {

                var cronica = jQuery("#vr" + this._value.encuentro.id);
                var spanCronica = jQuery("span" , cronica);
                if (cronica.length)
                {
                    cronica.attr("href", this._value.encuentro.cronica);
		    spanCronica.attr("style", ""); 


		    switch (this._value.encuentro.estado)
                    {
                        case "1":
							spanCronica.html("Seguilo en vivo");
                            break;
                        case "2":
							spanCronica.html("Seguilo en vivo");
                            break;
                        case "3":
							spanCronica.html("Ver resumen");
                            break;
                        case "4":
							spanCronica.html("Ver resumen");
                            break;
                        case "5":
							spanCronica.html("Seguilo en vivo");
                            break;
                    }
                }

            }
        }
        catch (err)
        {
            if (isDebugging) { showError('tableroFutbol.actualizarPeriodo', 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 = document.getElementById(this.TAG_TIEMPO + this._value.cc.id);
            if (oTiempo != undefined)
            {
                if (this._value.encuentro.estado == "2" && this._value.encuentro.tiempo != "5")
                {
                    oTiempo.style.display = "inline";
                    oTiempo.style.background = "";
                    oTiempo.innerHTML = this.tiempoTranscurrido(this._value.encuentro.inicio_tiempo);
                }
                else
                {
                    oTiempo.innerHTML = " ";
					oTiempo.style.background = "#FFFFFF url(/_ui/desktop/imgs/tableros/gris75.gif) no-repeat scroll 0 0";
                    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.actualizarGamecast();
            }
            if (this._cambioIncidencias)
            {
                this.actualizarIncidencias();
                this.actualizarResumen();
            }
            if (this._cambioExtras)
            {
                this.actualizarMarquesina();
            }
        }
        catch (err)
        {
            if (isDebugging) { showError('tableroFutbol.execute', err) };
        }
    }
};
