﻿/* objeto Tablero Tenis */
var tableroTenis =
function(value, isActive) {

    this._value = value;
    this._estaActivo = isActive || true;
    this._cambioEncuentro = false;
    this._cambioIncidencias = false;
    this._cambioExtras = false;
    this._cambioFormaciones = false;
    this._cantidadSet = 0;
    this._tieneTiempo = true;
    
    
    /* TAG A ACTUALIZAR */
    
	if (this._value.cc.hasOwnProperty('tipoTablero')) 
	{
		this.tipoTablero = this._value.cc.tipoTablero;
	}
	else
	{
		this.tipoTablero = "";
	}
	this.TAG_RESULTADOS = this.tipoTablero + "res";
	this.TAG_INCIDENCIA = this.tipoTablero + "i";
	this.TAG_ESTADO = this.tipoTablero + "st";
	this.TAG_PERIODO = this.tipoTablero + "p";
	this.TAG_TIEMPO = this.tipoTablero +  "t";
	this.TAG_GAMECAST = this.tipoTablero + "gamec";
	this.TAG_SAQUE = this.tipoTablero + "sq";
	this.TAG_EQUIPO = this.tipoTablero + "eq";
	this.TAG_BANDERA = this.tipoTablero + "img";
	this.TAG_PUNTOS = "ptos";
    
	/* ******************** */
	
	
    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('tableroTenis.setValue', err) };
        }
    }
	this.cambiarActivo = function(isActive) {
	    try {
	        this._estaActivo = isActive;
	    }
	    catch (err) {
	        if (isDebugging) { showError('tableroTenis.estaActivo', err) };
	    }
	}
	this.tieneTiempo = function() {
	    return this._tieneTiempo;
	}
	this.estaActivo = function() {
	        return this._estaActivo;

	}

    this.actualizarIncidencias = function() {
        try {

            var oIncidencia = jQuery("#" + this.TAG_INCIDENCIA + this._value.cc.id)[0];
            if ((oIncidencia != undefined) && (this._value.ins[0].inc.cm != undefined)) {
                for (var i = 0; i < this._value.ins.length; i++) {
                    oIncidencia.innerHTML = this._value.ins[0].inc.mm + "' " + this._value.ins[0].inc.cm;
                }
            }
        }
        catch (err) {
            if (isDebugging) { showError('tableroTenis.actualizarIncidencias', err) };
        }
    }
    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";
		 		}
	 		}
	 		else
	 		{
	 		}
 		}
 		catch (err) {
            if (isDebugging) { showError('tableroFutbol.actualizarGamecast', err) };
        }
    }
    
    this.actualizarBanderas = function() {

        try {

           		local = jQuery("#" + this.TAG_BANDERA + this._value.encuentro.local.id);
	            visitante = jQuery("#" + this.TAG_BANDERA + this._value.encuentro.visitante.id);
	            if ((local != undefined)  ) {
	            	if(this._value.encuentro.local.pais != ""){
		 				local.attr("src", "/_ui/desktop/imgs/layout/iconos/banderas/18x14/paises/" + this._value.encuentro.local.pais + ".gif");
					}
	           }
	           if (visitante != undefined){
					if (this._value.encuentro.visitante.pais != ""){
						visitante.attr("src", "/_ui/desktop/imgs/layout/iconos/banderas/18x14/paises/" + this._value.encuentro.visitante.pais + ".gif");
			 		}                	
	            }
            }
      
        catch (err) {
            if (isDebugging) { showError('tableroFutbol.actualizarResultado', err) };
        }

    }
    
    
    this.actualizarFormaciones = function() {
        try {

        }
        catch (err) {
            if (isDebugging) { showError('tableroTenis.actualizarFormaciones', err) };
        }
    }
    
    this.actualizarMarquesina = function() {
        try {        	
        	//TODO
            var oMarquesina = jQuery("#" + "mq" + this._value.cc.id)[0];
        	
            if ((oMarquesina != undefined) || (this._value.extras.mq != undefined)){
                oMarquesina.innerHTML = this._value.extras.mq.texto;
            }
        }
        catch (err) {
            if (isDebugging) { showError('tableroTenis.actualizarMarquesina', err) };
        }
    }
    this.actualizarSets = function(){
    	try{
    		for (var i = 0;i < this._value.encuentro.local.tantos.length; i++) {
    			if(this._value.encuentro.local.tantos[i].id == "5")
    				this._cantidadSet += 1; 
    		}    		
    		for(var i= this._cantidadSet + 1; i <= 5; i++)
    		{
    			jQuery("#" + this.TAG_RESULTADOS + this._value.encuentro.local.id + "_" + i).addClass("fdoGrisClaro");
    			jQuery("#" + this.TAG_RESULTADOS + this._value.encuentro.visitante.id + "_" + i).addClass("fdoGrisClaro");
    		}
    	}
    	catch (err) {
            if (isDebugging) { showError('tableroTenis.actualizarSets', err) };
        }
    }
 
    this.actualizarResultado = function() {

        try {
			
            var local, visitante;
            if ((this._value.encuentro.local.id != undefined) && (this._value.encuentro.visitante.id)) {
            	
            	//SET TOTALES
	            local = jQuery("#" + this.TAG_RESULTADOS + this._value.encuentro.local.id + "_0")[0];
	            visitante = jQuery("#" + this.TAG_RESULTADOS + this._value.encuentro.visitante.id + "_0")[0];                			
	            if ((local != undefined) && (visitante != undefined)) {
	                local.innerHTML = this._value.encuentro.local.sets;
	                visitante.innerHTML = this._value.encuentro.visitante.sets;
	            }
	            
	            //SAQUE
	            local = jQuery("#" + this.TAG_SAQUE + this._value.encuentro.local.id)[0];
	            visitante = jQuery("#" + this.TAG_SAQUE + this._value.encuentro.visitante.id)[0];
	            if ((local != undefined) && (visitante != undefined)) {
	            	if(this._value.encuentro.local.saque == "S"){
		 				local.style.display = "block";
		 				visitante.style.display = "none";
					}
					if (this._value.encuentro.visitante.saque == "S"){
						local.style.display = "none";
		 				visitante.style.display = "block";
			 		}                	
	            }
	            

	            for (var i = 0;i < this._value.encuentro.local.tantos.length; i++) {
	            	switch (this._value.encuentro.local.tantos[i].id) {  
	            		
                        case "5":   
                        	// RESALTA GANADOR SETS
                        	local = jQuery("#" + this.TAG_RESULTADOS + this._value.encuentro.local.id + "_" + (i + 1))[0];
                        	visitante = jQuery("#" + this.TAG_RESULTADOS + this._value.encuentro.visitante.id + "_" + (i + 1))[0];
                        	if (this._value.encuentro.local.tantos[i].valor > this._value.encuentro.visitante.tantos[i].valor){
                        		local.style.fontWeight = "bold";
                        		visitante.style.fontWeight = "normal";
                        	}
                        	if (this._value.encuentro.local.tantos[i].valor < this._value.encuentro.visitante.tantos[i].valor){
                        		local.style.fontWeight = "normal";
                        		visitante.style.fontWeight = "bold";
                        	}
                        	//SETS
                        	local = jQuery("#" + this.TAG_RESULTADOS + this._value.encuentro.local.id + "_" + this._value.encuentro.local.tantos[i].orden)[0];				            				            
                            break;
                        case "6"://PUNTOS
                            local = jQuery("#" + this.TAG_RESULTADOS + this._value.encuentro.local.id + "_" + this.TAG_PUNTOS)[0];		           
			       			break;
			       		default: 
			       			local = 'undefined';
			       			break;
                    }
                    if (local != undefined) {                    			
				            	local.innerHTML = this._value.encuentro.local.tantos[i].valor;								               	
				    }
				}
				
				for (var i = 0;i < this._value.encuentro.visitante.tantos.length; i++) {
	            	switch (this._value.encuentro.visitante.tantos[i].id) {  
	            		
                        case "5"://SETS               
                        	visitante = jQuery("#" + this.TAG_RESULTADOS + this._value.encuentro.visitante.id + "_" + this._value.encuentro.visitante.tantos[i].orden)[0];				            				            
                            break;
                        case "6"://PUNTOS
                            visitante = jQuery("#" + this.TAG_RESULTADOS + this._value.encuentro.visitante.id + "_" + this.TAG_PUNTOS)[0];		           
			       			break;
			       		default: 
			       			visitante = 'undefined';
			       			break;
                    }
                    if (visitante != undefined) {
				            	visitante.innerHTML = this._value.encuentro.visitante.tantos[i].valor;								               	
				    }
				}
				
				if(this._value.encuentro.estado == "3"){
					
					local = jQuery("#" + this.TAG_EQUIPO + this._value.encuentro.local.id);
	           		visitante = jQuery("#" + this.TAG_EQUIPO + this._value.encuentro.visitante.id);
	           		
	           		// DESTACAR EQUIPO GANADOR
	           		if(this._value.encuentro.local.sets > this._value.encuentro.visitante.sets){
	           			local.addClass("ganador");	           			
	           		}
	           		if(this._value.encuentro.visitante.sets > this._value.encuentro.local.sets){
	           			visitante.addClass("ganador");
	           		}
	           		
	           		
	           	
				
				}
	            
	            
            }   
				        
        }
        catch (err) {
            if (isDebugging) { showError('tableroTenis.actualizarResultado', err) };
        }

    }
    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('tableroTenis.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.ESTADO_EN_JUEGO ;
	                                break;
	                            case "2":
	                                oPeriodo.innerHTML = this.ESTADO_EN_JUEGO ;
	                                break;
	                            case "3":
	                                oPeriodo.innerHTML = this.ESTADO_EN_JUEGO ;
	                                break;
	                            case "4":
	                                oPeriodo.innerHTML = this.ESTADO_EN_JUEGO ;
	                                break;
	                            case "5":
	                                oPeriodo.innerHTML = this.ESTADO_EN_JUEGO ;
	                                break;
	                        }
	                    }
	
	                } else {
	                    oPeriodo.innerHTML = "";
	                }
                }
            }

        } catch (err) {
            if (isDebugging) { showError('tableroTenis.actualizarEstado', err) };
        }

    }
    this.isTypeOf = function(value) {
        var _isTypeOf = false;
        if (value == "tableroTenis")
            _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") {
                    oTiempo.style.display = "inline";
                    oTiempo.innerHTML = this.tiempoTranscurrido(this._value.encuentro.inicio_tiempo);
                }
                else {
                    oTiempo.innerHTML = "";
                }
            }
            oTiempo = null;

        }
        catch (err) {
            if (isDebugging) { showError('tableroTenis.renderReloj', err) };
            this._tieneTiempo = false;
        }

    }

    // implementa diferente para cada tipo de tablero: tennis, futbol
    this.execute = function() {
        try {
        	
        	if(this._cambioEncuentro)
        	{
        		if(this._cantidadSet == 0) this.actualizarSets();
        	    this.actualizarEstado();
            	this.actualizarPeriodo();
            	this.actualizarResultado();
            	this.actualizarGamecast();
            	this.actualizarBanderas();
            	
        	}
        	if(this._cambioIncidencias)
        	{
        		this.actualizarIncidencias();	
        	}
			
        	if(this._cambioExtras){this.actualizarMarquesina(); }  
        	}
        catch (err) {
            if (isDebugging) { showError('tableroTenis.execute', err) };
        }
   }
};