/**
 * @author alex
 */

function color_fila(contenido, id){
  if (typeof(divPartido)=='undefined')
  		divPartido=new Array();
    switch (contenido) {
        case 1:
            value = '';
            break;
        case 2:
            value = '#B5D4F4';
            break;
        case 3:
            value = '#8ebeee';
            break;
    }
    if (divPartido[id] != "si") {
        $("fila_" + id).setStyle({
            backgroundColor: value
        });
        $("res_" + id).setStyle({
            backgroundColor: value
        });
        $("fav_" + id).setStyle({
            backgroundColor: value
        });
        $("combi_" + id).setStyle({
            backgroundColor: value
        });
        $("res_1_" + id).setStyle({
            backgroundColor: value
        });
       	if($("res_x_" + id)) $("res_x_" + id).setStyle({
            backgroundColor: value
        });
        $("res_2_" + id).setStyle({
            backgroundColor: value
        });
    }
}

function ver_resultado(partido_id){
    var el = $('div_oculto_' + partido_id); //se define la variable "el" igual a nuestro div
    var cuotas = $('div_oculto_cuotas' + partido_id);
    if (cuotas.style.display == 'none') {
        Effect.Appear(cuotas);
        Effect.Appear(el);
        if (anteriorr.get(partido_id).statusType != 'live') 
            color_fila(3, partido_id);
        divPartido[partido_id] = "si";
        activar_individual(partido_id, deporte);
        $('desplegar_combi' + partido_id).writeAttribute({
            'src': '/img/iconoCombiAbajo.png',
            'title': 'Ocultar',
            'alt': '"Ocultar'
        });
    }
    else {
        Effect.Fade(cuotas);
        Effect.Fade(el);
        divPartido[partido_id] = "no";
        color_fila(2, partido_id);
        $('desplegar_combi' + partido_id).writeAttribute({
            'src': '/img/iconoCombi.png',
            'title': 'Desplegar',
            'alt': '"Desplegar'
        });
    }
}
activar_individual = function(id, tipo_deporte){
    cargar_resultado_ind(id);
    cargar_cuotas_ind(id, tipo_deporte);
	if (!$('img_1_'+id))
		pintar_imagnes_cuotas(id);
}
//pintamos la imagenes de cuotas
pintar_imagnes_cuotas=function(id){
	partido=anteriorr.get(id);
	var link_01_img="";
	var link_02_img="";
	var link_03_img="";
	var tdOcultoApuesta3 ="";
	var tdOcultoApuesta1 ="";
	var tdOcultoApuesta2 ="";
	var tdOcultoApuesta1 = '<td class="cuarenta"><img src="/img/flechaRojaFutbol.jpg" alt="flecha" width="19" height="11"  class="tablaFlechasCuotasMargin" /></td>';
    var trOcultoApuesta1 = "<tr>" + tdOcultoApuesta1;
	var td_temp=tdOcultoApuesta1;
	trOcultoApuesta1 += td_temp;
	trOcultoApuesta1 += tdOcultoApuesta1 + "</tr>";
    if (partido.o1 != '0.00') {
		link_01_img = comparar_cuotas_img(partido.o1, partido.o1_ant, partido.o1Bid, partido.o1Name, 1, partido.id);			
		link_02_img = comparar_cuotas_img(partido.o2, partido.o2_ant, partido.o2Bid, partido.o2Name, 2, partido.id);
		link_03_img = comparar_cuotas_img(partido.o3, partido.o3_ant, partido.o3Bid, partido.o3Name, 3, partido.id);
		tdOcultoApuesta1 = '<td id="img_1_' + partido.id + '">' + link_01_img + '<img src="/img/' + partido.o1Bid + 'v.jpg" alt="' + partido.o1Name + '" width="21" height="45" /></a></td>';
		tdOcultoApuesta2 = '<td id="img_2_' + partido.id + '">' + link_02_img + '<img src="/img/' + partido.o2Bid + 'v.jpg" alt="' + partido.o2Name + '" width="21" height="45"  /></a></td>';
		tdOcultoApuesta3 = '<td id="img_3_' + partido.id + '">' + link_03_img + '<img src="/img/' + partido.o3Bid + 'v.jpg" alt="' + partido.o3Name + '" width="21" height="45"  /></a></td>';
	}
    var trOcultoApuesta2 = "<tr>" + tdOcultoApuesta1 + tdOcultoApuesta2 + tdOcultoApuesta3 + "</tr>";
    var tdOcultoApuesta1 = '<td colspan="3">&nbsp;</td>';
    var trOcultoApuesta3 = "<tr>" + tdOcultoApuesta1 + "</tr>";
    var tdOcultoApuesta1 = '<td class="cuarenta">&nbsp;</td>';
    var trOcultoApuesta4 = "<tr>" + tdOcultoApuesta1 + tdOcultoApuesta1 + tdOcultoApuesta1 + "</tr>";
  	if (partido.o1 != '0.00' && partido.o3 != '0.00') {
     	tbodyOcultoApuesta = "<tbody>" + trOcultoApuesta1 + trOcultoApuesta2 + trOcultoApuesta3 + "</tbody>";
    }else {
        tbodyOcultoApuesta = "<tbody>" + trOcultoApuesta4 + "</tbody>";
        }
        //montamos la tabla
    var tablaOcultaApuesta = '<table>' + tbodyOcultoApuesta + '</table>';
	$('div_oculto_cuotas'+id).update(tablaOcultaApuesta);
	if (cuotasMaximas==1)
		 cargarCasas_elegidas_partido(id,0);
}
//recogemos los goles del partido
cargar_resultado_ind = function(id){
	 var tiempo_ = new Date();
    tiempo_ = tiempo_.getTime();
    var url = "/mostrarBD/mostrar_goles.php";
    var myAjax = new Ajax.Request(url, {
        method: "get",
        parameters: {
            'id': id,
			'tiempo': tiempo_
			        },
        onComplete: function(t){
            pintarGoles(id,t.responseText.evalJSON()); // pintaremos las listas de todo
        }
        
    });
    
}
obtener_tarjetas = function(id){
	 cantidad_tar=0;
	  var tiempo_ = new Date();
    tiempo_ = tiempo_.getTime();
    var url = "/mostrarBD/mostrar_tarjetas.php";
    var myAjax = new Ajax.Request(url, {
        method: "get",
        parameters: {
            'id': id,
			'tiempo': tiempo_
        },
        onComplete: function(t){
            pintarTarjetas_ultimas(id, t.responseText.evalJSON()); // pintaremos las listas de todo
        }
    });
    
}
//recogemos las cuotas del partido
cargar_cuotas_ind = function(id, tipo_deporte){
	 var tiempo_ = new Date();
    tiempo_ = tiempo_.getTime();
    var url = "/mostrarBD/mostrar_cuotas.php";
    var myAjax = new Ajax.Request(url, {
        method: "get",
        parameters: {
            'id': id,
            'deporte': tipo_deporte,
			'tiempo': tiempo_
        },
        onComplete: function(t){
            pintarApuestas(id, tipo_deporte,t.responseText.evalJSON()); // pintaremos las listas de todo
        }
        
    });
    
}
//PINTAMOS los goles
pintarGoles = function(id,res_ind){
	var tabla_res;
	if( $F('sl_deporte') == 'HOC')	tabla_res = 'tablaResultadoHockey';
	else							tabla_res = 'tablaResultado';
    if (res_ind) {
        var tabla = new Element('table', {
            'id': tabla_res
        });
        var tbody = new Element('tbody');
        var tr = new Element('tr');
        tr.insert('<td scope="col">&nbsp;</td>', {
            position: 'after'
        });
        tr.insert('<td class="celdaResulNeutra" scope="col">&nbsp;</td>', {
            position: 'after'
        });
        tr.insert('<td scope="col">&nbsp;</td>', {
            position: 'after'
        });
        tbody.insert(tr, {
            position: 'after'
        });
        resultadoH = 0;
        resultadoA = 0;
		resultadoPH = 0;
		resultadoPA = 0;
		var periodo4 = 0;
		var imagen_gol;
		switch($F('sl_deporte')){
			case 'SOC': imagen_gol = '/img/gol.gif'; break;
			case 'HOC': imagen_gol = '/img/tanto_hky.png'; break;
			default: 	imagen_gol = '/img/gol.gif'; break;
		}
		
        res_ind.each(function(e){
          	var   golH = '&nbsp;';
			var golA = '&nbsp;';
            home = e.team == e.id_eHome;
			var tipo="";
			if (e.type=='Pen'){
				tipo="("+e.type+")";
			}
            if (home) {
				if (e.type) {
					if (e.type == 'Own') {
						
						if (e.period != 4) {
							resultadoA += 1;
						}else{
							resultadoPA+=1;
						}
						gol = e.time + "' " + e.name.substr(0, 10)+" (p.p.)";
						golA = '<div class="golhome"><img src="' + imagen_gol + '" alt="gol" width="12" height="12" /> </div>' + gol;
					}
					else {
						gol = e.time + "' " + e.name.substr(0, 10)+" "+tipo;
						if (e.period != 4) {
							resultadoH += 1;
						}else{
							resultadoPH+=1;
							gol=e.type+" "+ e.time;
						}
						
						golH = '<div class="golaway"><img src="' + imagen_gol + '" alt="gol" width="12" height="12" /> </div>' + gol;
					}
				}else {
						gol = e.time + "' " + e.name.substr(0, 10)+" "+tipo;
						if (e.period != 4) {
							resultadoH += 1;
						}
						else{
							resultadoPH+=1;
							gol=e.type+" "+ e.time;
						}
					golH = '<div class="golaway"><img src="' + imagen_gol + '" alt="gol" width="12" height="12" /> </div>' + gol;
					}
            }
            away = e.team == e.id_eAway;
            if (away) {
				if (e.type) {
					if (e.type == 'Own') {
						
						if (e.period != 4) {
							resultadoH += 1;
						}else{
							resultadoPH+=1;
							
						}
						gol = e.time + "' " + e.name.substr(0, 10)+" (p.p.)";
						golH = '<div class="golaway"><img src="' + imagen_gol + '" alt="gol" width="12" height="12" /> </div>' + gol;
					}
					else {
						gol = e.time + "' " + e.name.substr(0, 10)+" "+tipo;
						if (e.period != 4) {
							resultadoA += 1;
						}else{
							resultadoPA+=1;
							gol=e.type+" "+ e.time;
						}
						
						golA = '<div class="golhome"><img src="' + imagen_gol + '" alt="gol" width="12" height="12" /> </div>' + gol;
						
					}
				}else {
						gol = e.time + "' " + e.name.substr(0, 10)+" "+tipo;
						if (e.period != 4) {
							resultadoA += 1;
						}else{
							resultadoPA+=1;
							gol=e.type+" "+ e.time;
						}
						
						golA = '<div class="golhome"><img src="' + imagen_gol + '" alt="gol" width="12" height="12" /> </div>' + gol;
					}
			}
			
			if ( e.period==4){
				periodo4=1;
				resultadoTA=resultadoPA;
				resultadoTH=resultadoPH;
				
				calseP='penalty';
				calseR='penaltyRst';
			}else{
				resultadoTH=resultadoH
				resultadoTA=resultadoA
				calseP='celdaResulIzq';
				calseR='celdaResulCentro bold';
			}
			tr = new Element('tr');
			td = new Element('td', {
                'class': calseP,
                'scope': "col"
            }).update(golH);
            
            tr.insert(td, {
                position: 'after'
            });
            td = new Element('td', {
                'class': calseR,
                'scope': "col"
            }).update(resultadoTH + ' - ' + resultadoTA);
            
            tr.insert(td, {
                position: 'after'
            });
            td = new Element('td', {
                'class': calseP,
                'scope': "col"
            }).update(golA);
            
            tr.insert(td, {
                position: 'after'
            });
            tbody.insert(tr, {
                position: 'after'
            });
        });
		var resultado_final=resultadoTH + ' - ' + resultadoTA;
		if (resultado_final != anteriorr.get(id).current) {
			tbody.insert('<tr><td colspan="3" class="celdaResulCentro">' + anteriorr.get(id).current + '</td></tr>', {
				position: 'after'
			});
		}
        tabla.insert(tbody, {
            position: 'after'
        });
        $('resultado_individual' + id).update('');
        $('resultado_individual' + id).insert(tabla, {
            position: 'after'
        });
    }
    else {
        $('resultado_individual' + id).update('');
    }
   obtener_tarjetas(id) ;
}
pintarTarjetas_ultimas = function(id,tar_ind){
	var calseP='celdaResulIzq';
	var calseR='celdaResulCentro';
    if (tar_ind) {
		
        var tabla = new Element('table', {
            'id': "tablaResultado2"
        });
        var tbody = new Element('tbody');
		if (!$('tablaResultado')) {
			var tr = new Element('tr');
			tr.insert('<td scope="col">&nbsp;</td>', {
				position: 'after'
			});
			tr.insert('<td class="celdaResulNeutra" scope="col">&nbsp;</td>', {
				position: 'after'
			});
			tr.insert('<td scope="col">&nbsp;</td>', {
				position: 'after'
			});
			tbody.insert(tr, {
				position: 'after'
			});
		}
          tar_ind.each(function(e){
				var tarH = '&nbsp;';
				var tarA = '&nbsp;';
				var tarjeta = '';
				//var tarjeta = String(e.type) == 'Red' ? '<img src="/img/roja.jpg" alt="tar roja" width="8" height="11" /> ' : '<img src="/img/amarilla.jpg" alt="tar amrilla" width="8" height="11" /> ';
				
				if(String(e.type) == 'Yellow') {
					tarjeta = '<img src="/img/amarilla.jpg" alt="tar amrilla" width="8" height="11" /> ';
				}
				else if(String(e.type) == 'Red') {
					tarjeta = '<img src="/img/roja.jpg" alt="tar roja" width="8" height="11" /> ';
				}
				else {
					tarjeta = '<img src="/img/amarilla.jpg" alt="tar amrilla" width="8" height="11" /><img src="/img/roja.jpg" alt="tar roja" width="8" height="11" />';
				}
				home = e.team == e.id_eHome;
				if (home) {
				
					tarH = '<div class="golaway">' + tarjeta + '</div>' + e.time + "' " + e.name.substr(0, 10);
				}
				away = e.team == e.id_eAway;
				if (away) {
					tarA = '<div class="golhome">' + tarjeta + '</div>' + e.time + "' " + e.name.substr(0, 10);
				}
				tr = new Element('tr');
				td = new Element('td', {
					'class': calseP,
					'scope': "col"
				}).update(tarH);
				tr.insert(td, {
					position: 'after'
				});
				td = new Element('td', {
					'class': calseR,
					'scope': "col"
				}).update('&nbsp;');
				
				tr.insert(td, {
					position: 'after'
				});
				td = new Element('td', {
					'class': calseP,
					'scope': "col"
				}).update(tarA);
				
				tr.insert(td, {
					position: 'after'
				});
				tbody.insert(tr, {
					position: 'after'
				});
			});
			tabla.insert(tbody, {
				position: 'after'
			});
		$('resultado_individual' + id).insert(tabla, {
            position: 'after'
        });
    }
}
//pintamos la tabla de apuestas
pintarApuestas = function(id, tipo_deporte,cuotas_ind){
    if (cuotas_ind) {
        num = cuotas_ind.size();
        var tabla = new Element('table', {
            'id': 'tablaDspCasas' 
        });
        var tbody = new Element('tbody');
        var tr1 = new Element('tr');
        var tr2 = new Element('tr');
        var tr3 = new Element('tr');
        var tr4 = new Element('tr');
        tabla.insert(' <colgroup span="1" width="30"></colgroup>', {
            position: 'after'
        });
        tabla.insert(' <colgroup span="' + num + '" width="30"></colgroup>', {
            position: 'after'
        });
        tr1.insert('<td class="celdaNeutraCasas" scope="col">&nbsp;</td>', {
            postion: 'after'
        });
        tr2.insert('<td class="columnaCasas" >1</td>', {
            postion: 'after'
        });
        tr3.insert('<td class="columnaCasas" >X</td>', {
            postion: 'after'
        });
        tr4.insert('<td class="columnaCasas" >2</td>', {
            postion: 'after'
        });
       	var mejor_o1 = 0;
		var mejor_o2 = 0;
		var mejor_o3 = 0;
		cuotas_ind.each(function(e){
			mejor_o1 = parseFloat(e.o1)>mejor_o1?parseFloat(e.o1):mejor_o1;
			mejor_o2 = parseFloat(e.o2)>mejor_o2?parseFloat(e.o2):mejor_o2;
			mejor_o3 = parseFloat(e.o3)>mejor_o3?parseFloat(e.o3):mejor_o3;
			});
        cuotas_ind.each(function(e){
            var td = new Element('td', {
                'scope': 'col',
                'class': 'columnaCasas' 
            }).update('<a href="' + linkapuesta('casa' + e.bid) + 
					'"  onclick="clic(\'marcadores_apuestas\',\'' + e.name + 
					'\');" target="_blank" ><img src="/img/' + e.bid + 'p.png" alt="' + e.name + 
					'" width="34" height="16" class="imgBloqueCasa" /></a>');
            tr1.insert(td, {
                position: 'after'
            });
            clase1 = "linkDspCasas";
            mejor_cla_o1 = e.o1 == mejor_o1;
            if (mejor_cla_o1) 
                clase1 = "linkMejorCuota";
            td = new Element('td').update('<a href="' + linkapuesta('casa' + e.bid) + '"  onclick="clic(\'marcadores_apuestas\',\'' + e.name + '\');" target="_blank"  class="' + clase1 + '">' + e.o1 + '</a>');
            tr2.insert(td, {
                position: 'after'
            });
            clase2 = "linkDspCasas";
            mejor_cla_o2 = e.o2 == mejor_o2;
            if (mejor_cla_o2) {
                clase2 = "linkMejorCuota";
            }
            td = new Element('td').update('<a href="' + linkapuesta('casa' + e.bid) + 
								'"  onclick="clic(\'marcadores_apuestas\',\'' + e.name + 
								'\');" target="_blank"  class="' + clase2 + '" >' + e.o2 + '</a>');
            clase3 = "linkDspCasas";
            mejor_cla_o3 = e.o3 == mejor_o3;
            if (mejor_cla_o3) 
                clase3 = "linkMejorCuota";
            
            tr3.insert(td, {
                position: 'after'
            });
            td = new Element('td').update('<a href="' + linkapuesta('casa' + e.bid) + 
								'"  onclick="clic(\'marcadores_apuestas\',\'' + e.name + 
								'\');" target="_blank"  class="' + clase3 + '">' + e.o3 + '</a>');
            tr4.insert(td, {
                position: 'after'
            });
        });
        tbody.insert(tr1, {
            position: 'after'
        });
        tbody.insert(tr2, {
            position: 'after'
        });
         tbody.insert(tr3, {
                position: 'after'
            });
        tbody.insert(tr4, {
            position: 'after'
        });
        tabla.insert(tbody, {
            position: 'after'
        });
		
        $('resultado_cuotas' + id).update('');
        $('resultado_cuotas' + id).insert(tabla, {
            position: 'after'
        });
        
    }
    else {
        $('resultado_cuotas' + id).update('<p></br></p><p></br>No Hay resultado de apuestas</br></p></br>');
        $('resultado_cuotas' + id).insert(new Element('br'), {
            position: 'after'
        });
    }
}
function comparar_cuotas(cuota, cuota_ant, bid, name, o, partido){
    var menor = '<img class="flechasCuotas" src="/img/bajaCuota.gif" alt="1" />';
    var mayor = '<img class="flechasCuotas" src="/img/subeCuota.gif" alt="2" />';
    var igual = '<img class="flechasCuotas" src="/img/flechaVacia.gif" alt="3" />';
    var imgFlecha = '';
    var titulo = '';
    cuota = parseFloat(cuota);
    cuota_ant = parseFloat(cuota_ant);
        if (cuota > cuota_ant) {
            imgFlecha = mayor;
            titulo = cuota_ant + ' >> ' + cuota;
        }else if (cuota < cuota_ant){
            imgFlecha = menor;
            titulo = cuota_ant + ' >> ' + cuota;
        }else{
            titulo = "";
            imgFlecha = igual;
        }
    return  imgFlecha + '<a  class="help" id="link_' + o + partido + '" onclick="clic(\'marcadores_mejor_cuota\',\'' + name + '\');" target="_blank" title="' + name + ' ' + titulo + '" href="' + linkapuesta('casa' + bid) + '">';
}

function comparar_cuotas_img(cuota, cuota_ant, bid, name, o, partido){
    var titulo = '';
    cuota = parseFloat(cuota);
    cuota_ant = parseFloat(cuota_ant);
            titulo =cuota > cuota_ant? cuota_ant + ' >> ' + cuota:cuota_ant + ' >> ' + cuota;
            titulo =cuota == cuota_ant?"":titulo;
    return'<a  class="help"  id="link_' + o + partido + '" onclick="clic(\'marcadores_mejor_cuota\',\'' + name + '\');" target="_blank" title="' + name + ' ' + titulo + '" href="' + linkapuesta('casa' + bid) + '">';
}