<?php
include 'header.php';


// ############ START QUERY STATIONS #####################################
$query_stations = "
SELECT stations.latitude, stations.longitude, stations.name,
GROUP_CONCAT(DISTINCT routes.name ORDER BY routes.name SEPARATOR ', ') AS 'Linien'  
FROM stations, routes, routes_stations
WHERE routes.id = routes_stations.r_id
AND stations.id = routes_stations.s_id
GROUP BY stations.id
";

$result_stations = mysqli_query($db, $query_stations);
if ($result_stations) {
   $rows_stations = mysqli_num_rows($result_stations);
   $cols_stations = mysqli_num_fields($result_stations);
   for($r=0; $r< $rows_stations; $r++) {
		list($x_station[$r], $y_station[$r], $n_station[$r], $inline_station[$r]) = mysqli_fetch_row($result_stations);
   }
} 
else {
   echo "Fehler";
}

mysqli_free_result($result_stations);


// ############ START QUERY SIGHTS ########################################
$query_sights = "
SELECT latitude, longitude, name, cost, opens, closes
FROM sights
";

$result_sights = mysqli_query($db, $query_sights);
if ($result_sights) {
   $rows_sights = mysqli_num_rows($result_sights);
   $cols_sights = mysqli_num_fields($result_sights);
   for($s=0; $s< $rows_sights; $s++) {
		list($x_sight[$s], $y_sight[$s], $n_sight[$s], $cost_sight[$s], $opens_sight[$s], $closes_sight[$s]) = mysqli_fetch_row($result_sights);
   }
} 
else {
   echo "Fehler";
}

mysqli_free_result($result_sights);


// ############ START QUERY FREETIME ######################################
$query_freetime = "
SELECT latitude, longitude, name
FROM freetime
";

$result_freetime = mysqli_query($db, $query_freetime);
if ($result_freetime) {
   $rows_freetime = mysqli_num_rows($result_freetime);
   $cols_freetime = mysqli_num_fields($result_freetime);
   for($s=0; $s< $rows_freetime; $s++) {
		list($x_freetime[$s], $y_freetime[$s], $n_freetime[$s]) = mysqli_fetch_row($result_freetime);
   }
} 
else {
   echo "Fehler";
}

mysqli_free_result($result_freetime);



// ############ START QUERY LINES #########################################
$query_lines = "
SELECT COUNT(id) AS 'anzahl' FROM routes
";
$result_lines = mysqli_query($db, $query_lines);
while ($line = mysqli_fetch_array($result_lines, MYSQLI_ASSOC)) {
   foreach ($line as $col_value) {
       $linien = $col_value;
   }
}

for($i=1; $i < $linien+1; $i++) {

   $query_line[$i] = "
SELECT stations.latitude, stations.longitude, routes.name, routes.color
FROM stations, routes_stations JOIN routes ON routes_stations.r_id = routes.id
WHERE stations.id = routes_stations.s_id
AND routes_stations.r_id = $i 
";

   $result_line[$i] = mysqli_query($db, $query_line[$i]);
   if ($result_line[$i]) {
      $rows_line[$i] = mysqli_num_rows($result_line[$i]);
      $cols_line[$i] = mysqli_num_fields($result_line[$i]);
      for($s=0; $s< $rows_line[$i]; $s++) {
			list($x_line[$i][$s], $y_line[$i][$s], $n_line[$i][$s], $c_line[$i][$s]) = mysqli_fetch_row($result_line[$i]);
      }
   } 
   else {
      echo "Fehler";
   }
   mysqli_free_result($result_line[$i]);
}



echo '</head>';
// ############ START BODY################################################
echo '<body onUnload="GUnload()">';

include 'menu.php';


// ############ START CONTENT #############################################
echo '
    <div id="content">

        <form name="layer" action=""><b>Zeige:</b>&nbsp;&nbsp;
           <input type="checkbox" id="lines" name="lines" onClick="boxclick()" checked>Linien &nbsp;&nbsp;
           <input type="checkbox" id="stations" name="stations" onClick="boxclick()" checked>Stationen &nbsp;&nbsp;
           <input type="checkbox" id="sights" name="sights" onClick="boxclick()">Sehensw&uuml;rdigkeiten &nbsp;&nbsp;          
           <input type="checkbox" id="freetime" name="freetime" onClick="boxclick()">Freizeit &nbsp;&nbsp;
        </form>	
        <div id="map" align="center" style="width: 800px; height: 600px"></div>
';

$lat = "31.250613";
$long = "121.481323";
$zoom = "11";
// ### START JAVASCRIPT ###################################################
echo '<script type="text/javascript">';

// ### Funktion boxclick() Show/Hide Overlays #############################
echo '

       function boxclick() {
        if (document.layer.stations.checked == true) {
           for (i=0; i< point_station.length; i++) {
              marker_station[i].show();
           }
        } else {
           for (i=0; i< point_station.length; i++) {
              marker_station[i].hide();
           }
        }
        if (document.layer.sights.checked == true) {
           for (i=0; i< point_sight.length; i++) {
              marker_sight[i].show();
           }
        } else {
           for (i=0; i< point_sight.length; i++) {
              marker_sight[i].hide();
           }
        }
        if (document.layer.freetime.checked == true) {
           for (i=0; i< point_freetime.length; i++) {
              marker_freetime[i].show();
           }
        } else {
           for (i=0; i< point_freetime.length; i++) {
              marker_freetime[i].hide();
           }
        }
        if (document.layer.lines.checked == true) {
           legend.show();
           for (i=1; i< polyline.length; i++) {
              polyline[i].show();
           }
        } else {
           legend.hide();
           for (i=1; i< polyline.length; i++) {
              polyline[i].hide();
           }
        }
      }
