<?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>\n";
					print "<td align='center'><h1>LFS-Tracker - VR12h</h1></td>\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 = ($XMLRACE['race_start'] + ($XMLRACE['time'] * 60 * 60)) - time();
			$current_hour = floor($diff / (60 * 60));

			if ($diff > 0 && (time() - $XMLRACE['race_start']) < ($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";
				
				for ($i = 0; $i < count($XMLTEAM); $i++)
				{				
					print "<tr bgcolor='#EBEBEB'>\n";
						print "<td bgcolor='#ffffff'><a id='" . $i . "' class='collapsable' border='0'><img src='images/arrow.gif' border='0' /></a></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 align='center' background='images/gradient.jpg'>" . $XMLTEAM[$i]['race_data']['num_swap'] . "</td>\n";
						
						if ($XMLTEAM[$i]['race_data']['pitstops'][count($XMLTEAM[$i]['race_data']['pitstops'])-1]['type'] == "PITIN")
							print "<td align='center' background='images/gradient.jpg'><font color='red'><b>" . $XMLTEAM[$i]['race_data']['num_pits'] . "</b></font></td>\n";
						else
							print "<td align='center' background='images/gradient.jpg'>" . $XMLTEAM[$i]['race_data']['num_pits'] . "</td>\n";
						print "<td align='center' 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 id='" . $i . "-collapse' style=\"display:none;\">\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 "<tr>\n";
									print "<td width='5'/>\n";
									print "<td colspan='" . (count($XMLTEAM[$i]['team_data']['drivers']) +1) . "'>";
									print "<a href='team_" . intval($XMLTEAM[$i]['team_data']['number']) . ".html'>Extended statistics</a> - ";
									print "<a href='unfiltered_" . intval($XMLTEAM[$i]['team_data']['number']) . ".html'>Lapcharts</a>";
									print "</td>\n";
								print "</tr>\n";
							print "</table>\n";
						print "</td>\n";
					print "</tr>\n";
				}
			print "</table>\n";
			print "<table width='800' cellspacing='3' cellpadding='5'>\n";
				print "<tr bgcolor='#FFFDCA'>\n";
					print "<td>Click the <img src='images/arrow.gif' border='0' /> icon to get the full statistics.</td>\n";
				print "</tr>\n";
				print "<tr bgcolor='#FFFDCA'>\n";
					print "<td>* The difference in time to the car in front on the last mutual lap.</td>\n";
				print "</tr>\n";
			print "</table>\n";
			print "</center>\n";
		print "</body>\n";
	print "</html>\n";
?>