<?require ("global.php");?>
<?php

$link = "dynamic";
$base_url = "http://www.slowtwitch.com/live/";
if (isset($_GET['link'])) { $link = $_GET['link']; }

$stageID = 0;
if (isset($_GET['ID']))
{
  $stageID = $_GET['ID'];
  
  $query = "SELECT events.name as event_name, events.header_html, events.footer_html, events.banner_html, events.tower_html, events.map_key, events.title, stages.*, DATE_FORMAT(stages.date, '%b %e, %Y') AS datef FROM events, stages WHERE events.ID = stages.eventID AND stages.ID = $stageID";
  $result = mysql_query($query, $db);
  
  if ($result)
  {
    while ($r = mysql_fetch_array($result))
    {
      $event_name = $r['event_name'];
      $map_key = $r['map_key'];
      $title = $r['title'];
      $eventID = $r['eventID'];
      $header_html = $r['header_html'];
      $footer_html = $r['footer_html'];
      $banner_html = $r['banner_html'];
      $tower_html = $r['tower_html'];
      $name = $r['name'];
      $date = $r['datef'];
      $inc_pics = $r['inc_pics'];
      $inc_video = $r['inc_video'];
      $inc_map = $r['inc_map'];
      $start_location = $r['start_location'];
      $end_location = $r['end_location'];
      $standings_gc = $r['standings_gc'];
      $standings_now = $r['standings_now'];
      $standings_gc_url = $r['standings_gc_url'];
      $standings_now_url = $r['standings_now_url'];
      $standings_gc_header = $r['standings_gc_header'];
      $standings_now_header = $r['standings_thru'];
      $profile = $r['profile'];
      $distance = $r['distance'];
      $refresh = $r['refresh'];
      $tick_sequence = $r['tick_sequence'];
      $refresh_type = $r['refresh_type'];
      $popup_link = $r['popup_link'];
      $popup_text = $r['popup_text'];
      $popup_position = $r['popup_position'];
      $extra_html = $r['extra_html'];
      $notes = $r['notes'];
      $static_link = $r['static_url'];
    }
    mysql_free_result($result);
  }

  $text_width = 345;
  $map_width = 450;
  if ($tower_html != "")
    {
      if ($inc_map == 'yes') // text, map, and tower
	{
	  $text_width = 345;
	  $map_width = 330;
	}
      else // text and tower, but no map
	{
	  $text_width = 675;
	}
    }
  else
    {
      if ($inc_map == 'yes') // text and map, but no tower
	{
	  $text_width = 345;
	  $map_width = 450;
	}
      else // only text
	{
	  $text_width = 795;
	}
    }

  $popup_url = "popwindow" . $stageID . ".html";
  if ($link == "dynamic")
  {
    $popup_url = "popwindow.php?ID=" . $stageID;
    $map_key = "ABQIAAAAeMrkW1kxL_t7AGsTQzv_9xRenuji4aHjAULXgP93zutp7Hv6ShRbebgpZJZpLLF2bt806Iyi896c8g";
  }
  
  $query = "SELECT * FROM maps WHERE stageID = $stageID";
  $result = mysql_query($query, $db);
  if ($result)
  {
    while ($r = mysql_fetch_array($result))
    {
      $zoom = $r['zoom'];
      $map_type = $r['map_type'];
      $center_lat = $r['center_lat'];
      $center_lon = $r['center_lon'];
    }
    mysql_free_result($result);
  }
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<META HTTP-EQUIV="charset" CONTENT="UTF-8">
<title><? echo "$title"; ?></title>
<style type="text/css">
<!--

.text
{
font-family : Verdana, Arial, Geneva, Helvetica;
font-size : 11px;
color : #000000;
line-height : 120%;
}

.smtext
{
font-family : Verdana, Arial, Geneva, Helvetica;
font-size : 10px;
color : #000000;
line-height : 120%;
}

.bigtext
{
font-family : Verdana, Arial, Geneva, Helvetica;
font-size : 14px;
color : #000000;
line-height : 120%;
}

A
{
font-family : Verdana, Arial, Geneva, Helvetica;
font-weight : bold;
font-size : 11px;
color : #ed1c24;
text-decoration : none;
}

A:hover
{
color : #ed1c24;
font-family : Verdana, Arial, Geneva, Helvetica;
text-decoration : underline;
font-weight : bold;
}

div#commentary1
{
 overflow: auto;
 width: <? echo $text_width; ?>px;
 height: 455px;
 border-left: 1px gray solid;
 border-bottom: 1px gray solid;
 border-right: 1px gray solid;
 border-top: 1px gray solid;
 padding: 0px 5px 5px 0px;
 margin: 0px;
 display: block;
}

div#commentary2
{
 overflow: auto;
 width: <? echo $text_width; ?>px;
 height: 455px;
 border-left: 1px gray solid;
 border-bottom: 1px gray solid;
 border-right: 1px gray solid;
 border-top: 1px gray solid;
 padding: 0px 5px 5px 0px;
 margin: 0px;
 display: none;
}

-->
</style>
<style type="text/css">
    v\:* {
      behavior:url(#default#VML);
    }
</style>

<? include "../db_templates/google_analytics.html" ?>


<script src="http://maps.google.com/maps?file=api&v=2&sensor=false&key=<? echo $map_key; ?>" type="text/javascript"></script>

<!-- begin script to create random number for ads -->
<script language="Javascript">
<!--
sgi_ord=Math.random()*10000000000000000;
//-->
</script>
<!-- End random number script -->

<script type="text/javascript">
//<![CDATA[

var map;
var text = new Array();
var timeoutID = 0;
var zoom = <? echo $zoom; ?>;
var center_lat = "<? echo $center_lat; ?>";
var center_lon = "<? echo $center_lon; ?>";
var base = new GIcon();
base.image = "images/pin_red.png";
base.shadow = "images/shadow.png";
base.iconSize = new GSize(12, 20);
base.shadowSize = new GSize(22, 20);
base.iconAnchor = new GPoint(6, 20);
base.infoWindowAnchor = new GPoint(5, 1);
loadText();

function createCookie(name,value,days)
{
  if (days)
    {
      var date = new Date();
      date.setTime(date.getTime()+(days*24*60*60*1000));
      var expires = "; expires="+date.toGMTString();
    }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++)
    {
      var c = ca[i];
      while (c.charAt(0)==' ') c = c.substring(1,c.length);
      if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
  return null;
}

function eraseCookie(name)
{
  createCookie(name,"",-1);
}

function show_route(points, color, size)
{
  if (points.length > 0)
  {
    var chunk = 200; 
    if (points.length > chunk)
    {
      show_route(points.slice(0, chunk), color, size);
      show_route(points.slice(chunk), color, size);
    }
    else
    {
      map.addOverlay(new GPolyline(points, color, size));
    }
  }
}

function toggleRefresh()
{
  var refresh_rate = <? echo "$refresh"; ?> * 1000;
  var refresh_type = "<? echo "$refresh_type"; ?>";
  if ((refresh_rate > 0) && (refresh_type == 'javascript'))
  {
    if (timeoutID != 0) { clearTimeout(timeoutID); }
    if (document.which.fresh.checked)
    {
      timeoutID = setTimeout('window.location.reload()', refresh_rate);
    }
  }
}

function popup(url, x, y)
{
  window.open(url,'OneTicker','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=' + y + ',height=' + x);
}

function toggleText()
{
  var on_style, off_style;
  var on = "commentary2";
  var off = "commentary1";
  if (document.which.textorder.checked)
  {
    on = "commentary1";
    off = "commentary2";
    createCookie('textorder', 'recentfirst', 7);
  }
  else
  {
    createCookie('textorder', 'oldfirst', 7);
  }

  if (document.getElementById)
  {
    on_style = document.getElementById(on).style;
    off_style = document.getElementById(off).style;
  }
  else if (document.all)
  {
    on_style = document.all[on].style;
    off_style = document.all[off].style;
  }
  else if (document.layers)
  {
    on_style = document.layers[on].style;
    off_style = document.layers[off].style;
  }
  on_style.display = "none";
  off_style.display = "block";
}

function loadText()
{

<?php

 $i = 0;
 $query = "SELECT *, DATE_FORMAT(ticks.timestamp, '%l:%i %p') AS timef FROM ticks WHERE ticks.stageID = $stageID ORDER BY timestamp";
 $result = mysql_query($query, $db);

 if ($result)
 {
   while ($r = mysql_fetch_array($result))
   {
     $body = chop(addslashes($r['body']));
     $body = preg_replace("((\r\n)+)", " ", $body);
     echo "text[" . $i . "] = new Array();";
     echo "text[" . $i . "]['time'] = '" . $r['timef'] . "';";
     echo "text[" . $i . "]['headline'] = '" . chop(addslashes($r['headline'])) . "';";
     echo "text[" . $i . "]['body'] = '" . $body . "';";
     $i++;
   }
   mysql_free_result($result);
 }

?>

}

function loadCoords()
{
  var routes, marker;
<?

$query = "SELECT * FROM segments WHERE stageID = $stageID";
$result = mysql_query($query, $db);
if ($result)
{
  while ($r = mysql_fetch_array($result))
  {
    echo "routes = new Array();";
    $query2 = "SELECT * FROM maps_coords WHERE segmentID = " . $r['ID'] . " ORDER BY sequence";
    $result2 = mysql_query($query2, $db);
    if ($result2)
    {
      while ($r2 = mysql_fetch_array($result2))
      {
	$lat = $r2['latitude'];
	$lon = $r2['longitude'];
	echo "point = new GLatLng(" . $lat . ", " . $lon . ");";
	echo "routes.push(point);";
      }
      mysql_free_result($result2);
    }
    echo "show_route(routes, '" . $r['color'] . "', 5);";
  }
  mysql_free_result($result);
}

?>

<?

$query = "SELECT * FROM markers WHERE stageID = $stageID";
$result = mysql_query($query, $db);
if ($result)
{
    while ($r = mysql_fetch_array($result))
    {
      echo 'base.image = "images/' . $r['icon'] . '";';
      echo "marker = createMarker(base, new GLatLng(" . $r['latitude'] . ", " . $r['longitude'] . "), '" . $r['ID'] . "', '" . addslashes($r['title']) . "', '" . addslashes($r['text']) . "', '" . $r['image'] . "');";
      echo "map.addOverlay(marker);";
    }
    mysql_free_result($result);
}

?>

}

function initCookies()
{
  var biscuit = readCookie('textorder');
  if (biscuit == null)
    {
      createCookie('textorder', 'oldfirst', 7);
      biscuit = 'oldfirst';
    }
  if (biscuit == 'recentfirst')
    {
      document.which.textorder.checked = true;
    }
}

function onLoad()
{
    map = new GMap2(document.getElementById("map"));
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.addMapType(G_PHYSICAL_MAP);
    map.setCenter(new GLatLng(center_lat, center_lon), zoom, <? echo "$map_type"; ?>);
    loadCoords();
}

function createMarker(icon, point, markerID, title, text, image)
{
  var marker = new GMarker(point, {icon: icon});
  var html = '<table cellpadding=1 cellspacing=1 border=0 width=200>';
  if (image != "")
  {
    html = html + '<tr><td align=center><a target=_blank href="/tick/black.php?img=images/gallery/' + image + '"><img height="130" src="images/gallery/t_' + image + '" border=0></a><br></td></tr>';
  }
  html = html  + '<tr><td><div class=text><b>' + title + '</b></td></tr>';
  html = html + '<tr><td><div class=text>' + text + '</div></td></tr>';
  html = html + '</table>';
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(html);
  });
  return marker;
}

   //]]>
