<html>
<?php

	$startTime = array_sum(explode(chr(32), microtime()));
	require_once 'variables.php';
	require_once 'sql.php';
	require_once 'functions.php';

	mysql_connect (SQL_HOST, SQL_USER, SQL_PASS);
	mysql_select_db (SQL_DB);

	$myColor1 = '#A1A1A1';
	$myColor2 = '#EBEBEB';
	$myColor3 = '#FFD600';
	$myColor4 = '#F2F2F2';
	
	include 'header.php';

	print "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">\n";

	print "<center>\n";
	//<!-- Top image -->
		print "<table cellspacing=0 cellpadding=0 width=800 border=0>\n";
		print "\t<tr>\n";
		print "\t\t<td><img src='images/banner_kyoto6.jpg'></td>\n";
		print "\t</tr>\n";
		print "\t<tr>\n";
		print "\t\t<td class='standing_big_black'>Live standings</td>\n";
		print "\t</tr>\n";
		print "</table>\n";

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

	//<!-- Race ID -->
		$r_res = mysql_query("SELECT max(id) AS id FROM " . SQL_RACE . " where 1");
		$race_id = mysql_result($r_res, 0, 'id');
		$rid_string = "AND ( race_id='" . $race_id . "')";

	//<!-- Race information, such as start time, time to finish, etc. -->
		$race_res = mysql_query("SELECT * FROM " . SQL_LAP . " WHERE type='RACE_START' " . $rid_string . " ORDER BY race_id asc");
		if (mysql_num_rows($race_res) > 0)
		{
			$race_start = mysql_result($race_res, 0, 'created_on');
			print "Race started on " . date("l, F jS Y \a\\t G:i", $race_start) . ". \n";

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

			$current_hour = floor($diff / (60 * 60));

			if ($diff > 0)
			{
				$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";
			}
			print "<hr class='standings_hr'>\n";
		}

	//<!-- If the race results should be displayed after a certain time in the race that is different from the 'current time' -->
	/*
		$afterhour = intval(htmlspecialchars($_GET['afterhour']));
		if ($afterhour > 0)
		{
			$mos = $race_start + ($afterhour * 60 * 60);
			$timeoffset = "AND created_on < '" . $mos . "'";
		}

		print "<table>\n";
		print "\t<tr>\n";
		print "\t\t<td>Standings after hour </td>\n";

		for ($e=1; $e < max($current_hour, $RACE['time']); $e++)
			print "\t\t<td><a href='stats_simple.php?afterhour=" . $e . "'>" . $e . "</a></td>\n";

		if (max($current_hour, $RACE['time']) == $RACE['time'])
			print "\t\t<td><a href='stats_simple.php?afterhour=" . ($e+1) . "'>" . $e . "</a></td>\n";

		print "\t</tr>\n";
		print "</table>\n";
		*/
	//<!-- The calculation of the standings at the given time -->

		// Put all the database references to Boxenstopp and Dutchfahrtsstrafe in an array, for getting the number of pitstops and drivetroughs
			$pitstop_search = mysql_query ("SELECT * FROM " . SQL_LAP . " WHERE type='MSG' AND value LIKE '%Boxenstopp'" . $rid_string . " $timeoffset ORDER BY race_id asc");
			$dt_search = mysql_query ("SELECT * FROM " . SQL_LAP . " WHERE type='MSG' AND value LIKE '%Durchfahrtsstrafe'" . $rid_string . " $timeoffset ORDER BY race_id asc");
		
			while ($r = mysql_fetch_array($pitstop_search))
				$pitstop_array[] = $r;
				
			while ($r = mysql_fetch_array($dt_search))
				$dt_array[] = $r;
		
		foreach ($TEAMS as $key => $team)
		{
			//<!-- Group the dirvers together and get all the LAPs they drove from the database -->
				$drivers = "lfsworld='" . implode ("' OR lfsworld='", $team['driver_lfs']);
				$sql_res = mysql_query("SELECT * FROM " . SQL_LAP . " WHERE type='LAP' AND (" . $drivers . "') " . $rid_string . " $timeoffset ORDER BY race_id asc, id ASC");
				$sql_dummy = $sql_res;

			//<!-- Put the laps into an array -->
			//<!-- Count the number of driverswaps a team has performed -->
				$laparray = array();

				$time = 0;
				$swap = 0;
				$first = true;
				$currentD = "";

				while ($res = mysql_fetch_object ($sql_res))
				{
					$laparray[] = $res->lap;

					if ($first == true)
					{
						$currentD = $res->lfsworld;
						$first = false;
					}
					else
					{
						if ($currentD != $res->lfsworld)
						{
							$swap++;
							$currentD = $res->lfsworld;
						}
					}
					$time = $res->created_on;
				}
				$time = ($time - $race_start)*100;

			// Search for pitstops done by this team
				$pitted = 0;
				foreach ($pitstop_array as $pitstop_value)
				{
					if (substr($pitstop_value['value'], 0, 2) == addzeros($team['number']))
						$pitted++;
				}
	
			// Search for drivetroughs done by this team
				$dt = 0;
				foreach ($dt_array as $dt_value)
				{
					if (substr($dt_value['value'], 0, 2) == addzeros($team['number']))
						$dt++;
				}
			
			// Put the results obtained so far into the result array
				$TEAMS_RES[$key]['laps'] = 0;
				$TEAMS_RES[$key]['time'] = 0;
				$TEAMS_RES[$key]['swaps'] = 0;
				$TEAMS_RES[$key]['pits'] = 0;
				$TEAMS_RES[$key]['dt'] = 0;

				$TEAMS_RES[$key]['id'] = $key;
				$TEAMS_RES[$key]['laps'] = countLaps ($laparray) + $team['penalty_laps'] + $team['standings_correction'];
				$TEAMS_RES[$key]['time'] = $time;
				$TEAMS_RES[$key]['swaps'] = $swap;
				$TEAMS_RES[$key]['pit'] = $pitted;
				$TEAMS_RES[$key]['dt'] = $dt;

			//<!-- Determin the current position of the car on the track -->
				$sql_res2 = mysql_query("SELECT * FROM " . SQL_LAP . " WHERE (type='LAP' OR type='SP1' OR type='SP2' OR type='SP3') AND (" . $drivers . "') " .  $rid_string . " $timeoffset ORDER BY race_id ASC, id DESC LIMIT 0, 1");
				if ($TEAMS_RES[$key]['laps'] > 0)
				{
					if (mysql_result($sql_res2, 0, 'type') == "LAP")
						$TEAMS_RES[$key]['cur_pos'] = 0;

					if (mysql_result($sql_res2, 0, 'type') == "SP1")
						$TEAMS_RES[$key]['cur_pos'] = 1;

					if (mysql_result($sql_res2, 0, 'type') == "SP2")
						$TEAMS_RES[$key]['cur_pos'] = 2;

					if (mysql_result($sql_res2, 0, 'type') == "SP3")
						$TEAMS_RES[$key]['cur_pos'] = 3;

					$TEAMS_RES[$key]['current_driver'] = "";
					$TEAMS_RES[$key]['current_driver'] = mysql_result($sql_res2, 0, 'lfsworld');
					//$TEAMS_RES[$key]['current_driver'] = @mysql_result($sql_res, ($TEAMS_RES[$key]['laps']-1), 'lfsworld');
				}
				else
				{
					$TEAMS_RES[$key]['cur_pos'] = 0;
					$TEAMS_RES[$key]['current_driver'] = "";
				}
		}

		//<!-- Sort the results according to the 'sort_standings' sorting function -->
			usort ($TEAMS_RES, "sort_standings");

	print "<table cellspacing=0 cellpadding=0 border=0>\n";

	print "\t<tr bgcolor='$myColor1'>\n";
		print "\t\t<td width='20' bgcolor='#ffffff'></td>\n";
		print "\t\t<td width='50' bgcolor='$myColor1' align='center' class='standing_big_white' background='images/gradient_gray.jpg'>Pos</td>\n";
		print "\t\t<td width=3 bgcolor='#FFFFFF'></td>\n";
		print "\t\t<td width=300 bgcolor='$myColor1' class='standing_big_blue' background='images/gradient_gray.jpg'><b>Team name</b></td>\n";
		print "\t\t<td width=45 bgcolor='$myColor1' align=center background='images/gradient_gray.jpg'>Car</td>\n";

		print "\t\t<td width=35 bgcolor='$myColor1' background='images/gradient_gray.jpg'>Swap</td>\n";
		print "\t\t<td width=25 bgcolor='$myColor1' background='images/gradient_gray.jpg'>Pit</td>\n";
		print "\t\t<td width=25 bgcolor='$myColor1' background='images/gradient_gray.jpg'>DT</td>\n";
		print "\t\t<td width=50 bgcolor='$myColor1' background='images/gradient_gray.jpg'>Sector</td>\n";
		print "\t\t<td width=125 bgcolor='$myColor1' background='images/gradient_gray.jpg'>Current driver</td>\n";
		print "\t\t<td width=75 bgcolor='$myColor1' background='images/gradient_gray.jpg'>Car in front</td>\n";
		print "\t\t<td width=3 bgcolor='#FFFFFF'></td>\n";
		print "\t\t<td width=100 bgcolor='$myColor1' class='standing_big_white' align=center background='images/gradient_gray.jpg'>Gap</td>\n";
	print "\t</tr>\n";
	print "\t<tr>\n";
		print "\t\t<td colspan=12 height=5></td>\n";
	print "\t</tr>\n";

	$stop = count ($TEAMS);
	$i = 0;

	for ($i=0; $i < $stop; $i++)
	{
		$k = $TEAMS_RES[$i]['id'];

		if (!$TEAMS[$k]['display'])
			continue;

		print "\t<tr bgcolor='$myColor2'>\n";
		
		print "\t\t<td bgcolor='#ffffff'><a id='$i' class='collapsable' border=0><img src='images/arrow.gif' border=0></a></td>\n";
		print "\t\t<td bgcolor='$myColor1' align='center' class='standing_big_white' background='images/gradient_gray.jpg'>" . ($i+1) . "</td>\n";
		print "\t\t<td bgcolor='#ffffff'></td>\n";
		print "\t\t<td class='standing_big_black' height=25 background='images/gradient.jpg'><b>" . $TEAMS[$k]['team_name'] . " - #" . $TEAMS[$k]['number'] . "</b></td>\n";
		print "\t\t<td align=center background='images/gradient.jpg'>" . $TEAMS[$k]['car'] . "</td>\n";
		print "\t\t<td background='images/gradient.jpg'>" . $TEAMS_RES[$i]['swaps'] . "</td>\n";
		print "\t\t<td background='images/gradient.jpg'>" . $TEAMS_RES[$i]['pit'] . "</td>\n";
		print "\t\t<td background='images/gradient.jpg'>" . $TEAMS_RES[$i]['dt'] . "</td>\n";
		print "\t\t<td background='images/gradient.jpg'><img src='images/split" . $TEAMS_RES[$i]['cur_pos'] . ".jpg'></td>\n";

		$flagString = "\t\t<td></td>\n";
		foreach ($TEAMS[$k]['driver_lfs'] as $h => $lfsworld)
		{
			if (strtolower($lfsworld) == strtolower($TEAMS_RES[$i]['current_driver']))
				$flagString =  "\t\t<td background='images/gradient.jpg'><img src='flags/" . strtolower($TEAMS[$k]['driver_country'][$h]) . ".gif'>&nbsp;" . $TEAMS[$k]['driver_real'][$h] . "</td>\n";
		}
		print $flagString;

		if ($i == 0)
			print "\t\t<td background='images/gradient.jpg'></td>\n";
		else
		{
			$distL = $TEAMS_RES[$i-1]['laps'] - $TEAMS_RES[$i]['laps'];
			$distT = $TEAMS_RES[$i-1]['time'] - $TEAMS_RES[$i]['time'];

			if ($distL > 0)
				print "\t\t<td background='images/gradient.jpg'>+" . $distL . " laps</td>\n";
			else
				print "\t\t<td background='images/gradient.jpg'>+" . normalTime($distT, false) . "</td>\n";
		}

		print "\t\t<td bgcolor='#ffffff'></td>\n";

		if ($i == 0)
			print "\t\t<td bgcolor='$myColor3' class='standing_big_black' align=center background='images/gradient_yellow.jpg'>Lap " . $TEAMS_RES[$i]['laps'] . "</td>\n";
		else
		{
			$distL = $TEAMS_RES[0]['laps'] - $TEAMS_RES[$i]['laps'];
			$distT = $TEAMS_RES[0]['time'] - $TEAMS_RES[$i]['time'];

			if ($distL > 0)
				print "\t\t<td bgcolor='$myColor3' class='standing_big_black' align=center background='images/gradient_yellow.jpg'>+" . $distL . " laps</td>\n";
			else
				print "\t\t<td bgcolor='$myColor3' class='standing_big_black' align=center background='images/gradient_yellow.jpg'>+" . normalTime($distT, false) . "</td>\n";
		}
		print "\t</tr>\n";
		
		/*
			COLLAPSE MENU STARTS HERE
										*/
										
		print "\t<tr id='$i-collapse'>\n";
		print "\t\t<td colspan=3></td>\n";
		print "\t\t<td colspan=10 bgcolor='$myColor4'>\n";
		
		print "\t\t\t<table cellspacing=3 cellpadding=0 border=0 bgcolor='$myColor4'>\n";
			print "\t\t\t\t<tr>\n";
				print "\t\t\t\t\t<td width=5></td>\n";
				print "\t\t\t\t\t<td width=100></td>\n";

		$dnum = count ($TEAMS[$k]['driver_lfs']);

		for ($l=0; $l < $dnum; $l++)
		{
			if ($TEAMS[$k]['driver_active'][$l])
			{
				if (strtolower($TEAMS[$k]['driver_lfs'][$l]) == strtolower($TEAMS_RES[$i]['current_driver']))
					print "\t\t\t\t\t<td width=100><img src='flags/" . strtolower($TEAMS[$k]['driver_country'][$l]) . ".gif'><br><b>" . format_name($TEAMS[$k]['driver_real'][$l]) . "</b></td>\n";
				else
					print "\t\t\t\t\t<td width=100><img src='flags/" . strtolower($TEAMS[$k]['driver_country'][$l]) . ".gif'><br>" . format_name($TEAMS[$k]['driver_real'][$l]) . "</td>\n";
			}
		}
			print "\t\t\t\t</tr>\n";
			
		// RACE DISTANCE IN PERCENT

			print "\t\t\t\t<tr>\n";
				print "\t\t\t\t\t<td width=5></td>\n";
				print "\t\t\t\t\t<td width=100>Race dist-%</td>\n";

		for ($l=0; $l < $dnum; $l++)
		{
			if ($TEAMS[$k]['driver_active'][$l])
			{				
				$drv_qry = mysql_query("SELECT value FROM " . SQL_LAP . " WHERE lfsworld='" . $TEAMS[$k]['driver_lfs'][$l] . "' AND type='LAP'" . $rid_string . " ORDER BY race_id asc");
	
				$drv_t = 0;
				$drv_l[$k][$l]['laps'] = 0; // total amount of laps
				$drv_l[$k][$l]['fl'] = 999999999;	// fastest lap
	
				while ($res = mysql_fetch_object($drv_qry))
				{
					$drv_t += $res->value;
	
					if ($res->value < $drv_l[$k][$l]['fl'])
					{
						$drv_l[$k][$l]['fl'] = $res->value;
					}
	
					$drv_l[$k][$l]['laps']++;
				}
	
				$percent = round((($drv_l[$k][$l]['laps'] / $TEAMS_RES[$i]['laps']) * 100), 1);
	
				print "\t\t\t\t\t<td>" . $percent . "%</td>\n";
			}
		}

			print "\t\t\t\t</tr>\n";

		// NUMBER OF LAPS

			print "\t\t\t\t<tr>\n";
				print "\t\t\t\t\t<td width=5></td>\n";
				print "\t\t\t\t\t<td width=100>Abs. race dist.</td>\n";

		for ($l=0; $l < $dnum; $l++)
		{
			if ($TEAMS[$k]['driver_active'][$l])
				print "\t\t\t\t\t<td>" . $drv_l[$k][$l]['laps'] . "</td>\n";
		}

			print "\t\t\t\t</tr>\n";

		// FASTEST LAP
		
		$fl = 99999999;
		$fr = 999;
		
		foreach ($drv_l[$k] as $o => $drv_t)
		{
			if ($drv_t['fl'] < $fl)
			{
				$fl = $drv_t['fl'];
				$fr = $o;
			}
		}

			print "\t\t\t\t<tr>\n";
				print "\t\t\t\t\t<td width=5></td>\n";
				print "\t\t\t\t\t<td width=100>Fastest lap</td>\n";

		for ($l=0; $l < $dnum; $l++)
		{
			if ($TEAMS[$k]['driver_active'][$l])
			{	
				if ($drv_l[$k][$l]['laps'] > 0)
				{
					if ($fr == $l)
						print "\t\t\t\t\t<td><i>" . normalTime($drv_l[$k][$l]['fl']) . "</i></td>\n";
					else
						print "\t\t\t\t\t<td>" . normalTime($drv_l[$k][$l]['fl']) . "</td>\n";
				}
				else
					print "\t\t\t\t\t<td>&nbsp;</td>\n";
			}
		}
		print "\t\t\t\t</tr>\n";
		print "\t\t\t</table>\n";
		
		/*
			LAST LAPS COMPARE
								*/
		
		print "\t\t\t<table cellspacing=3 cellpadding=0 border=0>\n";
		
		
		print "\t\t\t\t<tr>\n";
		print "\t\t\t\t\t<td width=5></td>\n";
		print "\t\t\t\t\t<td colspan=" . (COMPARE_LAPS+1) . " width=150><b>Last six lap comparision.</b>*</td>\n";
		print "\t\t\t\t\t<td align=center><b>6LT</b>**</td>\n";
		print "\t\t\t\t</tr>\n";
		
		
		// CAR IN FRONT
		
		if ($i > 0)
		{
			print "\t\t\t\t<tr>\n";
			print "\t\t\t\t\t<td width=5></td>\n";
			print "\t\t\t\t\t<td width=100>Car in front</td>\n";
			
			$drivers1 = "lfsworld='" . implode ("' OR lfsworld='", $TEAMS[$TEAMS_RES[$i-1]['id']]['driver_lfs']);		
			$drivers2 = "lfsworld='" . implode ("' OR lfsworld='", $TEAMS[$k]['driver_lfs']);
		
			$ZLT = 0;
			$calc = 0;
			
			$sql_res1 = mysql_query("SELECT * FROM " . SQL_LAP . " WHERE type='LAP' AND (" . $drivers1 . "') " . $rid_string . " ORDER BY race_id asc, id DESC LIMIT 0," . COMPARE_LAPS);
				
			$sql_res2 = mysql_query("SELECT * FROM " . SQL_LAP . " WHERE type='LAP' AND (" . $drivers2 . "') " . $rid_string . " ORDER BY race_id asc, id DESC LIMIT 0," . COMPARE_LAPS);
				
			$r = 0;
			
			if (mysql_num_rows($sql_res1) > 0 && mysql_num_rows($sql_res2) > 0)
			{
				while ($res = mysql_fetch_object($sql_res2))
				{
					$t1 = $res->value;
					$t2 = mysql_result($sql_res1, $r, 'value');					
					
					$ZLT += ($t1 - $t2);
					$calc = 1;
				
					if (($t1 - $t2) > 0)
					{
						$bg = '#FF0000';
						$string = "+" . normalTime((-1)*($t1 - $t2));
					}
					else
					{
						$bg = '#38B549';
						$string = "-" . normalTime($t1 - $t2);
					}
					
					print "\t\t\t\t\t<td bgcolor='" . $bg . "' align='center' width=75><font color='#ffffff'><b>" . $string . "</b></font></td>\n";
					
					$r++;
				}				
			}
			
			while ($r < COMPARE_LAPS)
			{
				print "\t\t\t\t\t<td bgcolor='#959595' align='center' width=75><font color='#ffffff'><b>-</b></font></td>\n";
				$r++;
			}
			
			if ($ZLT > 0)
			{
				$bg = '#FF0000';
				$string = "+" . normalTime((-1)*($ZLT));
			}
			else
			{
				$bg = '#38B549';
				$string = "-" . normalTime($ZLT);
			}
			
			if ($calc != 1)
			{
				$bg = '#959595';
				$string = "-";
			}
			
			print "\t\t\t\t\t<td bgcolor='" . $bg . "' align='center' width=75><font color='#ffffff'><b>" . $string . "</b></font></td>\n";
			print "\t\t\t\t</tr>\n";
		}
			
		
		// CAR BEHIND
		
		
		if ($i < ($stop -1))
		{
			print "\t\t\t\t<tr>\n";
			print "\t\t\t\t\t<td width=5></td>\n";
			print "\t\t\t\t\t<td width=100>Car behind</td>\n";
			
			
			$drivers1 = "lfsworld='" . implode ("' OR lfsworld='", $TEAMS[$k]['driver_lfs']);
			$drivers2 = "lfsworld='" . implode ("' OR lfsworld='", $TEAMS[$TEAMS_RES[$i+1]['id']]['driver_lfs']);
			
			$ZLT = 0;
			$calc = 0;
			
			$sql_res1 = mysql_query("SELECT * FROM " . SQL_LAP . " WHERE type='LAP' AND (" . $drivers1 . "') " . $rid_string . " ORDER BY race_id asc, id DESC LIMIT 0," . COMPARE_LAPS);
				
			$sql_res2 = mysql_query("SELECT * FROM " . SQL_LAP . " WHERE type='LAP' AND (" . $drivers2 . "') " . $rid_string . " ORDER BY race_id asc, id DESC LIMIT 0," . COMPARE_LAPS);
			
			$r = 0;
			
			if (mysql_num_rows($sql_res1) > 0 && mysql_num_rows($sql_res2) > 0)
			{
				while ($res = mysql_fetch_object($sql_res1))
				{
					$t1 = $res->value;
					
					if (mysql_num_rows($sql_res2) >= ($r+1))
					{
						$t2 = mysql_result($sql_res2, $r, 'value');
					
						$ZLT += $t1 - $t2;
						$calc = 1;
					
						if (($t1 - $t2) > 0)
						{
							$bg = '#FF0000';
							$string = "+" . normalTime((-1)*($t1 - $t2));
						}
						else
						{
							$bg = '#38B549';
							$string = "-" . normalTime($t1 - $t2);
						}
						
						print "\t\t\t\t\t<td bgcolor='" . $bg . "' align='center' width=75><font color='#ffffff'><b>" . $string . "</b></font></td>\n";
					}
					else
					{
						print "\t\t\t\t\t<td bgcolor='#959595' align='center' width=75><font color='#ffffff'><b>-</b></font></td>\n";
					}					
					$r++;
				}
			}
				
			while ($r < COMPARE_LAPS)
			{
				print "\t\t\t\t\t<td bgcolor='#959595' align='center' width=75><font color='#ffffff'><b>-</b></font></td>\n";
				$r++;
			}
			
			if ($ZLT > 0)
			{
				$bg = '#FF0000';
				$string = "+" . normalTime((-1)*($ZLT));
			}
			else
			{
				$bg = '#38B549';
				$string = "-" . normalTime($ZLT);
			}
			
			if ($calc != 1)
			{
				$bg = '#959595';
				$string = "-";
			}
			
			print "\t\t\t\t\t<td bgcolor='" . $bg . "' align='center' width=75><font color='#ffffff'><b>" . $string . "</b></font></td>\n";
						
			print "\t\t\t\t</tr>\n";
		}	
		
		// LAPTIMES
		
		print "\t\t\t\t<tr>\n";
		
		print "\t\t\t\t\t<td width=5></td>\n";
		print "\t\t\t\t\t<td colspan=" . (COMPARE_LAPS+1) . " width=150><b>Last six laptimes.</b></td>\n";
		print "\t\t\t\t\t<td align=center><b>Race avg.</b></td>\n";
		
		print "\t\t\t\t</tr>\n";
		print "\t\t\t\t<tr>\n";
		
		print "\t\t\t\t\t<td width=5></td>\n";
		print "\t\t\t\t\t<td width=100>Laptimes</td>\n";
		
		$drivers1 = "lfsworld='" . implode ("' OR lfsworld='", $TEAMS[$k]['driver_lfs']);			
		$sql_res = mysql_query("SELECT * FROM " . SQL_LAP . " WHERE type='LAP' AND (" . $drivers1 . "') " . $rid_string . " ORDER BY race_id asc, id DESC LIMIT 0," . COMPARE_LAPS);
			
		$u = 0;
				
		while ($res = mysql_fetch_object($sql_res))
		{
			print "\t\t\t\t\t<td align=center>" . normalTime($res->value) . "</td>\n";
			$u++;
		}
		
		while ($u < COMPARE_LAPS)
		{
			print "\t\t\t\t\t<td align=center>-</td>\n";
			$u++;
		}
		
		$sql_res = mysql_query("SELECT * FROM " . SQL_LAP . " WHERE type='LAP' AND (" . $drivers1 . "') " . $rid_string . " ORDER BY race_id asc, id DESC");
		
		$avgTotal = 0;
		$avgNum = 0;
		unset ($res);
		
		$avgTotal = $TEAMS_RES[$i]['time'];
		$avgNum = $TEAMS_RES[$i]['laps'];
		
		/*
		while ($res = mysql_fetch_object($sql_res))
		{
			$avgTotal += $res->value;
			$avgNum++;
		}
		*/
		
		if ($avgNum > 0)
			print "\t\t\t\t\t<td align=center>" . normalTime(ceil($avgTotal / $avgNum)) . "</td>\n";
		else
			print "\t\t\t\t\t<td align=center>-</td>\n";
		
		print "\t\t\t\t</tr>\n";
			
		print "\t\t\t</table>\n";
		print "\t\t</td>\n";
		print "\t</tr>\n";
	}
	print "</table>\n";

	print "<table width=800 cellspacing=3 cellpadding=5>\n";
	
	print "\t<tr bgcolor='#FFFDCA'>\n";
	print "\t\t<td>Click the <img src='images/arrow.gif' border=0> icon to get the full statistics.</td>\n";
	print "\t</tr>\n";
	
	print "\t<tr bgcolor='#FFFDCA'>\n";
	print "\t\t<td>* Compared laps may not match if car behind or in front is one lap down/ahead.</td>\n";
	print "\t</tr>\n";
	
	print "\t<tr bgcolor='#FFFDCA'>\n";
	print "\t\t<td>** 6LT : Six Lap Total.</td>\n";
	print "\t</tr>\n";
	
	print "\t<tr bgcolor='#FFFDCA'>\n";
	print "\t\t<td><b>Driver in bold</b> is currently in the car.</td>\n";
	print "\t</tr>\n";
		
	print "</table>\n";
	$stopTime = array_sum(explode(chr(32), microtime()));;
?>
</html>