<?php
/**
 ********************************************
 * Masters of Endurance tracker / LFS-Tracker 
 ********************************************
 * 
 * Copyright © 2007 Wolfgang Tom (WolleT)
 * v1.0 originally written by Wolfgang Tom
 * 
 * Copyright © 2009-2011 Michael Booth (Boothy)
 * v2.0 tracker modified by Michael Booth
 * 
 * Thanks also to Casper Roescher (Kaspur).  
 * 
 * Latest tracker version available from the LFS forum thread - http://www.lfsforum.net/showthread.php?t=73414
 * Any code changes/improvements can be submitted for inclusion in the public tracker version. 
 * E-mail me at web@michaelbooth.me.uk or PM me via http://www.lfsforum.net/private.php?do=newpm&u=202039
 * 
 ********************************************
 * 
 * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
 * Version 2, December 2004

 * Copyright © 2004 Sam Hocevar <sam@hocevar.net>

 * Everyone is permitted to copy and distribute verbatim or modified
 * copies of this license document, and changing it is allowed as long
 * as the name is changed.

 * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
 * TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

 * 0. You just DO WHAT THE FUCK YOU WANT TO. 
 * 
 ******************************************** 
 * See http://sam.zoy.org/wtfpl/ for more information
 **/
?>
<!DOCTYPE html>
<html dir='ltr' lang='en'><head><meta charset='utf-8'><title><?php
if ($bodypage == "teamstats") {
    echo "Teamstats #" . $XMLTEAM[$i]['team_data']['number'] . " - " . $windowTitle;
} elseif ($bodypage == "lapcharts") {
    echo "Lapchart #" . $XMLTEAM[$j]['team_data']['number'] . " - " . $windowTitle;
} else {
    echo $windowTitle;
} ?></title><?php
if (strtoupper($XMLRACE['session_status']) == "CHEQUERED FLAG" || strtoupper($XMLRACE['session_status']) ==
    "FINISHED") {
} else {
    echo ("<meta http-equiv='refresh' content='$refreshTime'>");
}
if ($faviconFilepath) {
    if ($faviconFilepath == "leader") {
        $leadingCar = strtolower(substr_replace($XMLTEAM[0]['team_data']['car'], "R", -
            1));
        echo ("<link rel='shortcut icon' href='/favicon$leadingCar.ico' type='image/x-icon'>");
    } else
        echo ("<link rel='shortcut icon' href='$faviconFilepath' type='image/x-icon'>");
}
if ($jsFilepath) {
    echo ("<script type='text/javascript' src='$jsFilepath'></script>");
} ?><link rel='stylesheet' href='<?php echo ($cssFilepath); ?>' media='Screen'><meta name="viewport" content="width=device-width, initial-scale=1.0"><?php if ($bodypage ==
"index")
    echo ('<script>function addLoadEvent(a){var b=window.onload;if(typeof window.onload!="function"){window.onload=a}else{window.onload=function(){b();a()}}}function initCollapse(){if(!document.getElementsByTagName){return}var k="-collapse";var g="collapsable";var d=new Array();var b=document.getElementsByTagName("a");for(var c=0;c<b.length;c++){var j=b[c];if(j.className==g){d[d.length]=j;var l=document.getElementById(j.id+k);l.style.display="none";j.onclick=function f(){h(this)}}}function h(a){a=document.getElementById(a.id+k);a.style.display=(a.style.display=="none")?"":"none"}}addLoadEvent(initCollapse);</script>');
?></head><body id='<?php echo ("$bodypage"); ?>'><?php if ($showtoplinksbar) {
    echo ("<ul id='nav'>");
    $x = $numtoplinksbar;
    for ($f = 1; $f <= $x; $f++) {
        echo "<li><a href='$toplinksbarhref[$f]'>$toplinksbar[$f]</a></li>";
    }
    echo ("</ul>");
} ?><div id='container'><div id='container2'><div id='header'><?php

if ($mainLogoLink or $mainLogoSrc) {
    echo "<h1>";
}