</script>
</head>

<body bgcolor=#ffffff marginheight=4 marginwidth=15 leftmargin=2 topmargin=4 onload="initCookies();toggleRefresh();toggleText();<? if ($inc_map == "yes") { echo ' onLoad()" onunload="GUnload()'; } ?>">

<form name="which">
<table cellspacing=1 cellpadding=1 width="800" border=1 align=center>

<tr> <!-- header -->
<td colspan=2 align=center width="100%"><? echo "$header_html"; ?></td>
</tr>

<tr bgcolor="#cccccc" valign=top> <!-- stage information -->
<td colspan=2><table cellpadding=0 cellspacing=0 border=0 width="100%">
<tr valign=top>
<td><div class=text>

<?

if ($profile != "")
{
  echo "<a target=\"_blank\" href=\"/tick/black.php?img=images/gallery/${profile}\"><img src=\"images/gallery/t_${profile}\" border=0 align=left></a>";
}

?>

&nbsp;<span class=bigtext><b><? echo "$event_name :: $name"; ?></b></span>
<br>&nbsp;<? echo "$date"; ?>&nbsp;-&nbsp;
&nbsp;<? echo "$start_location"; ?><? if ($start_location != $end_location) { echo "&nbsp;to&nbsp;$end_location"; } ?>
<br>&nbsp;<? echo "$distance"; ?>
<br><? if ($notes != "") { echo "&nbsp;${notes}"; } ?>

