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

$userID = 0;
if (isset($_COOKIE['userID'])) { $userID = $_COOKIE['userID']; }
else { header("Location: /tick/login.html"); }

$ID = 0;
$zoom = "4";
$map_type = "G_NORMAL_MAP";
$center_lat = "39.2";
$center_lon = "-96.1419";

$stageID = 0;
if (isset($_COOKIE['stageID'])) { $stageID = $_COOKIE['stageID']; }
else { header("Location: /tick/stages.php"); }

$query = "SELECT events.name as event_name, stages.* FROM events, stages WHERE events.ID = stages.eventID AND stages.ID = $stageID AND events.userID = $userID";
$result = mysql_query($query, $db);

if ($result)
{
    while ($r = mysql_fetch_array($result))
    {
	$event_name = $r['event_name'];
	$name = $r['name'];
    }
}
mysql_free_result($result);

$query = "SELECT * FROM maps WHERE stageID = $stageID";
$result = mysql_query($query, $db);
if ($result)
{
    while ($r = mysql_fetch_array($result))
    {
	$ID = $r['ID'];
	$zoom = $r['zoom'];
	$center_lat = $r['center_lat'];
	$center_lon = $r['center_lon'];
	$current_lat = $r['current_lat'];
	$current_lon = $r['current_lon'];
	$map_type = $r['map_type'];
    }
}
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>
<title>OneTicker :: Maps :: Courses</title>
<link rel=stylesheet type="text/css" href="/tick/tick.css">
<style type="text/css">
    v\:* {
      behavior:url(#default#VML);
    }
</style>
<script src="banner.js" type="text/javascript"></script>
<script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAeMrkW1kxL_t7AGsTQzv_9xRenuji4aHjAULXgP93zutp7Hv6ShRbebgpZJZpLLF2bt806Iyi896c8g" type="text/javascript"></script>

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

var map;
var routes = new Array();
var activeSegmentID = 0;
var original_zoom = <? echo "$zoom"; ?>;
var original_center_lat = "<? echo "$center_lat"; ?>";
var original_center_lon = "<? echo "$center_lon"; ?>";

function onLoad()
{
  var point;
  map = new GMap2(document.getElementById("map"));
  map.addControl(new GLargeMapControl());
  map.addControl(new GMapTypeControl());
  map.setCenter(new GLatLng(original_center_lat, original_center_lon), original_zoom, <? echo "$map_type"; ?>);

  GEvent.addListener(map, 'click', function(marker, point)
  {
    if (marker) // someone clicked a marker
    {
    }
    else  // someone clicked the map
    {
      if (activeSegmentID != 0)
      {
	map.panTo(point);
	routes[activeSegmentID]['course'].push(point);
	showRoute(activeSegmentID);
	makeString();
      }
    }
  });

<?

$query = "SELECT * FROM segments WHERE stageID = $stageID";
$result = mysql_query($query, $db);
if ($result)
{
    while ($r = mysql_fetch_array($result))
    {
      $segmentID = $r['ID'];
      echo "activeSegmentID = " . $segmentID . ";";
      echo "routes[" . $segmentID . "] = new Array();";
      echo "routes[" . $segmentID . "]['color'] = '". $r['color'] . "';";
      echo "routes[" . $segmentID . "]['course'] = new Array();";

      $query2 = "SELECT * FROM maps_coords WHERE segmentID = " . $segmentID . " 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[" . $segmentID . "]['course'].push(point);";
	    }
	  mysql_free_result($result2);
	}
      echo "makeString();";
    }
    mysql_free_result($result);
}

?>

if (typeof document.courses.segmentID != "undefined")
{
  document.courses.segmentID[0].click();
}
}

function makeString()
{
    var result = "";
    var points = routes[activeSegmentID]['course'];
    for (var i=0; i < points.length; i++)
    {
	result = result + points[i].lat() + "," + points[i].lng() + ":";
    }
    eval("document.courses.segment" + activeSegmentID + ".value = result");
    calculate_distance();
}

function showRoute(segmentID)
{
  activeSegmentID = segmentID;
  map.clearOverlays();
  if (segmentID > 0)
  {
    var num_points = routes[segmentID]['course'].length;
    if (num_points > 0)
    { 
      map.addOverlay(new GPolyline(routes[segmentID]['course'], routes[segmentID]['color'], 5)); 
      map.addOverlay(new GMarker(routes[segmentID]['course'][0]));
      map.addOverlay(new GMarker(routes[segmentID]['course'][num_points-1]));
    }
  }
}

function removeLastPoint()
{
  if (routes[activeSegmentID]['course'].length > 0)
  {
    routes[activeSegmentID]['course'].pop();
  }
  showRoute(activeSegmentID);
  makeString();
}

function clearRoute()
{
  routes[activeSegmentID]['course'] = new Array();
  showRoute(activeSegmentID);
  makeString();
}