if ($mainLogoLink) {
    echo "<a href='", $mainLogoLink, "'>";
}
if ($mainLogoSrc) {
    if ($mainLogoSrc{0} == "/") {
        $mainLogoSrc2 = substr($mainLogoSrc, 1);
    }
    list($width, $height, $type, $attr) = getimagesize($mainLogoSrc2);
    echo "<img src='", $mainLogoSrc, "' ", $attr, " alt='", $eventFull, "'>";
} else {
    echo $eventFull;
}
if ($mainLogoLink) {
    echo "</a>";
}
if ($mainLogoLink or $mainLogoSrc) {
    echo "</h1>";
}
?>
<dl class='raceData'><?php echo $topInfoCars ? "<dt>Cars</dt><dd>$topInfoCars</dd>" :
""; ?><?php echo $topInfoCars ? "<dt>Track</dt><dd>$topInfoTrack</dd>" : ""; ?>
<?php //main session status if statement
unset($d);
unset($h);
unset($m);
unset($s);
unset($s2);
if (strtoupper($XMLRACE['session_status']) == "PRE-RACE") {
    switch ($trackerMode) {
        case "Qualifying":
            $futureTime = $futureQualStart;
            break;
        case "Practice":
            $futureTime = $futurePracStart;
            break;
        default:
            $futureTime = $futureRaceStart;
    }
    echo ("<dt>Session starts on</dt><dd>" . date("l, F jS Y \a\\t H:i e", $futureTime) .
        "</dd> ");
    $diff = $futureTime - time();
    $current_hour = floor($diff / (60 * 60));
    if ($diff > 0) {
        $d = floor($diff / (24 * 60 * 60));
        $diff -= $d * 24 * 60 * 60;
        $h = floor($diff / (60 * 60));
        $diff -= $h * 60 * 60;
        $m = floor($diff / 60);
        $diff -= $m * 60;
        $s = $diff;
        $s2 = 0;
        if ($d == 1) {
            $d .= " day ";
        } elseif ($d < 1) {
            $d = "";
        } else {
            $d .= " days ";
        }
        if ($h == 1) {
            $h .= " hour ";
        } elseif ($h < 1) {
            $h = "";
        } else {
            $h .= " hours ";
        }
        if ($m == 1) {
            $m .= " minute ";
        } elseif ($m < 1) {
            $m = "";
        } else {
            $m .= " minutes ";
        }
        if ($s == 1) {
            $s2 = " and " . $s . " second ";
        } elseif ($s < 1) {
            $s2 = "";
        } elseif (!$d && !$h && !$m) {
            $s2 = $s . " seconds ";
        } else {
            $s2 = " and " . $s . " seconds ";
        }
        echo ("<dt>Time remaining</dt><dd>" . $d . $h . $m . $s2 . "</dd>");
        echo ("<dt>" . $trackerMode .
            " status</dt><dd><strong>PRE-SESSION</strong></dd>");
    }
} elseif (strtoupper($XMLRACE['session_status']) == "GREEN FLAG" || strtoupper($XMLRACE['session_status']) ==
"RED FLAG") {
    echo ("<dt>Session started on</dt><dd>" . date("l, F jS Y \a\\t H:i e", $XMLRACE['race_start']) .
        "</dd> ");
    //time remaining if diff <=0 then time elapsed
    $diff = ($XMLRACE['race_start'] + ($XMLRACE['timeMins'] * 60) + ($raceFinishOffset *
        60)) - time();
    $current_hour = floor($diff / (60 * 60));
    if ($diff > 0 && (time() - $XMLRACE['race_start']) < ($XMLRACE['timeMins'] * 60 *
        60) + ($raceFinishOffset * 60)) {
        $h = floor($diff / (60 * 60));
        $diff -= $h * 60 * 60;
        $m = floor($diff / 60);
        $diff -= $m * 60;
        $s = $diff;
        if ($h == 1) {
            $h .= " hour ";
        } elseif ($h < 1) {
            $h = "";
        } else {
            $h .= " hours ";
        }
        if ($m == 1) {
            $m .= " minute ";
        } elseif ($m < 1) {
            $m = "";
        } else {
            $m .= " minutes ";
        }
        if ($s == 1) {
            $s2 = " and " . $s . " second ";
        } elseif ($s < 1) {
            $s2 = "";
        } elseif (!$d && !$h && !$m) {
            $s2 = $s . " seconds ";
        } else {
            $s2 = " and " . $s . " seconds ";
        }
        echo ("<dt>Time remaining</dt><dd>" . $h . $m . $s2 . "</dd>");
    } else {
        echo ("<dt>Time remaining</dt><dd>Time elapsed</dd>");
    }
    switch (strtoupper($XMLRACE['session_status'])) {
        case "GREEN FLAG":
            echo "<dt>" . $trackerMode . " status</dt><dd class='gf'><strong>" . strtoupper($XMLRACE['session_status']) .
                "</strong></dd>";
            break;
        case "RED FLAG":
            echo "<dt>" . $trackerMode . " status</dt><dd class='rf'><strong>" . strtoupper($XMLRACE['session_status']) .
                "</strong></dd>";
            break;
    }

} else {
    echo ("<dt>Session started on</dt><dd>" . date("l, F jS Y \a\\t H:i e", $XMLRACE['race_start']) .
        "</dd> ");
    if ($XMLRACE['timeMins'] == 0) {
        echo ("<dt>" . $trackerMode .
            " status</dt><dd class='cf'><strong>CHEQUERED FLAG - Session finished after " .
            $XMLRACE['timeLaps'] . " laps</strong></dd>");
    } else {
        echo ("<dt>" . $trackerMode .
            " status</dt><dd class='cf'><strong>CHEQUERED FLAG - Session finished after " .
            $XMLRACE['timeMins'] . " minutes</strong></dd>");
    }
}
echo "</dl><h2>" . $h2eventName . " &mdash; " . ucfirst($trackerMode) . "</h2></div>"; ?>