<?

if ($profile != "")
{
  echo "&nbsp;[<a target=\"_blank\" href=\"/tick/black.php?img=images/gallery/${profile}\">Course Profile</a>]";
}

if (($popup_link != "") && ($popup_position == "notes"))
{
  echo "&nbsp;[";
  echo "<a href=\"javascript:popup('" . $popup_url . "', 600, 380);\">";
  echo "$popup_link" . '</a>]';
}

?>

</div></td>
<td width=280><div class=text><? echo "$banner_html"; ?></div></td>
</tr>
</table></td>
</tr>

<tr bgcolor="#dddddd" valign=top>
<td><table border=0 cellpadding=1 cellspacing=1 width="100%">
<tr>
<td><div class=text>
<b>Other races:&nbsp;</b>
<select name=stage class=text onChange="location.href = document.which.stage.options[document.which.stage.selectedIndex].value">
<option value="<? echo $base_url.$static_link.".html"; ?>">--please select--</option>
<?php

$query = "SELECT * FROM stages WHERE eventID = $eventID AND (status = 'ongoing' OR status = 'complete') ORDER BY date";
$result = mysql_query($query, $db);

if ($result)
{
  while ($r = mysql_fetch_array($result))
  {
    $location = $r['static_url'] . ".html";
    if ($link == "static")
    {
      $location = "stage" . $r['ID'] . ".html";
    }
    $selected = "";
    if ($stageID == $r['ID']) { $selected = " selected"; }
    echo "<option value=\"$base_url${location}\"${selected}>${event_name} ${r['name']}</option>";
  }
}