';


// ### Funktion createListener() Listener for stations/sights ###########
echo '   function createListener (marker, text) {
         GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(text);
         }
         );
      }
';

// ### Karteneinstellungen - Icons #######################################
echo '   if (GBrowserIsCompatible()) {
         var map = new GMap2(document.getElementById("map"));
         map.setMapType(G_SATELLITE_MAP);
         map.addControl(new GLargeMapControl());
         map.addControl(new GMapTypeControl());
         map.addControl(new GScaleControl());
         map.addControl(new GOverviewMapControl());
         var icon_station = new GIcon();
         icon_station.image = "images/markerstation.png";
         icon_station.shadow = "images/markerstation_schatten.png";
         icon_station.iconSize = new GSize(10, 10);
         icon_station.shadowSize = new GSize(10, 10);
         icon_station.iconAnchor = new GPoint(5, 5);
         icon_station.infoWindowAnchor = new GPoint(5, 5);
         var icon_sight = new GIcon();
         icon_sight.image = "images/markersight.png";
         icon_sight.shadow = "images/markerstation_schatten.png";
         icon_sight.iconSize = new GSize(10, 10);
         icon_sight.shadowSize = new GSize(10, 10);
         icon_sight.iconAnchor = new GPoint(5, 5);
         icon_sight.infoWindowAnchor = new GPoint(5, 5);

';
echo '
         map.setCenter(new GLatLng('.$lat.', '.$long.'), '.$zoom.');
         var point_station = new Array();
         var text_station = new Array();
         var point_sight = new Array();
         var text_sight = new Array();
         var point_freetime = new Array();
         var text_freetime = new Array();
         var polyline = new Array();
         var legend = new GScreenOverlay(\'images/legendKopie.png\', new GScreenPoint(0, 35, \'pixels\',\'pixels\'), new GScreenPoint(70, 0), new GScreenSize(140, 250, \'pixels\', \'pixels\')); 
         map.addOverlay(legend);
';


// ### Erstellen und Anzeige der Linien ##################################
for($i=1; $i < $linien+1; $i++) { 
   echo	 'polyline['.$i.'] = new GPolyline([
   ';

   for($s=0; $s< $rows_line[$i]; $s++) {
      echo '
         new GLatLng('.$x_line[$i][$s].','.$y_line[$i][$s].'),
      ';
   }
   // Farbe der Linien? manche schlecht erkennbar auf Karte
   echo '
         ], "'.$c_line[$i][1].'", 4, 1);
         map.addOverlay(polyline['.$i.']);
   ';
}

// ### Erstellen und Anzeige der Haltestellen und Sehenswürdigkeiten und Freizeit ###
for ($r=0; $r<$rows_stations; $r++) {
   echo '
         point_station['.$r.'] = new GLatLng('.$x_station[$r].','.$y_station[$r].');
         text_station['.$r.'] = "<table align=\"left\"><tr><td><img src=\"images/markerstation.png\" width=\"20\" height=\"20\">&nbsp <b><font color=\"#FF0000\">'.$n_station[$r].'</font></b></td></tr><tr><td>Einstieg m&ouml;glich in:  <b>'.$inline_station[$r].'</b></td></tr></table>";
   ';
}
for ($s=0; $s<$rows_sights; $s++) {
   echo '
         point_sight['.$s.'] = new GLatLng('.$x_sight[$s].','.$y_sight[$s].');
         text_sight['.$s.'] = "<table align=\"left\"><tr><td><img src=\"images/markersight.png\" width=\"20\" height=\"20\">&nbsp <b><font color=\"#FF0000\">'.$n_sight[$s].'</font></b></td></tr><tr><td><b>Eintritt:</b>&nbsp&nbsp&nbsp&nbsp&nbsp '.$cost_sight[$s].' Yuan</td></tr><tr><td><b>&Ouml;ffnet:</b>&nbsp&nbsp&nbsp&nbsp&nbsp '.$opens_sight[$s].'</td></tr><tr><td><b>Schlie&szlig;t:</b>&nbsp '.$closes_sight[$s].'</td></tr></table>";
   ';
}
for ($s=0; $s<$rows_freetime; $s++) {
   echo '
         point_freetime['.$s.'] = new GLatLng('.$x_freetime[$s].','.$y_freetime[$s].');
         text_freetime['.$s.'] = "<img src=\"images/markersight.png\" width=\"20\" height=\"20\">&nbsp <b><font color=\"#FF0000\">'.$n_freetime[$s].'</font></b>";
   ';
}
echo '
         var marker_station = new Array();
         var marker_sight = new Array();
         var marker_freetime = new Array();
  
         for (i=0; i< point_station.length; i++) {
            marker_station[i] = new GMarker(point_station[i], icon_station);
            map.addOverlay(marker_station[i]);
            createListener (marker_station[i], text_station[i]);
         }
         for (i=0; i< point_sight.length; i++) {
            marker_sight[i] = new GMarker(point_sight[i], icon_sight);
            map.addOverlay(marker_sight[i]);
            createListener (marker_sight[i], text_sight[i]);
            marker_sight[i].hide();
         }
         for (i=0; i< point_freetime.length; i++) {
            marker_freetime[i] = new GMarker(point_freetime[i], icon_sight);
            map.addOverlay(marker_freetime[i]);
            createListener (marker_freetime[i], text_freetime[i]);
            marker_freetime[i].hide();
         }

';

echo '
      }
   
   </script>

';

include 'footer.php';
?>