<?require ("global.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";
$current_lat = "";
$current_lon = "";
$route = "";

$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>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>OneTicker :: Maps</title>
<link rel=stylesheet type="text/css" href="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_9xTiDdLVRVFb_6ZTq65OUOi0peX-hhSUkw5QRZCCxeeji7sl84l_PgpHCw" type="text/javascript"></script>
<script type="text/javascript">
    //<![CDATA[

  var map;
  var markers = new Array();
  var points = new Array();
  var whichRadio = 0;
  var original_zoom = <? echo "$zoom"; ?>;
  var original_center_lat = "<? echo "$center_lat"; ?>";
  var original_center_lon = "<? echo "$center_lon"; ?>";
  var original_current_lat = "<? echo "$current_lat"; ?>";
  var original_current_lon = "<? echo "$current_lon"; ?>";
  var currentMarker = new GMarker(new GLatLng(original_current_lat, original_current_lon));

function loadCoords()
{
    var point;
<?
$query = "SELECT * FROM maps_coords WHERE stageID = $stageID ORDER BY sequence";
$result = mysql_query($query, $db);
if ($result)
{
    while ($r = mysql_fetch_array($result))
    {
	$latitude = $r['latitude'];
	$longitude = $r['longitude'];
	echo "point = new GLatLng(\"${latitude}\",\"${longitude}\");";
	echo "markers.push(new GMarker(point));";
	echo "points.push(point);";
    }
}
echo "document.maps.route.value = makeString();";
mysql_free_result($result);
?>
}

function makeString()
{
    var result = "";
    for (var i=0; i < points.length; i++)
    {
	result = result + points[i].lat() + "," + points[i].lng() + ":";
    }
    return result;
}

function showRoute(showMarkers)
{
  map.clearOverlays();
  if ((showMarkers == "yes") && (markers.length) > 0)
    {
      map.addOverlay(markers[0]);
      if (markers.length > 1) { map.addOverlay(markers[markers.length-1]); }
    }  
  if (points.length > 0) { map.addOverlay(new GPolyline(points)); }
}

function removeLastPoint()
{
    if (whichRadio == 1)
    {
	if (markers.length > 0) { markers.pop(); }
	if (points.length > 0) { points.pop(); }
	showRoute("yes");
	document.maps.route.value = makeString();
    }
}

function clearRoute()
{
    if (whichRadio == 1)
    {
	markers = new Array();
	points = new Array();
	map.clearOverlays();
	document.maps.route.value = "";
    }
}

function onLoad()
{
    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"; ?>);
    loadCoords();
    showRoute("no");
    map.addOverlay(currentMarker);
    
   GEvent.addListener(map, 'click', function(marker, point)
   {
       if (marker) // someone clicked a marker
       {
	   if (whichRadio == 0)  // in "current" mode
	   {
	       map.removeOverlay(marker);
	       document.maps.current_lat.value = "0.0";
	       document.maps.current_lon.value = "0.0";
	   }
       }
       else  // someone clicked the map
       {
	   if (whichRadio == 0)  // in "current" mode
	   {
	       map.removeOverlay(currentMarker);
	       currentMarker = new GMarker(point);
	       map.addOverlay(currentMarker);
	       document.maps.current_lat.value = point.lat();
	       document.maps.current_lon.value = point.lng();
	   }
	   else  // in "route" mode
	   {
	     markers.push(new GMarker(point));
	     points.push(point);
	     showRoute("yes");
	     document.maps.route.value = makeString();
	   }
       }
   });
}

   //]]>
</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="maps" action="maps_update.php" method="post">
<input type="hidden" name="ID" value="<? echo "$ID"; ?>">
<input type="hidden" name="stageID" value="<? echo "$stageID"; ?>">
<input type="hidden" name="route" value="0">
<input type="hidden" name="current_lat" value="0">
<input type="hidden" name="current_lon" value="0">

<table border=0 cellspacing=1 cellpadding=1 width="650">
<tr><td colspan=3>
<div id="map" style="width: 730px; height: 450px; border: 2px solid #aaaaaa"></div>
</td></tr>

<tr>
<td><div class=text align=right><b>Zoom:</b>&nbsp;</div></td>
<td><div class=text><input class=blacktext type=text name="zoom" value="<? echo "$zoom"; ?>" size=5 maxlength=5>
[<a onclick="document.maps.zoom.value = map.getZoom();" href="javascript:void(0);">set</a>]
</div></td>
<td><div class=text>
<input class=blacktext type=radio name="choose" value="current" onclick="if (whichRadio == 1) { showRoute('no'); map.addOverlay(currentMarker); } whichRadio=0;" checked>&nbsp;<b>Mark current position</b>
</div></td>
</tr>

<tr>
<td><div class=text align=right><b>Center latitude:</b>&nbsp;</div></td>
<td><div class=text>
<input class=blacktext type=text name="center_lat" value="<? echo "$center_lat"; ?>" size=20 maxlength=20>
[<a onclick="document.maps.center_lat.value = map.getCenter().lat();" href="javascript:void(0);">set</a>]
</div></td>
<td rowspan=3><div class=text><input class=blacktext type=radio name="choose" value="route" onclick="if (whichRadio == 0) { showRoute('yes'); } whichRadio=1;">&nbsp;<b>Edit route</b>
<br><ul>
<li><a onclick="removeLastPoint();" href="javascript:void(0);">remove last point</a></li>
<li><a onclick="clearRoute();" href="javascript:void(0);">clear route</a></li>
</ul>
</div></td>
</tr>

<tr>
<td><div class=text align=right><b>Center longitude:</b>&nbsp;</div></td>
<td><div class=text>
<input class=blacktext type=text name="center_lon" value="<? echo "$center_lon"; ?>" size=20 maxlength=20>
[<a onclick="document.maps.center_lon.value = map.getCenter().lng();" href="javascript:void(0);">set</a>]
</div></td>
</tr>

<tr>
<td><div class=text align=right><b>Initial map type:</b>&nbsp;</div></td>
<td><select class=blacktext name="map_type">
<option value="G_NORMAL_MAP"<? if ($map_type == 'G_NORMAL_MAP') { echo " selected"; } ?>>normal</option>
<option value="G_SATELLITE_MAP"<? if ($map_type == 'G_SATELLITE_MAP') { echo " selected"; } ?>>satellite</option>
<option value="G_HYBRID_MAP"<? if ($map_type == 'G_HYBRID_MAP') { echo " selected"; } ?>>hybrid</option>
</select></td>
</tr>

<tr>
<td colspan=2 align=center><div class=text>
[<a onclick="document.maps.zoom.value = original_zoom; document.maps.center_lat.value = original_center_lat; document.maps.center_lon.value = original_center_lon; map.setCenter(new GLatLng(original_center_lat, original_center_lon), original_zoom);" href="javascript:void(0);">reset map to saved</a>]
</div></td>
<td><div class=text>&nbsp;</div></td>
</tr>

<tr><td colspan=3><div class=text>&nbsp;</div></td></tr>

<tr>
<td colspan=3 align=center>
<input class=boldtext type="submit" name="submit" value="update map for <? echo "$event_name $name"; ?>"></td>
</tr>

</table>
</form>

</body>
</html>
