<?php
	
	//require_once 'functions.php';
	//require_once 'stats_bf.php';
	
	//$XMLTEAM = unserialize(file_get_contents('stats_bf.php'));
	//$XMLRACE = unserialize(file_get_contents('stats_race.php'));

/*
	print "<?php\n";
	
	print '$loadavg = file("/proc/loadavg");';
	print '$loadavg = explode(" ",$loadavg[0]);';
	print '$loadavg = $loadavg[0];';
	print 'if ($loadavg>4) {echo "Sorry! Server load critical.";exit();}';
	
	print "?>\n";
*/	
	
	print "<html>\n";
	include 'header.php';
	print "<body>\n";
		print "<center>\n";
		print "<table cellspacing='0' cellpadding='0' width='800' border='0'>\n";
				print "<tr><td colspan=3>&nbsp;</td></tr>";
				
				print "<tr>\n";					
					if (is_array($XMLTEAM[$i-1]))
						print "<td width='250' align='center'><a href='team_" . intval($XMLTEAM[$i-1]['team_data']['number']) . ".html'>#" . $XMLTEAM[$i-1]['team_data']['number'] . " - " . $XMLTEAM[$i-1]['team_data']['name'] . "</a></td>";
					else
						print "<td width='250'/>\n";
						
						print "<td  width='300' align='center'><h2>#" . $XMLTEAM[$i]['team_data']['number'] . " - " . $XMLTEAM[$i]['team_data']['name'] . "</h2></td>\n";
						
					if (is_array($XMLTEAM[$i+1]))
						print "<td width='250' align='center'><a href='team_" . intval($XMLTEAM[$i+1]['team_data']['number']) . ".html'>#" . $XMLTEAM[$i+1]['team_data']['number'] . " - " . $XMLTEAM[$i+1]['team_data']['name'] . "</a></td>";
					else
						print "<td width='250'/>\n";
						
				print "</tr>\n";
				print "<tr>\n";
					print "<td class='standing_big_black'><a href='index.html'>Live standings</a></td>\n";
				print "</tr>\n";
		print "</table>\n";
		
		print "<hr class='standings_hr'>\n";
		print "Race started on " . date("l, F jS Y \a\\t G:i", $XMLRACE['race_start']) . ". \n";

			$diff = ($race_start + ($RACE['time'] * 60 * 60)) - time();
			$current_hour = floor($diff / (60 * 60));

			if ($diff > 0 && $diff < ($XMLRACE['time']*60*60))
			{
				$h = floor($diff / (60 * 60));
				$diff -= $h * 60 * 60;
				$m = floor($diff / 60);
				$diff -= $m * 60;
				$s = $diff;

				print "Finish in " . $h . " hours " . $m . " minutes and " . $s . " seconds.\n";
			}
			else
				print "Race finished after " . $XMLRACE['time'] . " hours.\n";

		print "<hr class='standings_hr'>\n";

		print "<table cellspacing='0' cellpadding='0' border='0'>\n";
				print "<tr bgcolor='#A1A1A1'>\n";
					print "<td width='20' bgcolor='#ffffff'></td>\n";

					print "<td width='50' bgcolor='#A1A1A1' align='center' class='standing_big_white' background='images/gradient_gray.jpg'>Pos</td>\n";
					print "<td width='3' bgcolor='#FFFFFF'></td>\n";
					print "<td width='300' bgcolor='#A1A1A1' class='standing_big_blue' background='images/gradient_gray.jpg'><b>Team name</b></td>\n";
					print "<td width='45' bgcolor='#A1A1A1' align='center' background='images/gradient_gray.jpg'>Car</td>\n";
					print "<td width='35' bgcolor='#A1A1A1' background='images/gradient_gray.jpg'>Swap</td>\n";
					print "<td width='25' bgcolor='#A1A1A1' background='images/gradient_gray.jpg'>Pit</td>\n";
	
					print "<td width='25' bgcolor='#A1A1A1' background='images/gradient_gray.jpg'>DT</td>\n";
					print "<td width='50' bgcolor='#A1A1A1' background='images/gradient_gray.jpg'>Sector</td>\n";
					print "<td width='125' bgcolor='#A1A1A1' background='images/gradient_gray.jpg'>Current driver</td>\n";
					print "<td width='75' bgcolor='#A1A1A1' background='images/gradient_gray.jpg'>Car in front***</td>\n";
					print "<td width='3' bgcolor='#FFFFFF'></td>\n";
					print "<td width='100' bgcolor='#A1A1A1' class='standing_big_white' align='center' background='images/gradient_gray.jpg'>Gap</td>\n";
					
					print "<td width='50' bgcolor='#FFFFFF'></td>\n";
				print "</tr>\n";
				print "<tr>\n";
					print "<td colspan='12' height='5'></td>\n";
				print "</tr>\n";
				
					print "<tr bgcolor='#EBEBEB'>\n";
						print "<td bgcolor='#ffffff'></td>";
						print "<td bgcolor='#A1A1A1' align='center' class='standing_big_white' background='images/gradient_gray.jpg'>" . $XMLTEAM[$i]['race_data']['position'] . "</td>\n";
				
						print "<td bgcolor='#ffffff'></td>\n";
						print "<td class='standing_big_black' height='25' background='images/gradient.jpg'><b>#" . $XMLTEAM[$i]['team_data']['number'] . " - " . $XMLTEAM[$i]['team_data']['name'] . "</b></td>\n";
						print "<td align='center' background='images/gradient.jpg'>" . $XMLTEAM[$i]['team_data']['car'] . "</td>\n";
						print "<td background='images/gradient.jpg'>" . $XMLTEAM[$i]['race_data']['num_swap'] . "</td>\n";
						print "<td background='images/gradient.jpg'>" . $XMLTEAM[$i]['race_data']['num_pits'] . "</td>\n";
						print "<td background='images/gradient.jpg'>" . $XMLTEAM[$i]['race_data']['num_dt'] . "</td>\n";
						
						print "<td background='images/gradient.jpg'><img src='images/" . $XMLRACE['num_sectors'] . "split". $XMLTEAM[$i]['race_data']['current_sector'] . ".gif'/></td>\n";
						print "<td background='images/gradient.jpg'>\n";
						for ($j=0; $j<count($XMLTEAM[$i]['team_data']['drivers']); $j++)
						{
							if ($XMLTEAM[$i]['team_data']['drivers'][$j]['driver_lfs'] == $XMLTEAM[$i]['race_data']['current_driver'])
								print "<img src='flags/" . $XMLTEAM[$i]['team_data']['drivers'][$j]['driver_country'] . ".gif'/>&nbsp;" . $XMLTEAM[$i]['team_data']['drivers'][$j]['driver_real'];
						}
						print "</td>\n";
						print "<td background='images/gradient.jpg'>" . $XMLTEAM[$i]['race_data'][ 'dist_to_car_in_front'] . "</td>";
						print "<td bgcolor='#ffffff'></td>";
						print "<td bgcolor='#FFD600' class='standing_big_black' align='center' background='images/gradient_yellow.jpg'>" . $XMLTEAM[$i]['race_data'][ 'dist_to_lead_car'] . "</td>\n";
						
						if ($XMLTEAM[$i]['race_data']['penalty_laps'] != 0)
							print "<td bgcolor='#FF0000' class='standing_big_white' align='center'>".$XMLTEAM[$i]['race_data']['penalty_laps']."</td>\n";
						else
							print "<td bgcolor='#FFFFFF'></td>\n";
		
						print "</tr>\n";
						print "<tr>\n";
						print "<td colspan='3'></td>\n";
						print "<td colspan='10' bgcolor='#F2F2F2'>\n";
							print "<table cellspacing='3' cellpadding='0' border='0' bgcolor='#F2F2F2'>\n";
								print "<tr>\n";
									print "<td width='5'></td>\n";
									print "<td width='100'></td>\n";
									for ($j=0; $j<count($XMLTEAM[$i]['team_data']['drivers']); $j++)
										print "<td width='100'><img src='flags/" . $XMLTEAM[$i]['team_data']['drivers'][$j]['driver_country'] . ".gif'/><br/>" . $XMLTEAM[$i]['team_data']['drivers'][$j]['driver_real'] . "</td>\n";
								print "</tr>\n";
								print "<tr>\n";
									print "<td width='5'></td>\n";
									print "<td width='100'>Race dist-%</td>\n";
									for ($j=0; $j<count($XMLTEAM[$i]['team_data']['drivers']); $j++)
										print "<td>". $XMLTEAM[$i]['race_data']['drivers'][$j]['percent'] . "%</td>\n";
								print "</tr>\n";
								print "<tr>\n";
									print "<td width='5'></td>\n";
									print "<td width='100'>Abs. race dist.</td>\n";
									for ($j=0; $j<count($XMLTEAM[$i]['team_data']['drivers']); $j++)
										print "<td>". $XMLTEAM[$i]['race_data']['drivers'][$j]['laps'] . "</td>\n";
								print "</tr>\n";
								print "<tr>\n";
									print "<td width='5'></td>\n";
									print "<td width='100'>Fastest lap</td>\n";
									
									for ($j=0; $j<count($XMLTEAM[$i]['team_data']['drivers']); $j++)
									{
										if ($XMLTEAM[$i]['race_data']['drivers'][$j]['fastest_lap'] != 99999999999)
										{
											if ($XMLTEAM[$i]['race_data']['fastest_lap'] == $XMLTEAM[$i]['race_data']['drivers'][$j]['fastest_lap'])
												print "<td><i>". normalTime($XMLTEAM[$i]['race_data']['drivers'][$j]['fastest_lap'], true) . "</i></td>\n";
											else
												print "<td>". normalTime($XMLTEAM[$i]['race_data']['drivers'][$j]['fastest_lap'], true) . "</td>\n";
										}
										else
											print "<td/>";
									}
								print "</tr>\n";
							print "</table>\n";
						print "</td>\n";
					print "</tr>\n";
					print "<tr>\n";
						print "<td colspan=3/>\n";
						print "<td colspan=10 bgcolor='#F2F2F2'>\n";
							print "<table cellspacing='1' cellpadding='1' border='0' width=100%>\n";
								print "<tr>\n";
										print "<td colspan=2/>\n";
										for ($j=0; $j < min ($XMLTEAM[$i]['race_data']['laps'], 10); $j++)
											print "<td align='center'><b>Lap " . ($XMLTEAM[$i]['race_data']['laps'] - $j) . "</b></td>\n";
								print "</tr>\n";
								
										if ($i > 0)
										{
											print "<tr>\n";
											print "<td><b>" . $XMLTEAM[$i-1]['race_data']['position'] . "</b></td>";
											print "<td>" . $XMLTEAM[$i-1]['team_data']['name'] . "</td>";
											for ($j=0; $j < min ($XMLTEAM[$i]['race_data']['laps'], 10); $j++)
											{
												//if (isset($XMLTEAM[$i-1]['filter_laps'][($XMLTEAM[$i]['race_data']['laps'] - ($j +1))]))
												if (isset($XMLTEAM[$i-1]['filter_laps'][(count($XMLTEAM[$i]['filter_laps']) - ($j +1))]))
												{
													if ($XMLTEAM[$i-1]['filter_laps'][(count($XMLTEAM[$i]['filter_laps']) - ($j +1))]['laptime']
													  < $XMLTEAM[$i]['filter_laps'][(count($XMLTEAM[$i]['filter_laps']) - ($j +1))]['laptime'])
														print "<td bgcolor='#FF0000' align='center'><font color='#FFFFFF'><b>";
													else
														print "<td bgcolor='#38B549' align='center'><font color='#FFFFFF'><b>";
														
													print normalTime($XMLTEAM[$i-1]['filter_laps'][(count($XMLTEAM[$i]['filter_laps']) - ($j +1))]['laptime'], true);
													
												}
												else {
													print "<td>\n";
												}
											/*
												if (isset($XMLTEAM[$i-1]['filter_laps'][($XMLTEAM[$i]['race_data']['laps'] - ($j +1))]))
												{
													if ($XMLTEAM[$i-1]['filter_laps'][($XMLTEAM[$i]['race_data']['laps'] - ($j +1))]['laptime']
													  < $XMLTEAM[$i]['filter_laps'][($XMLTEAM[$i]['race_data']['laps'] - ($j +1))]['laptime'])
														print "<td bgcolor='#FF0000' align='center'><font color='#FFFFFF'><b>";
													else
														print "<td bgcolor='#38B549' align='center'><font color='#FFFFFF'><b>";
														
													print normalTime($XMLTEAM[$i-1]['filter_laps'][($XMLTEAM[$i]['race_data']['laps'] - ($j +1))]['laptime'], true);
													
												}
												else {
													print "<td>\n";
												}
											*/		
												print "</b></font></td>\n";
											}
											print "</tr>\n";
										}
										
										print "<tr>\n";
										print "<td><b>" . $XMLTEAM[$i]['race_data']['position'] . "</b></td>";
										print "<td>" . $XMLTEAM[$i]['team_data']['name'] . "</td>";
										for ($j=0; $j < min ($XMLTEAM[$i]['race_data']['laps'], 10); $j++)
										{
											print "<td align='center'><b>\n";
											if (isset($XMLTEAM[$i]['filter_laps'][(count($XMLTEAM[$i]['filter_laps']) - ($j +1))]))
												print normalTime($XMLTEAM[$i]['filter_laps'][(count($XMLTEAM[$i]['filter_laps']) - ($j +1))]['laptime'], true);
											print "</b></td>\n";
										}
										print "</tr>\n";
										
										if (isset($XMLTEAM[$i+1]))
										{
											print "<tr>\n";
											print "<td><b>" . $XMLTEAM[$i+1]['race_data']['position'] . "</b></td>";
											print "<td>" . $XMLTEAM[$i+1]['team_data']['name'] . "</td>";
											for ($j=0; $j < min ($XMLTEAM[$i]['race_data']['laps'], 10); $j++)
											{
												if (isset($XMLTEAM[$i+1]['filter_laps'][(count($XMLTEAM[$i]['filter_laps']) - ($j +1))]))
												{
													if ($XMLTEAM[$i+1]['filter_laps'][(count($XMLTEAM[$i]['filter_laps']) - ($j +1))]['laptime']
															< $XMLTEAM[$i]['filter_laps'][(count($XMLTEAM[$i]['filter_laps']) - ($j +1))]['laptime'])
														print "<td bgcolor='#FF0000' align='center'><font color='#FFFFFF'><b>";
													else
														print "<td bgcolor='#38B549' align='center'><font color='#FFFFFF'><b>";
													print normalTime($XMLTEAM[$i+1]['filter_laps'][(count($XMLTEAM[$i]['filter_laps']) - ($j +1))]['laptime'], true);
												}
												else
													print "<td>\n";
												print "</b></font></td>\n";
											}
											print "</tr>\n";
										}
											
							print "</table>\n";
						print "</td>\n";
						print "<td/>\n";
					print "</tr>\n";
					
					print "<tr><td colspan='3'/><td colspan='10' align='center'><b>Lapchart</b></td><td/></tr>\n";
					
					print "<tr>\n";
						print "<td colspan=3/>\n";
						print "<td colspan=10 bgcolor='#F2F2F2'>\n";
							print "<table cellspacing='1' cellpadding='1' border='0'>\n";
							
								$column = 0;
								$stintLap = 1;
								
								$swapDriver = true;
								
								for ($j=0; $j < $XMLTEAM[$i]['race_data']['laps']; $j++)
								//for ($j=0; $j < count($XMLTEAM[$i]['filter_laps']); $j++)
								{
									if ($swapDriver || $newStint)
									{
										if ($newStint)
										{
											if (isset($XMLTEAM[$i]['race_data']['pitstops'][$pitInAtKey +1]))
											{
												if ($XMLTEAM[$i]['race_data']['pitstops'][$pitInAtKey +1]['type'] == 'PITOUT')
													print "<td colspan=3><b>Pitstop " . normalTime(($XMLTEAM[$i]['race_data']['pitstops'][$pitInAtKey +1]['time'] - $XMLTEAM[$i]['race_data']['pitstops'][$pitInAtKey]['time'])*1000, false) . "</b></td>";
												else
													print "<td colspan=3>No time recorded</td>";
											}
											else
												print "<td colspan=3>Driver didn't leave pitlane yet.</td>";
										}
										
										if ($column % 3 == 0 && $j == 0)
										{
											$column = 0;										
											print "<tr><td valign='top'><table width='200'><tr><td colspan=3>";
											
											foreach ($XMLTEAM[$i]['team_data']['drivers'] as $driver)
											{
												
												if (strtolower($driver['driver_lfs']) == strtolower($XMLTEAM[$i]['filter_laps'][$j]['driverUserName']))
													print  "<img src='flags/" . $driver['driver_country'] . ".gif'/><br/>" . $driver['driver_real'] . "</td></tr>\n";
											}
										}
										else if ($column % 4 != 0 && $j != 0 && $j != ($XMLTEAM[$i]['race_data']['laps']))
										{											
											print "</table></td><td valign='top'><table width='200'><tr><td colspan=3>\n";
											//print ;
											
											foreach ($XMLTEAM[$i]['team_data']['drivers'] as $driver)
											{
												
												if (strtolower($driver['driver_lfs']) == strtolower($XMLTEAM[$i]['filter_laps'][$j]['driverUserName']))
													print  "<img src='flags/" . $driver['driver_country'] . ".gif'/><br/>" . $driver['driver_real'] . "</td></tr>\n";
											}
										}
										else if ($column % 4 == 0 && $j != 0 && $j != ($XMLTEAM[$i]['race_data']['laps']))
										{
											
											$column = 0;
											print "</table></td></tr><tr><td valign='top'><table width='200'><tr><td colspan=3>\n";
											
											foreach ($XMLTEAM[$i]['team_data']['drivers'] as $driver)
											{
												
												if (strtolower($driver['driver_lfs']) == strtolower($XMLTEAM[$i]['filter_laps'][$j]['driverUserName']))
													print  "<img src='flags/" . $driver['driver_country'] . ".gif'/><br/>" . $driver['driver_real'] . "</td></tr>\n";
											}
										}
										else
										{
											
											print "</table></td></tr>\n";
										}
										
										$stintLap = 1;
										$column++;
									}
									
									print "<tr>\n";
									print "<td>" . ($j+1) . "</td><td>" . $stintLap . "</td><td>" . normalTime($XMLTEAM[$i]['filter_laps'][$j]['laptime'], true) . "</td>\n";
									print "</tr>\n";
									
									$stintLap++;
									
									$newStint = false;									
									for ($k=0; $k < count($XMLTEAM[$i]['race_data']['pitstops']); $k++)
									{
										if (is_array($XMLTEAM[$i]['filter_laps'][$j-1]))
										{											
											if ($XMLTEAM[$i]['race_data']['pitstops'][$k]['mysqlid'] > $XMLTEAM[$i]['filter_laps'][$j-1]['mysqlid'] 
											      && $XMLTEAM[$i]['race_data']['pitstops'][$k]['mysqlid'] < $XMLTEAM[$i]['filter_laps'][$j]['mysqlid'] 
											        && $XMLTEAM[$i]['race_data']['pitstops'][$k]['type'] == 'PITIN')
											{
												$pitInAtKey = $k;
												$newStint = true;
											}
										}
									}
									$swapDriver = false;
									
									if (is_array($XMLTEAM[$i]['filter_laps'][$j-1]) && $XMLTEAM[$i]['filter_laps'][$j-1]['driverUserName'] != $XMLTEAM[$i]['filter_laps'][$j]['driverUserName'])
										$swapDriver = true;
								}
							print "</table>\n";
						print "</td>\n";
						print "<td/>\n";
					print "</tr>\n";
					
					
			print "</table>\n";
			print "</center>\n";
		print "</body>\n";
	print "</html>\n";
?>