function calculate_distance()
{
  var miles = 0.0, kilometers = 0.0;
  var points = routes[activeSegmentID]['course'];
  if (points.length < 2)
  {
    eval("document.courses.distance_mi" + activeSegmentID + ".value = '0.0'");
    eval("document.courses.distance_km" + activeSegmentID + ".value = '0.0'");
    return;
  }
  for (var i=0; i < points.length - 1; i++)
  {
    kilometers += points[i].distanceFrom(points[i+1]) / 1000;
  }
  miles = Math.round(kilometers * 0.621371192 * 100) / 100;
  kilometers = Math.round(kilometers * 100) / 100;
  eval("document.courses.distance_mi" + activeSegmentID + ".value = miles");
  eval("document.courses.distance_km" + activeSegmentID + ".value = kilometers");
}

   //]]>
</script>

</head>

<body bgcolor=#000000 marginheight=4 marginwidth=15 leftmargin=15 topmargin=4 onload="onLoad()" onunload="GUnload()">

<script type="text/javascript">banner(6);</script>

<form name="courses" action="/tick/courses_add.php" method="post">
<input type="hidden" name="stageID" value="<? echo $stageID ?>">

<table border=1 cellspacing=2 cellpadding=2>
<tr valign=top>

<td bgcolor="#444444"><table cellpadding=2 cellspacing=2 border=0 width=200>

<tr>

<?

$i = 0;
$query = "SELECT * FROM segments WHERE stageID = $stageID";
$result = mysql_query($query, $db);
if ($result)
{
  while ($r = mysql_fetch_array($result))
  {
    if ($i == 0) { echo '<td><div class=text><b>Choose a course:</b><br>'; }

    $file = "dot_red.png";
    if ($r['color'] == '#ff0202') { $file = "dot_red.png"; }
    if ($r['color'] == '#fe6702') { $file = "dot_orange.png"; }
    if ($r['color'] == '#fefe35') { $file = "dot_yellow.png"; }
    if ($r['color'] == '#02fe35') { $file = "dot_green.png"; }
    if ($r['color'] == '#3567fe') { $file = "dot_blue.png"; }
    if ($r['color'] == '#cc00ff') { $file = "dot_purple.png"; }
    if ($r['color'] == '#ff6699') { $file = "dot_pink.png"; }
    if ($r['color'] == '#999999') { $file = "dot_grey.png"; }
    echo '<input type=hidden name=segment' . $r['ID'] . ' value=""';
    echo "<br><input class=blacktext type=radio name=segmentID onclick=\"javascript:showRoute(" . $r['ID'] . ");\" value=\"" . $r['ID'] . "\"><img height=5 width=40 src=\"/maps/images/" . $file . "\" border=0>&nbsp;" . $r['name'];
    echo "&nbsp;<a href=\"javascript:popup('courses_remove.php?ID=" . $r['ID'] . "', 'segment');\"><img src=\"/images/x.gif\" border=0></a>";
    echo '<br><input type=text class=smtext size=7 maxlength=10 name="distance_mi' . $r['ID'] . '" value="' . $r['distance_mi'] . '" readonly> mi';
    echo '&nbsp;&nbsp;&nbsp;<input type=text class=smtext size=7 maxlength=10 name="distance_km' . $r['ID'] . '" value="' . $r['distance_km'] . '" readonly> km<br>';
    $i++;
  }
  mysql_free_result($result);
  if ($i > 0)
  {
    echo '<p>&nbsp;<br><input class=blacktext type=radio name=segmentID value=0 onclick="javascript:showRoute(0);">...or create a new course:';
  }
  else
  {
    echo '<td><div class=text><b>Create a new course:</b><br>';
  }
}

?>

</div></td>
</tr>

<tr>
<td><div class=text><b>Label: </b>
<input class=blacktext type=text name=name size=20 maxlength=40>
</div></td>
</tr>

<tr>
<td><div class=text><b>Color: </b>
<select name=color class=blacktext>
<option value="#ff0202">red</option>
<option value="#fe6702">orange</option>
<option value="#fefe35">yellow</option>
<option value="#02fe35">green</option>
<option value="#3567fe">blue</option>
<option value="#cc00ff">purple</option>
<option value="#ff6699">pink</option>
<option value="#999999">grey</option>
</select>
</div></td>
</tr>

<tr>
<td align=center><div class=smtext>
<p><input class=blackboldtext type=submit value="save course">
</div></td>
</tr>

<tr>
<td><div class=text>
<p><ul>
<li><a onclick="removeLastPoint();" href="javascript:void(0);">remove last point</a></li>
<li><a onclick="clearRoute();" href="javascript:void(0);">clear segment</a></li>
</ul>
</div></td>
</tr>

</table>
</td>

<td><table cellpadding=0 cellspacing=0 border=0>
<tr valign=top>
<td align=center>
<div id="map" style="width: 600px; height: 400px; border: 2px solid #aaaaaa"></div>
</td>
</tr>

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

</table>
</form>

</body>
</html>
