/*
	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: 07.08.2007
*/
    //<![CDATA[
    	var map = null;
    	var geocoder = null;

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

	function load() {

		//var path = "";
		//path = Request.QueryString("txtInput");
		//if (path==""){
		//	path="mapg_eint.php";
		//}			

		if (GBrowserIsCompatible()) {

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

                     function createMarker(point, name, addDate, addPu, comment) {
                       var marker = new GMarker(point,Icon);
                       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>";
                       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++;
                       return marker;
                     }

                     //this function generates the html for the sidebar	
                     function addSidebarHtml(sideHtml){
                       document.getElementById("vgulist").innerHTML = sideHtml;
                     }

                     // Karte definieren
                     map = new GMap2(document.getElementById("map"));

                     // Koordinaten aunter der Karte ausgeben
                     GEvent.addListener(map, "moveend", function() {
                       var center = map.getCenter();
                       document.getElementById("message").innerHTML = center.toString();
                     });

                     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), 5);

                     // Icon für einen einzelnen User
                     var Icon = new GIcon();
                     Icon.image = "images/google/marker.png";
                     Icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
                     Icon.iconSize = new GSize(20, 34);
                     Icon.shadowSize = new GSize(37, 34);
                     Icon.iconAnchor = new GPoint(9, 34);
                     Icon.infoWindowAnchor = new GPoint(9, 2);
                     Icon.infoShadowAnchor = new GPoint(18, 25);
      
                     // Icon für eine Usergruppe
                     var clusterIcon = new GIcon();
                     clusterIcon.image = 'images/google/blue_large.png';
                     clusterIcon.shadow = 'images/google/shadow_large.png';
                     clusterIcon.iconSize = new GSize( 30, 51 );
                     clusterIcon.shadowSize = new GSize( 56, 51 );
                     clusterIcon.iconAnchor = new GPoint( 13, 34 );
                     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 = 100;
                     clusterer.gridSize = 10;
                     clusterer.minMarkersPerClusterer = 5;
                     clusterer.maxLinesPerInfoBox = 6;

                     // 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");					
                         if(lat!=null && lng!=null && lat!="" && lng!="") {
                           var point = new GLatLng(parseFloat(lat),parseFloat(lng));
                           var marker = createMarker(point,name,addDate,addPu,comment);

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

                       // User rechtes in der SideBar anzeigen
                       addSidebarHtml(sideHtml);

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

                     });
		}
                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) {
          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);
	}	

    //]]>
