﻿/* objeto Tablero Basquet */
var tableroBasquet =
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_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";
	}
	else
	{
		this.TAG_RESULTADOS = "res";
		this.TAG_INCIDENCIA = "i";
		this.TAG_ESTADO = "st";
		this.TAG_PERIODO = "p";
		this.TAG_TIEMPO = "t";	
		this.TAG_GAMECAST = "gamec";
	}
    
	/* ******************** */
	
    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('tableroBasquet.setValue', err) };
        }
    }
	this.cambiarActivo = function(isActive)
	{
	    try
	    {
	        this._estaActivo = isActive;
	    }
	    catch (err)
	    {
	        if (isDebugging) { showError('tableroBasquet.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);
			if ((oGamecast.length) && (this._value.encuentro.linkeable != undefined))
			{
		 		if(this._value.encuentro.linkeable == "1")
		 		{
		 			oGamecast.attr("style","display:'block';");
				}
				else
				{
		 			oGamecast.attr("style","display:'none';");
		 		}
	 		}
 		}
 		catch (err)
 		{
            if (isDebugging) { showError('tableroBasquet.actualizarGamecast', err) };
        }
    }
    this.actualizarIncidencias = function()
    {
        try
        {
            var oIncidencia = jQuery("#" + this.TAG_INCIDENCIA + this._value.cc.id);
            if ((oIncidencia.length) && (this._value.ins[0].inc.cm != undefined))
            {
                for (var i = 0; i < this._value.ins.length; i++)
                {
                    oIncidencia.html(this._value.ins[0].inc.mm + "' " + this._value.ins[0].inc.cm);
                }
            }
        }
        catch (err)
        {
            if (isDebugging) { showError('tableroBasquet.actualizarIncidencias', 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('tableroBasquet.actualizarMarquesina', err) };
        }
    }
    this.actualizarResultado = function()
    {
        try
        {
            var local, visitante;
            if ((this._value.encuentro.local.id != undefined) && (this._value.encuentro.visitante.id)) 
            {
                local = jQuery("#" + this.TAG_RESULTADOS + this._value.encuentro.local.id);
                visitante = jQuery("#" + this.TAG_RESULTADOS + this._value.encuentro.visitante.id);

                if ((local.length) && (visitante.length)) 
                {
                    local.html(this._value.encuentro.local.goles);
                    visitante.html(this._value.encuentro.visitante.goles);
                    if (parseInt(this._value.encuentro.local.goles) > parseInt(this._value.encuentro.visitante.goles)) 
                    {
                        local.addClass("ganador");
                        visitante.removeClass("ganador");
                    }
                    else if (parseInt(this._value.encuentro.local.goles) < parseInt(this._value.encuentro.visitante.goles)) 
                    {
                        visitante.addClass("ganador");
                        local.removeClass("ganador");
                    }
                    else 
                    {
                        visitante.removeClass("ganador");
                        local.removeClass("ganador");
                    }
                }
            }
        }
        catch (err)
        {
            if (isDebugging) { showError('tableroBasquet.actualizarResultado', err) };
        }

    }
    this.actualizarEstado = function() 
    {
        try 
        {
            var estado;
            if (this._value.encuentro.estado != undefined) 
            {
                estado = jQuery("#" + this.TAG_ESTADO + this._value.cc.id);
                if (estado.length)
                {
                    if (this.Estados[this._value.encuentro.estado] != null)
                    {
                        estado.html(this.Estados[this._value.encuentro.estado]);
                    }
                    else
                    {
                        estado.html("");
                    }
                }
            }
        }
        catch (err) 
        {
            if (isDebugging) { showError('tableroBasquet.actualizarPeriodo', err) };
        }

    }
    this.actualizarPeriodo = function()
    {
        try
        {
            if (this._value.encuentro.tiempo != undefined)
            {
                var oPeriodo = jQuery("#" + this.TAG_PERIODO + this._value.cc.id);
				if (oPeriodo.length)
				{
	                if (this._value.encuentro.estado == "2")
	                {
	                    oPeriodo.html(this.Tiempos[this._value.encuentro.tiempo]); 
                        //oPeriodo.html(this.Estados[this._value.encuentro.estado]); 
	                }
	                else
	                {
	                    oPeriodo.html("");
	                }
	             }
            }
        }
        catch (err)
        {
            if (isDebugging) { showError('tableroBasquet.actualizarEstado', err) };
        }
    }
    this.isTypeOf = function(value)
    {
        var _isTypeOf = false;
        if (value == "tableroBasquet")
            _isTypeOf = !_isTypeOf;
        return _isTypeOf;
    }

    /* Estados del partido */
	this.Estados = [];
	this.Estados["1"] = "Sin comienzo";
	this.Estados["2"] = "En juego";
	this.Estados["3"] = "Finalizado";
	this.Estados["4"] = "Suspendido";
	this.Estados["5"] = "Entretiempo";
	
	/* Tiempos del partido */
	this.Tiempos = [];
	this.Tiempos["17"] = "1er Cuarto";
	this.Tiempos["18"] = "2do Cuarto";
	this.Tiempos["19"] = "3er Cuarto";
	this.Tiempos["20"] = "4to Cuarto";
	
    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") {
                    oTiempo.attr("style","display:'inline';");
                    oTiempo.html(this.tiempoTranscurrido(this._value.encuentro.inicio_tiempo));
                }
                else {
                    oTiempo.html("");
                }
            }
            oTiempo = null;

        }
        catch (err) {
            if (isDebugging) { showError('tableroBasquet.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();
        	}
			
        	if(this._cambioExtras){this.actualizarMarquesina(); }  
        }
        catch (err) {
            if (isDebugging) { showError('tableroBasquet.execute', err) };
        }
    }
};
