<?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 :: Markers</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_9xRenuji4aHjAULXgP93zutp7Hv6ShRbebgpZJZpLLF2bt806Iyi896c8g" type="text/javascript"></script>

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

var map, currentMarker;
var geocoder = new GClientGeocoder();
var zoom = <? echo "$zoom"; ?>;
var center_lat = "<? echo "$center_lat"; ?>";
var center_lon = "<? echo "$center_lon"; ?>";
var pins = new Array();
pins[0] = 'pin_red.png';
pins[1] = 'pin_orange.png';
pins[2] = 'pin_yellow.png';
pins[3] = 'pin_green.png';
pins[4] = 'pin_blue.png';
pins[5] = 'pin_purple.png';
pins[6] = 'pin_pink.png';
pins[7] = 'pin_grey.png';

var base = new GIcon();
base.image = "/maps/images/" + pins[0];
base.shadow = "/images/balloons/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);

function onLoad()
{
  var marker, routes;
  map = new GMap2(document.getElementById("map"));
  map.addControl(new GLargeMapControl());
  map.addControl(new GMapTypeControl());
  map.setCenter(new GLatLng(center_lat, center_lon), zoom, <? echo "$map_type"; ?>);
  currentMarker = new GMarker(new GLatLng(0.0, 0.0), {icon: base});
  map.addOverlay(currentMarker);

  GEvent.addListener(map, 'click', function(marker, point)
  {
    if (marker) // someone clicked a marker
    {
    }
    else  // someone clicked the map
    {
      currentMarker.setPoint(point);
      document.markers.latitude.value = point.lat();
      document.markers.longitude.value = point.lng();
    }
  });

<?

$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 "if (routes.length > 0) { map.addOverlay(new GPolyline(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 = \"/maps/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);
}

?>
document.markers.latitude.value = "<? echo "$center_lat"; ?>";
document.markers.longitude.value = "<? echo "$center_lon"; ?>";
document.markers.icon[0].click();
}

function showAddress(address)
{
  geocoder.getLatLng(address, function(point)
  {
    if (!point) { alert("Sorry! I couldn't find that address."); }
    else
    {
      map.panTo(point);
      currentMarker.setPoint(point);
      document.markers.latitude.value = point.lat();
      document.markers.longitude.value = point.lng();
    }
  }
  );
}

function changePin(which)
{
  currentMarker.getIcon().image = "/maps/images/" + pins[which];
  map.removeOverlay(currentMarker); 
  map.addOverlay(currentMarker);
}

function whichNewPin()
{
  for (var i=0; i < document.markers.icon.length; i++)
    {
      if (document.markers.icon[i].checked == true)
	return i;
    }
  return 0;
}

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/html/black.php?img=/tick/images/gallery/' + image + '"><img height="130" src="/tick/images/gallery/t_' + image + '" border=0></a><br></td></tr>';
  }
  html = html  + '<tr><td><div class=blacktext><b>' + title + '</b></td></tr>';
  html = html + '<tr><td><div class=blacktext>' + text + '</div></td></tr>';
  html = html + "<tr><td><div class=blacktext><a href=\"javascript:popup('markers_remove.php?ID=" + markerID + "', 'marker');\"><img src=\"/images/x.gif\" border=0></a>remove this marker</div></td></tr>";
  html = html + '</table>';
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(html);
  });
  return marker;
}

   //]]>
</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="markers" enctype="multipart/form-data" action="markers_add.php" method="post">
<input type="hidden" name="stageID" value="<? echo $stageID ?>">

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

<td bgcolor="#444444"><table cellpadding=2 cellspacing=2 border=0 width="180">
<tr>
<td colspan=2><div class=smtext>
<b>Title</b>
<br><input class=blacktext type=text size=35 maxlength=120 name=title value="<? echo "$title"; ?>">
</div></td>
</tr>

<tr>
<td colspan=2><div class=smtext>
<b>Image</b>
<br><input class=blacktext type=file name="pic" size=30>
<p><ul class=text>
<li>image will be resized automatically</li>
<li>uploaded filesize limit is 800K</li>
</ul>
</div></td>
</tr>

<tr>
<td colspan=2><div class=smtext>
<b>Text</b>
<br><textarea class=blacktext name="text" cols=35 rows=4 wrap="virtual"><? echo "$text"; ?></textarea>
</div></td>
</tr>

<tr>
<td><div class=smtext>
<b>Latitude</b>
<br><input class=blacktext type=text size=16 maxlength=20 name=latitude value="<? echo "$latitude"; ?>" readonly>
</div></td>
<td><div class=smtext>
<b>Longitude</b>
<br><input class=blacktext type=text size=16 maxlength=20 name=longitude value="<? echo "$longitude"; ?>" readonly>
</div></td>
</tr>

<tr>
<td colspan=2><table cellpadding=2 cellspacing=2 border=0 width="100%">
<tr>
<td><input type=radio name=icon value="pin_red.png" checked onclick="javascript:changePin(0);"><img src="/maps/images/pin_red.png" border=0></td>
<td><input type=radio name=icon value="pin_orange.png" onclick="javascript:changePin(1);"><img src="/maps/images/pin_orange.png" border=0></td>
<td><input type=radio name=icon value="pin_yellow.png" onclick="javascript:changePin(2);"><img src="/maps/images/pin_yellow.png" border=0></td>
<td><input type=radio name=icon value="pin_green.png" onclick="javascript:changePin(3);"><img src="/maps/images/pin_green.png" border=0></td>
</tr>
<tr>
<td><input type=radio name=icon value="pin_blue.png" onclick="javascript:changePin(4);"><img src="/maps/images/pin_blue.png" border=0></td>
<td><input type=radio name=icon value="pin_purple.png" onclick="javascript:changePin(5);"><img src="/maps/images/pin_purple.png" border=0></td>
<td><input type=radio name=icon value="pin_pink.png" onclick="javascript:changePin(6);"><img src="/maps/images/pin_pink.png" border=0></td>
<td><input type=radio name=icon value="pin_grey.png" onclick="javascript:changePin(7);"><img src="/maps/images/pin_grey.png" border=0></td>
</tr>
</table></td>
</tr>

<tr>
<td colspan=2 align=center><div class=smtext>
<p><input class=blackboldtext type=submit value="add marker">
</div></td>
</tr>

</table>
</td>

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

<tr>
<td align=center><div class=text>
<b>Move marker to address:</b>&nbsp;
<input class=blacktext type=text name=address size=30 maxlength=150>
&nbsp;<a href="javascript:void(0);" onclick="javascript:showAddress(document.markers.address.value); return false">GO</a>
</div></td>
</tr>

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

</table>
</form>

</body>
</html>
