<?php

include_once("../include/global.inc.php");
include_once ("../include/auth.inc.php");

// $hq_id = $HTTP_SESSION_VARS['hq_id'];
$hq_id = $_SESSION['hq_id'];
if ($hq_id == "") : $hq_id = $HTTP_SESSION_VARS['hq_id']; endif;

$hq_marker = "../images/marker_sb.png";
if ($hq_id >= 100)
  $hq_marker = "../images/marker_ht.png";

$today = getdate();
//$start_time = date("YmdHis", mktime($today['hours'] - 24, $today['minutes'],  $today['seconds'], $today['mon'], $today['mday'], $today['year']));
$start_time = date("Y-m-d H:i:s", mktime(0, 0, 0, $today['mon'], $today['mday'], $today['year']));
$lat = 0;
$long = 0;

list ($cr_sid) =
  getHttpVars(array("cr_sid"));

//$markers_text = "point	title	description	icon	iconSize	iconOffset\x0A";
$sqlquery = "SELECT hq_gps_long, hq_gps_lat FROM headquarters WHERE hq_id = '$hq_id'";
$markers_javascript = "";
$res = $db->query($sqlquery);
if (DB::isError($res))
  die ("$PHP_SELF: '$sqlquery' : " . $res->getMessage());
if ($row = $res->fetch_assoc()):
  $markers_javascript .= "var hq_icon = new OpenLayers.Icon('$hq_marker', size, offset);\n";
  $markers_javascript .= "marker = new OpenLayers.Marker(new OpenLayers.LonLat(". $row["hq_gps_long"] . ", " . $row["hq_gps_lat"] . ").transform(new OpenLayers.Projection(\"EPSG:4326\"), map.getProjectionObject()), hq_icon);\n"; 
  $markers_javascript .= "marker.events.register('mousedown', marker, function(evt) { alert('Zentrale'); OpenLayers.Event.stop(evt); });";
  $markers_javascript .= "markers.addMarker(marker);\n";
//  $markers_text .= $row["hq_gps_lat"] . "," . $row["hq_gps_long"] . "	Zentrale	honk	$hq_marker	28,28	-10,-28\x0A";
  $lat = $row["hq_gps_lat"];
  $long = $row["hq_gps_long"];
endif;
$res->free();

$sqlquery = "SELECT cr_sid, cr_locationzipcode, cr_available, cr_availabletime, cr_gps_long, cr_gps_lat, cr_gps_time FROM courier WHERE hq_id = '$hq_id' AND cr_gps_time > '" . $start_time . "'"; //AND cr_available = 1 ORDER BY cr_gps_time, cr_sid";
if (trim($cr_sid) != "")
  $sqlquery = "SELECT cr_sid, cr_locationzipcode, cr_availabletime, cr_gps_long, cr_gps_lat, cr_gps_time FROM courier WHERE cr_sid = '$cr_sid'";
$res = $db->query($sqlquery);
if (DB::isError($res))
  die ("$PHP_SELF: '$sqlquery' : " . $res->getMessage());
$i = 1;
while ($row = $res->fetch_assoc()):
//  $markers_text .= $row["cr_gps_lat"] . "," . $row["cr_gps_long"] . "	" . $row["cr_sid"] . "honk	" . $row["cr_gps_time"] . "	../images/marker_white.png	28,28	-10,-28\x0A";
  $markers_javascript .= "var icon$i = new OpenLayers.Icon('../images/marker_white.png', size.clone(), offset.clone());\n";
  $markers_javascript .= "marker = new OpenLayers.Marker(new OpenLayers.LonLat(". $row["cr_gps_long"] . ", " . $row["cr_gps_lat"] . ").transform(new OpenLayers.Projection(\"EPSG:4326\"), map.getProjectionObject()), icon" . $i++ . ");\n"; 
  $markers_javascript .= "marker.events.register('mousedown', marker, function(evt) { alert('" . $row["cr_sid"] . "\\nFreimeldung: " . $row["cr_locationzipcode"] . " (" . $row["cr_availabletime"] . ")\\n" . "Ortungszeit: " .  $row["cr_gps_time"] . "'); OpenLayers.Event.stop(evt); });";
  $markers_javascript .= "markers.addMarker(marker);\n";
  if (trim($cr_sid) != ""):
    $lat = $row["cr_gps_lat"];
    $long = $row["cr_gps_long"];
  endif;
endwhile;
$res->free();

