<?php
	$mid = $_GET["match"];
	$info = array();
	$isLive = 0;

	// Consulta se jogo está no ao vivo
	$x = simplexml_load_file('http://www.sysfbet.com.br/live/aovivo-sb.xml');
	foreach ($x as $j) {
		if($j->id == $mid){
			$isLive = 1;
			$info["placar"] = (string)$j->placar_jogo;
			$info["tempo"] = (string)$j->tempo."'";
			$info["oddCasa"] = (string)$j->cotacoes->time1;
			$info["oddEmpate"] = (string)$j->cotacoes->empate;
			$info["oddVisitante"] = (string)$j->cotacoes->time2;
			$info["isLive"] = 1;
			$info["odds"] = array();
			$info["eventos"] = array();

			// ODDS
			$x2 = simplexml_load_file('http://www.sysfbet.com.br/live/xml/live/'.$mid.'.xml');

			$updates = (string)$x2->attributes()->updates;
			$updates = explode('|||', $updates);
			$i = 0;
			foreach ($updates as $u) {
				$u2 = explode(' - ', $u);
				$u2[0] = '<b>'.$u2[0].'</b>';
				$u2[1] .= ' ||';
				$u2 = implode(' - ', $u2);
				$u2 = str_replace('|| - ', '<div class="event_description">', $u2).'</div>';
				$u2 = str_replace('||', '', $u2);

				if(strpos($u2, 'gol')){ $u2 = '<img src="/img/goal_icon.png" /> '.$u2; }
				elseif(strpos($u2, 'Gol')){ $u2 = '<img src="/img/goal_icon.png" /> '.$u2; }
				elseif(strpos($u2, 'escanteio')){ $u2 = '<img src="/img/corner_icon.png" /> '.$u2; }
				elseif(strpos($u2, 'amarelo')){ $u2 = '<img src="/img/yellow_icon.png" /> '.$u2; }
				elseif(strpos($u2, 'vermelho')){ $u2 = '<img src="/img/red_icon.png" /> '.$u2; }
				elseif(strpos($u2, 'Substituição')){ $u2 = '<img src="/img/subs_icon.png" /> '.$u2; }
				else { $u2 = '<img src="/img/other_icon.png" /> '.$u2; }

				$u2 = str_replace('Substitution', 'Substituição', $u2);
				$u2 = str_replace(' for ', ' por ', $u2);

				$info["eventos"][$i] = $u2; $i++;
			}

			$aux = explode('-', $x2->attributes()->ICORNER);
			$info["corner_1"] = $aux[0];
			$info["corner_2"] = $aux[1];
			$info["corner_total"] = $aux[0] + $aux[1];

			$aux = explode('-', $x2->attributes()->IYELLOWCARD);
			$info["yellow_1"] = $aux[0];
			$info["yellow_2"] = $aux[1];
			$info["yellow_total"] = $aux[0] + $aux[1];

			$aux = explode('-', $x2->attributes()->IREDCARD);
			$info["red_1"] = $aux[0];
			$info["red_2"] = $aux[1];
			$info["red_total"] = $aux[0] + $aux[1];

			$aux = explode('-', $x2->attributes()->IPENALTY);
			$info["pen_1"] = $aux[0];
			$info["pen_2"] = $aux[1];
			$info["pen_total"] = $aux[0] + $aux[1];

			$aux = explode('-', $x2->attributes()->ISUBSTITUTION);
			$info["subs_1"] = $aux[0];
			$info["subs_2"] = $aux[1];
			$info["subs_total"] = $aux[0] + $aux[1];
			
			foreach ($x2 as $b) {
				$betName = (string)$b->attributes()->name;
				$info["odds"][$betName] = array();
				$i = 0;

				foreach ($b as $o) {
					$info["odds"][$betName][$i]["name"] = (string)$o->attributes()->name;
					$info["odds"][$betName][$i]["odd"] = (string)$o->attributes()->odd;
					$i++;
				}
			}
		}
	}

	if(!$isLive && $x2 = simplexml_load_file('http://www.sysfbet.com.br/live/xml/live/'.$mid.'.xml')){

		$updates = (string)$x2->attributes()->updates;
		$updates = explode('|||', $updates);
		$i = 0;
		foreach ($updates as $u) {
			$u2 = explode(' - ', $u);
			$u2[0] = '<b>'.$u2[0].'</b>';
			$u2[1] .= ' ||';
			$u2 = implode(' - ', $u2);
			$u2 = str_replace('|| - ', '<div class="event_description">', $u2).'</div>';
			$u2 = str_replace('||', '', $u2);

			if(strpos($u2, 'gol')){ $u2 = '<img src="/img/goal_icon.png" /> '.$u2; }
			elseif(strpos($u2, 'Gol')){ $u2 = '<img src="/img/goal_icon.png" /> '.$u2; }
			elseif(strpos($u2, 'escanteio')){ $u2 = '<img src="/img/corner_icon.png" /> '.$u2; }
			elseif(strpos($u2, 'amarelo')){ $u2 = '<img src="/img/yellow_icon.png" /> '.$u2; }
			elseif(strpos($u2, 'vermelho')){ $u2 = '<img src="/img/red_icon.png" /> '.$u2; }
			elseif(strpos($u2, 'Substitution')){ $u2 = '<img src="/img/subs_icon.png" /> '.$u2; }
			elseif(strpos($u2, 'Substituição')){ $u2 = '<img src="/img/subs_icon.png" /> '.$u2; }
			else { $u2 = '<img src="/img/other_icon.png" /> '.$u2; }

			$u2 = str_replace('Substitution', 'Substituição', $u2);
			$u2 = str_replace(' for ', ' por ', $u2);

			$info["eventos"][$i] = $u2; $i++;
		}

		$info["placar"] = str_replace('-', ':', $x2->attributes()->GOAL);
		$info["tempo"] = "FIM";
		$info["oddCasa"] = '--';
		$info["oddEmpate"] = '--';
		$info["oddVisitante"] = '--';
		$info["isLive"] = 3;

		if($x3 = simplexml_load_file('http://www.sysfbet.com.br/resultados/xmls/'.$mid.'.xml')){
			$info["placar"] = (string)$x3->resultado->placar->ft->attributes()->placar;
			$info["ht"] = '('.(string)$x3->resultado->placar->ht->attributes()->placar.')';
		}

		$aux = explode('-', $x2->attributes()->ICORNER);
		$info["corner_1"] = $aux[0];
		$info["corner_2"] = $aux[1];
		$info["corner_total"] = $aux[0] + $aux[1];

		$aux = explode('-', $x2->attributes()->IYELLOWCARD);
		$info["yellow_1"] = $aux[0];
		$info["yellow_2"] = $aux[1];
		$info["yellow_total"] = $aux[0] + $aux[1];

		$aux = explode('-', $x2->attributes()->IREDCARD);
		$info["red_1"] = $aux[0];
		$info["red_2"] = $aux[1];
		$info["red_total"] = $aux[0] + $aux[1];

		$aux = explode('-', $x2->attributes()->IPENALTY);
		$info["pen_1"] = $aux[0];
		$info["pen_2"] = $aux[1];
		$info["pen_total"] = $aux[0] + $aux[1];

		$aux = explode('-', $x2->attributes()->ISUBSTITUTION);
		$info["subs_1"] = $aux[0];
		$info["subs_2"] = $aux[1];
		$info["subs_total"] = $aux[0] + $aux[1];
	}

	elseif(!$isLive){
		$info["placar"] = '-:-';
		$info["tempo"] = '';
		$info["oddCasa"] = 0;
		$info["oddEmpate"] = 0;
		$info["oddVisitante"] = 0;
		$info["isLive"] = 0;
	}

	echo json_encode($info);
?>