?>
</select>
</div></td>
<td align=center><div class=text>
<?

if (($refresh_type == 'javascript') && ($refresh != 0))
{
  echo "<input type=checkbox name=fresh value=yes checked onClick='toggleRefresh();'>refresh page";
}

?>
</div></td>

<td align=center><div class=text>
<input type=checkbox name=textorder value=yes onClick='toggleText();'>most recent first
</div></td>

<?

if (($popup_link != "") && ($popup_position == "stages"))
{
  echo "<td align=center><div class=text>[";
  echo "<a href=\"javascript:popup('" . $popup_url . "', 600, 380);\">";
  echo "$popup_link" . '</a>]</div></td>';
}

?>

</tr>
</table></td>

<?

if ($tower_html != "")
{
  echo "<td width=120 height=600 rowspan=3 bgcolor=#ffffff><div class=text>";
  echo "$tower_html";
  echo "</div></td>";
}

?>

</tr>

<tr valign=top>
<td><table cellpadding=2 cellspacing=0 border=1 width="100%">
<tr valign=top>
<td width="345" height=100><div class=text><b><u><? echo "$standings_now_header"; ?></u></b>
&nbsp;&nbsp;<? if ($standings_now_url != "") { echo "[<a target=_blank href=\"${standings_now_url}\">all</a>]"; } ?>
<br><span class=smtext><? echo "$standings_now"; ?></span>
</div></td>
<td><div class=text><b><u><? echo "$standings_gc_header"; ?></u></b>
&nbsp;&nbsp;<? if ($standings_gc_url != "") { echo "[<a target=_blank href=\"${standings_gc_url}\">all</a>]"; } ?>
<br><span class=smtext><? echo "$standings_gc"; ?></span>
</div></td>
</tr>
</table></td>

</tr>

<tr valign=top><td><table border=0 cellpadding=0 cellspacing=0><tr valign=top>
<td><DIV ID="commentary1">
<dl>

<script type="text/javascript">

document.open();
for (var i=0; i < text.length; i++)
{
  document.write("<dt class=text><b>&nbsp;" + text[i]['time'] + "&nbsp;&nbsp;" + text[i]['headline'] + "</b></dt>");
  document.write("<dd class=text>" + text[i]['body'] + "<p></dd>");
}
document.close();