//$fname = "../temp/osm_text/" . $usr_id . $start_time . ".txt";
//if (!$handle = fopen($fname, "w"))
//  die("Fehler beim schreibenden Zugriff auf '$fname'");
//if (!fwrite($handle, $markers_text))
//  die ("Fehler beim Schreiben in '$fname'");
//fclose($handle);

?>

<html>
<head>
<title>KARTENDARSTELLUNG</title>
<link rel="stylesheet" type="text/css" href="../css/phoenix.css">
<!--
<script src="http://www.openlayers.org/api/OpenLayers.js"></script>
-->
<script src="../include/openlayers/OpenLayers.js"></script>
<script src="http://www.openstreetmap.org/openlayers/OpenStreetMap.js"></script>
<!--
<script src='http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAA3HdfrnxFAPWyY-aiJUxmqRTJQa0g3IQ9GZqIMmInSLzwtGDKaBQ0KYLwBEKSM7F9gCevcsIf6WPuIQ'></script>
<script src="http://api.maps.yahoo.com/ajaxymap?v=3.0&appid=euzuro-openlayers"></script>
-->
<noscript>
  <center>
  <b><br>JavaScript ist nicht verf&uuml;gbar. Bitte aktivieren Sie JavaScript<br><br>
        in Ihrem Browser, damit diese Seite ordnungsgem&auml;&szlig; funktioniert!</b><br><br>
  </center>
</noscript>
</head>
<body>

<div style="display: table; height: 100%; #position: relative; overflow: hidden; margin:auto;">
<div style=" #position: absolute; #top: 50%;display: table-cell; vertical-align: middle;">
<div style=" #position: relative; #top: -50%; border: 2px solid black;">
<div id="map" style="width:1007px; height:675px; vertical-align:middle;">

<script type="text/javascript">

var lon = <?php echo $long; ?>;
var lat = <?php echo $lat; ?>;
var zoom = 12;
var map = new OpenLayers.Map('map',
    { controls:
      [ new OpenLayers.Control.KeyboardDefaults(),
        new OpenLayers.Control.MouseDefaults(),
        new OpenLayers.Control.LayerSwitcher(),
        new OpenLayers.Control.PanZoomBar(),
        new OpenLayers.Control.ScaleLine(),
        new OpenLayers.Control.MousePosition(),
        new OpenLayers.Control.Attribution()
      ],
      maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
      restrictedExtent: new OpenLayers.Bounds(-1337508.34, 4037508.34, 4837508.34, 9037508.34),
      maxResolution: 156543.0399,
//      minScale: 100000,
//      maxScale: 10000000,
//      numZoomLevels: 13,
      units: 'm',
      projection: new OpenLayers.Projection("EPSG:900913"),
      displayProjection: new OpenLayers.Projection("EPSG:4326")
    });
var mapnik_layer = new OpenLayers.Layer.OSM.Mapnik("OSM Mapnik");
//var tah_layer = new OpenLayers.Layer.OSM.Osmarender("OSM Tiles@Home");
//var google_layer = new OpenLayers.Layer.Google("Google Streets", {'sphericalMercator': true});
//var yahoo_layer = new OpenLayers.Layer.Yahoo("Yahoo Street", {'sphericalMercator': true});
//map.addLayers([yahoo_layer, mapnik_layer, tah_layer]);
//map.addLayers([mapnik_layer, yahoo_layer]);
map.addLayers([mapnik_layer]);

map.setCenter (new OpenLayers.LonLat(lon, lat).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject()), zoom);

//var newl = new OpenLayers.Layer.Text( "Fahrzeuge", { location:"<?php echo "$fname"; ?>", projection: new OpenLayers.Projection("EPSG:4326") } );
//map.addLayer(newl);

//var newll = new OpenLayers.Layer.Text( "PLZs", { location:"zipcode.txt", projection: new OpenLayers.Projection("EPSG:4326") } );
//map.addLayer(newll);

//OpenLayers.Lang.setCode("de");
//alert (OpenLayers.Lang.getCode());

var markers = new OpenLayers.Layer.Markers("Marker");
map.addLayer(markers);

//var size = new OpenLayers.Size(10,17);
//var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
//var icon = new OpenLayers.Icon('http://boston.openguides.org/markers/AQUA.png', size, offset);
var size = new OpenLayers.Size(28,28);
var offset = new OpenLayers.Pixel(-10, -28);

<?php echo $markers_javascript; ?>

//10,20	my orange title	my orange description	
//2,4	my aqua title	my aqua description	
//42,-71	my purple title	my purple description<br/>is great.	http://www.openlayers.org/api/img/zoom-world-mini.png

</script>

</div>
</div>
</div>
</div>

</body>
</html>



