/*
	This little app has been written by Juri Strumpflohner (http://mitglied.lycos.de/kito85)
	Sources: - http://www.google.com/apis/maps/
		 - http://www.econym.demon.co.uk/googlemaps/
	This app is provided "as-is". You are free to modify, but for fairness leave this note here
	in the header of the script!

	Date: 11/09/2006

        Angepasst von Viktor Gertzen
        Neue Funktionen hinzugefügt

        Date: 25.04.2008 VGGoogle-Map V1.3
*/
    //<![CDATA[
    	var map = null;
    	var geocoder = null;

	var sideHtml= "";
	var gmarkers = [];
	var markerPos = [];
        var to_htmls = [];
        var from_htmls = [];
        var counter = 0;
	var info = [];
	var j = 0;
	var counter = 0;

	function load(show) {

		if (GBrowserIsCompatible()) {

                     // Text für Karte wird generiert ausgeben
                     var om = new OverlayMessage(document.getElementById('map'));
                     om.Set('<b>Karte wird geladen...</b>');

                     function createMarker(point, name, addDate, addPu, comment, address, woher) {
                       var marker = new GMarker(point,Icon);
                       if(woher==''){
                         var displayHtml = "<div style=\"width:250px;\"><b>" + name + "</b><br>" + comment + "<br><br><div style=\"font-size:8pt; color:#008000;\"><i>Profil: <a href=profile.php?userid="+ addPu +" target=\"_blank\">Zum Profil</a></i></div><div><font style=\"font-size:8pt; color:#008000;\"><i>Entfernung: </font><font size=\"2\">"+ addDate +"</i></font></div> <div style=\"font-size:8pt; color:#008000;\"><br>Koordinaten:" + point + "</div>";
                       }else{
                         if(address==''){
                           var displayHtml = "<div style=\"width:250px;\"><b>" + name + "</b><br><br><div style=\"font-size:8pt; color:#008000;\"><i>URL: Keine angegeben</i></div><div><font style=\"font-size:8pt; color:#008000;\"><i>Entfernung: </font><font size=\"2\">"+ addDate +"</i></font></div> <div style=\"font-size:8pt; color:#008000;\"><br>Koordinaten:" + point + "</div>";
                         }else{
                           var displayHtml = "<div style=\"width:250px;\"><b>" + name + "</b><br><br><div style=\"font-size:8pt; color:#008000;\"><i>URL: <a href="+ address +" target=\"_blank\">Zum Ort</a></i></div><div><font style=\"font-size:8pt; color:#008000;\"><i>Entfernung: </font><font size=\"2\">"+ addDate +"</i></font></div> <div style=\"font-size:8pt; color:#008000;\"><br>Koordinaten:" + point + "</div>";
                         }
                       }

                       to_htmls[counter] = displayHtml;
                       to_htmls[counter] += '<form class="gmapDir" id="gmapDirTo" style="font-size:8pt; white-space: nowrap;" action="http://maps.google.com/maps" method="get" target="_blank">';
                       to_htmls[counter] += '<p class="gmapDirHead" id="gmapDirHeadTo">Wegbeschreibung: <strong>Hierher</strong> - <a href="javascript:fromhere(' + counter + ')">Von hier aus</a></p>';
                       to_htmls[counter] += '<p class="gmapDirItem" id="gmapDirItemTo"><label for="gmapDirSaddr" class="gmapDirLabel" id="gmapDirLabelTo">Start-Adresse:<br /></label><input type="text" size="30" maxlength="40" name="saddr" class="gmapTextBox" id="gmapDirSaddr" value="" onfocus="this.style.backgroundColor = \'#e0e0e0\';" onblur="this.style.backgroundColor = \'#ffffff\';" /></p>';
                       to_htmls[counter] += '<p class="gmapDirBtns" id="gmapDirBtnsTo"><input value="Wegbeschreibung anzeigen" type="submit" class="gmapDirButton" id="gmapDirButtonTo" /></p>';
                       to_htmls[counter] += '<input type="hidden" name="daddr" value="' + point.y + ', ' + point.x + '(' + name + ')' + '" /></form>';

                       from_htmls[counter] = displayHtml;
                       from_htmls[counter] += '<form class="gmapDir" id="gmapDirFrom" style="font-size:8pt; white-space: nowrap;" action="http://maps.google.com/maps" method="get" target="_blank">';
                       from_htmls[counter] += '<p class="gmapDirHead" id="gmapDirHeadFrom">Wegbeschreibung: <a href="javascript:tohere(' + counter + ')">Hierher</a> - <strong>Von hier aus</strong></p>';
                       from_htmls[counter] += '<p class="gmapDirItem" id="gmapDirItemFrom"><label for="gmapDirSaddr" class="gmapDirLabel" id="gmapDirLabelFrom">Ziel-Adresse:<br /></label><input type="text" size="30" maxlength="40" name="saddr" class="gmapTextBox" id="gmapDirSaddr" value="" onfocus="this.style.backgroundColor = \'#e0e0e0\';" onblur="this.style.backgroundColor = \'#ffffff\';" /></p>';
                       from_htmls[counter] += '<p class="gmapDirBtns" id="gmapDirBtnsFrom"><input value="Wegbeschreibung anzeigen" type="submit" class="gmapDirButton" id="gmapDirButtonFrom" /></p>';
                       from_htmls[counter] += '<input type="hidden" name="daddr" value="' + point.y + ',' + point.x + '(' + name + ')' + '" /></form>';
                       displayHtml = displayHtml + '<br /><div id="gmapDirHead" class="gmapDir" style="font-size:8pt; white-space: nowrap;">Wegbeschreibung: <a href="javascript:tohere(' + counter + ')">Hierher</a> - <a href="javascript:fromhere(' + counter + ')">Von hier aus</a></div>';

                       GEvent.addListener(marker, "click", function() {
                         marker.openInfoWindowHtml(displayHtml);
                       });
                       //alert("Debug:\npoint:" + point + "\nname:" + name);
                       //storing information about the marker
                       markerPos[j] = point;
                       gmarkers[j] = marker;
                       info[j]= displayHtml;

                       var zoomout="1";
                       sideHtml += '<a href="javascript:myclick(' + j + ')">' + name + '</a> - <font size="2"><a href="javascript:openMarker(' + j + ',15)"><img src="images/google/zoomin.gif" border="0" alt="Zeige auf Karte" title="Zeige auf Karte"></a></font>&nbsp;<font size="2"><a href="javascript:openMarker(' + j + ',5)"><img src="images/google/zoomout.gif" border="0" alt="Zurück" title="Zurück"></a></font><br>';
                       j++;
                       counter++;
                       return marker;
                     }

                     // Karte definieren
                     map = new GMap2(document.getElementById("map"),{mapTypes:[G_HYBRID_MAP,G_SATELLITE_MAP,G_NORMAL_MAP]});

                     // Koordinaten aunter der Karte ausgeben
                     GEvent.addListener(map, "moveend", function() {
                       var center = map.getCenter();
                       var latLngStr = '(Breitengrad: ' + center.y + ', Längengrad: ' + center.x + ')';
                       document.getElementById("message").innerHTML = latLngStr;
                       document.coordinates.lat.value = center.y;
                       document.coordinates.lng.value = center.x;
                     });

                     geocoder = new GClientGeocoder();
                     map.addControl(new GLargeMapControl());
                     map.addControl(new GMapTypeControl());
                     map.addControl(new GScaleControl());
                     map.addControl(new GOverviewMapControl());
                     map.setCenter(new GLatLng(50.9781, 11.0045), 6);

                     // Icon für einen einzelnen User
                     var Icon = new GIcon();
                     Icon.image = "images/map/homie/home3.gif";

                     Icon.iconSize = new GSize(16, 16);
                     Icon.shadowSize = new GSize(37, 34);
                     Icon.iconAnchor = new GPoint(0, 2);
                     Icon.infoWindowAnchor = new GPoint(9, 2);
                     Icon.infoShadowAnchor = new GPoint(18, 25);

                     // Icon für eine Usergruppe
                     var clusterIcon = new GIcon();
                     clusterIcon.image = 'images/map/homie/home2.gif';

                     clusterIcon.iconSize = new GSize( 16, 16 );
                     clusterIcon.shadowSize = new GSize( 56, 51 );
                     clusterIcon.iconAnchor = new GPoint( 0, 2 );
                     clusterIcon.infoWindowAnchor = new GPoint( 13, 3 );
                     clusterIcon.infoShadowAnchor = new GPoint( 27, 37 );

                     // define clusterer
                     var clusterer = new Clusterer(map);

                     // set the clusterer parameters if you dont like the defaults
                     clusterer.icon = clusterIcon;
                     clusterer.maxVisibleMarkers = 80;
                     clusterer.gridSize = 50;
                     clusterer.minMarkersPerClusterer = 100;
                     clusterer.maxLinesPerInfoBox = 20;

                     // User aus Tabelle einlesen und aufgereiten
                     GDownloadUrl("mapg_eint.php", function(data, responseCode) {
                       var xml = GXml.parse(data);
                       var markers = xml.documentElement.getElementsByTagName("marker");
                       for (var i = 0; i < markers.length; i++) {
                         var lat = null;
                         var lng = null;
                         lat = markers[i].getAttribute("lat");
                         lng = markers[i].getAttribute("lng");
                         var name = markers[i].getAttribute("name");
                         var address = markers[i].getAttribute("address");
                         var addDate = markers[i].getAttribute("added");
                         var addPu = markers[i].getAttribute("pu");
                         var comment = markers[i].getAttribute("comment");
                         var woher   = markers[i].getAttribute("woher");
                         if(lat!=null && lng!=null && lat!="" && lng!="") {
                           var point = new GLatLng(parseFloat(lat),parseFloat(lng));
                           var marker = createMarker(point,name,addDate,addPu,comment,address,woher);

                           // create clusterer object
                           clusterer.AddMarker(marker, name);
                         }
                       }

                       // User rechtes in der SideBar anzeigen
                       document.getElementById("vgulist").innerHTML = sideHtml;

                       // Test "Karte wird generiert--" löschen
                       om.Clear(); // Clear the loading message

                       if(show){
				myclick(show-1);
                       }

                     });
		}
                else {
                  alert("Sorry, the Google Maps API is not compatible with this browser");
                }
                //end of function load
	}

       //this function is called when the user clicks on a link. It opens the InfoWindow of the attached
       //marker and depending on the link it zooms-in or out
       function openMarker(index, zoom){
         if(zoom!=-1){
           setPosAndZoom(markerPos[index],zoom);
           gmarkers[index].openInfoWindowHtml(info[index]);
         }else{
           //leave level of zoom as it is
           gmarkers[index].openInfoWindowHtml(info[index]);
         }
       }

       // This function picks up the click and opens the corresponding info window
       function myclick(j) {
          setPosAndZoom(markerPos[j],15);
          gmarkers[j].openInfoWindowHtml(info[j]);
       }

	//this function sets the map-center to the given "point" and sets the zoom to "zoom"
	function setPosAndZoom(point, zoom){
		map.setCenter(point,zoom);
	}

        function tohere(index) {
          gmarkers[index].openInfoWindowHtml(to_htmls[index]);
        }

        function fromhere(index) {
          gmarkers[index].openInfoWindowHtml(from_htmls[index]);
        }

    //]]>