</script>

</dl></div>

<DIV ID="commentary2">
<dl>

<script type="text/javascript">

document.open();
for (var i = text.length - 1; i >= 0; i--)
{
  document.write("<dt class=text><b>&nbsp;" + text[i]['time'] + "&nbsp;&nbsp;" + text[i]['headline'] + "</b></dt>");
  document.write("<dd class=text>" + text[i]['body'] + "<p></dd>");
}
document.close();

</script>

</dl></div></td>

<?

if ($inc_map == "yes")
{
  echo "<td halign=top>";
  echo '<div id="map" style="width: ' . $map_width . 'px; height: 460px; border: 1px solid #979797">';
  echo "</td>";
}

?>

</tr></table>
</td>
</tr>

<?

if ($footer_html != "")
{
  echo "<tr bgcolor=#ffffff valign=middle>";
  echo "<td colspan=2 align=center><div class=text>";
  echo "$footer_html";
  echo "</div></td></tr>";
}

if ($extra_html != "")
{
  echo "<tr bgcolor=#ffffff>";
  echo "<td colspan=2><div class=text>";
  echo "$extra_html";
  echo "</div></td></tr>";
}

?>

<!-- PICS -->
<? 

if ($inc_pics == 'yes')
{
  echo "<tr bgcolor=#ffffff>";
  echo "<td colspan=2 bgcolor=#cccccc><div class=text><b>LATEST PHOTOS</b></div></td>";
  echo "</tr>";
  echo "<tr><td colspan=2>";
  echo '<DIV STYLE="overflow: auto; width: 800px; height: 182px;
            border-top: 1px gray solid; padding:0px; margin: 0px">';
  echo "<table cellpadding=1 cellspacing=1 border=0 width=100%><tr valign=top>";

  $query = "SELECT * FROM pics WHERE stageID = $stageID AND default_flag = 'yes' ORDER BY ID DESC";
  $result = mysql_query($query, $db);
  
  if ($result)
  {
    while ($r = mysql_fetch_array($result))
    {
      $fullname = $r['fullname'];
      $caption = $r['caption'];
      $credit = $r['courtesy_of'];
      
      echo "<td align=center><div class=text><a target=_blank href=\"/tick/black.php?img=images/gallery/${fullname}\"><img border=0 src=\"images/gallery/t_${fullname}\"></a><br><b>${caption}</b>";
      if ($credit != "") { echo "<br><i>by ${credit}</i>"; }
      echo "</div></td>";
    }
    mysql_free_result($result);
  }
  
  echo "</tr></table></td>";
  echo "</tr>";
}

?>

<!-- video -->
<? 

if ($inc_video == 'yes')
{
  echo "<tr bgcolor=#ffffff>";
  echo "<td colspan=2 bgcolor=#cccccc><div class=text><b>LATEST VIDEO</b></div></td>";
  echo "</tr>";
  echo "<tr><td colspan=2>";
  echo '<DIV STYLE="overflow: auto; width: 800px; height: 182px;
            border-top: 1px gray solid; padding:0px; margin: 0px">';
  echo "<table cellpadding=1 cellspacing=1 border=0 width=100%><tr valign=top>";
  
  $query = "SELECT * FROM videos WHERE stageID = $stageID AND default_flag = 'yes' ORDER BY ID DESC";
  $result = mysql_query($query, $db);
  
  if ($result)
  {
    while ($r = mysql_fetch_array($result))
    {
      $thumbname = $r['thumbname'];
      $link = $r['link'];
      $caption = $r['caption'];
      $credit = $r['courtesy_of'];
      
      echo "<td align=center><div class=text><a target=_blank href=\"${link}\"><img border=0 src=\"images/gallery/${thumbname}\"></a>";
      if ($caption != "") { echo "<br>${caption}"; }
      if ($credit != "") { echo " <i>by ${credit}</i>"; }
      echo "</div></td>";
    }
    mysql_free_result($result);
  }
  
  echo "</tr></table></td>";
  echo "</tr>";
}

?>

<tr>
<td colspan=2 align=center bgcolor="#dddddd" width="100%"><div class=text>&copy; 2010-2012 Slowtwitch, Inc.</div></td>
</tr>

</table>
</form>

</body>
</html>
