// (c) EIKONA AG, it.x informationssysteme gmbh, Alle Rechte vorbehalten.

	var map = false;
	var routenplaner;
	var str_fehler_msg;
	var str_routenplaner_id;
	var int_zoom = 10;
	var marker = [];
  	
  	/**
    *	Google Maps karte anzeigen
    */
  	function anzeigen_karte(str_map_id, int_control, control_type, scale_control, zoom_control, overview_control)
  		{
  		if (GBrowserIsCompatible()) 
    		{
      		var str_map_container = $(str_map_id);
	   		if (str_map_container)
	   			{
	   			map = new GMap2(str_map_container);				
   					
				// Steuerelemente
	      		steuerelemente(int_control, control_type, scale_control, zoom_control, overview_control);
				}
    		}
  		} // anzeigen_karte
  		
    /**
    *	Icons Anzeigen
    */
	function anzeigen_icon(array_koordinaten, str_bild, int_breite, int_hoehe, ausrichtung_x, ausrichtung_y, system_id, titel)
		{
	  	if(str_bild == "portal/landkarte/pics/" || str_bild == "" )
	    	{
	    	str_bild = "portal/landkarte/pics/pin_red.png";
	    	ausrichtung_y = "unten";
	    	ausrichtung_x = "mitte";
	    	int_breite = 24;
	    	int_hoehe = 17;
	    	}
	    
	    // Icon einbindung
    	var icon = new GIcon();
		icon.image = str_bild;
		icon.iconSize = new GSize(int_breite, int_hoehe);
		
		if(ausrichtung_x == "links")
			position_x = 0;
		else if(ausrichtung_x == "mitte")
			position_x = int_breite / 2;
		else if(ausrichtung_x == "rechts")
			position_x = int_breite;
		
		if(ausrichtung_y == "oben")
			position_y = 0;
		else if(ausrichtung_y == "mitte")
			position_y = int_hoehe / 2;
		else if(ausrichtung_y == "unten")
			position_y = int_hoehe ;

		icon.iconAnchor = new GPoint(position_x, position_y);
		icon.infoWindowAnchor = new GPoint(position_x, position_y);
				
    	var marker = new GMarker(new GLatLng(array_koordinaten[0], array_koordinaten[1]), {title:titel, icon:icon});
    	marker.id = system_id;
    	
    	map.addOverlay(marker);
    	
    	GEvent.addListener
    	(marker, "click",
	    	function()
	    		{
	    		marker.openInfoWindowHtml(kopiere_inhalt_infowindow(system_id));
	    		}
	    );
	  	}//anzeigen_icon

	/**
    *	Zentrieren in Karte für Sidebar Navi
    */
  	function zentrieren(array_koordinaten, system_id, int_zoom_faktor)
	  	{
	  	map.setCenter(new GLatLng(array_koordinaten[0],array_koordinaten[1]), int_zoom_faktor)
	  	//map.openInfoWindowHtml(new GLatLng(array_koordinaten[0],array_koordinaten[1]), kopiere_inhalt_infowindow(system_id));
	  	}//zentrieren

    function uebernehme_in_routenplaner(str_feld, str_adresse)
        {
        if($(str_feld))
		  $(str_feld).value = str_adresse;
        }

	/**
    * Laden des Inhalts, der im Infowindow angezeigt werden soll aus
    * dem Element <div id="id_content_xxx"> das in der HTML Seite
    * vorhanden sein muss.
    */
	function kopiere_inhalt_infowindow(system_id)
		{
		if($('id_content_' + system_id))
			return $('id_content_' + system_id).innerHTML;
		else
			return '';
		} // kopiere_inhalt_infowindow()

  	/**
    *	Routenplanung durchführen
    */
  	function routenplaner_start(str_map_id, str_planer_id, str_sprache_kuerzel, str_fehler_message, start, ziel)
  		{
  		// Die Karte muss dafür schon vorhanden sein
  		if (map != false)
  			{
  			// Ergebnisfeld zurücksetzen
  			$(str_planer_id + '_ergebnis').innerHTML = "";
  			
  			// Routenplanerobjekt erstellen
	      	routenplaner = new GDirections(map, $(str_planer_id + '_ergebnis'));
	      	
	      	GEvent.addListener(routenplaner, "error", routenplaner_handleErrors);
	      	
	      	// Strecke ermitteln
	      	routenplaner.load("from: " + start + " to: " + ziel, { "locale": str_sprache_kuerzel} );
					
	      	// Fehlermeldung und ID speichern
	      	str_fehler_msg = start + ' ' + str_fehler_message;
	      	str_routenplaner_id = str_planer_id;
  			}
  		
  		} // routenplaner_start

  /**
    * Koordinaten des Startorts für den Umkreisfinder ermitteln
    * und Formular absenden.    
    */
  	function pruefe_koordinaten(start, funcCallback, funcCallbackFehler)
  		{
		var geocoder = new GClientGeocoder();
		
		geocoder.getLatLng(start,
			function(point)
				{
				if (!point)
					{
					funcCallbackFehler(start);
					}
				else
					{
					funcCallback(point.lat(), point.lng());
					}
				}
			);
  		} // pruefe_koordinaten()

 	/**
    *	Steuerelmente für Kartenanzeige
    */
	function steuerelemente(int_control, control_type, scale_control, zoom_control, overview_control)
		{
  		// Zoomleiste
  		if (int_control == 1)
  			{
  			map.addControl(new GSmallMapControl());
  			}
  		else if(int_control == 2)
  			{
  			map.addControl(new GLargeMapControl());
  			}
  		if(control_type)
  			{
  			map.addControl(new GMapTypeControl());
			}
  		// Kartenskalierung	
  		if(scale_control)
  			{
  			map.addControl(new GScaleControl());
  			}
		// kleine Karte
		if(overview_control)
  			{
			map.addControl(new GOverviewMapControl());
			}
		// Zoom kleine Karte
  		if(zoom_control)
  			{
			map.addControl(new GSmallZoomControl());
			}
		} // steuerelemente

	/**
    *	Google Maps karte mit Umkreisanzeige
    */
  	function anzeigen_karte_umkreisfinder(str_map_id, array_koordinaten, int_control, control_type, scale_control, zoom_control, overview_control, radiuskm)
  		{

    	if (GBrowserIsCompatible()) 
    		{
	      	var str_map_container = $(str_map_id);
		   	if (str_map_container)
		   		{
		   		map = new GMap2(str_map_container);

				steuerelemente(int_control, control_type, scale_control, zoom_control, overview_control);

  				var icon = new GIcon();
				icon.image = 'admin/pics/16x16/house.png';
				icon.iconSize = new GSize(16, 16);
	
				icon.iconAnchor = new GPoint(6, 12);
	  		
	  			map.setCenter(new GLatLng(array_koordinaten[0],array_koordinaten[1]));
	  			var marker2 = new GMarker(new GLatLng(array_koordinaten[0],array_koordinaten[1]), {icon:icon});
	  			map.addOverlay(marker2);
	  			var bounds = new GLatLngBounds();

				///////////////	
			    var cColor = 'red';
                var cWidth = 3;
                var Cradius = radiuskm;   
                var d2r = Math.PI/180; 
                var r2d = 180/Math.PI; 
                var Clat = (Cradius/6377.83027)*r2d; 
                var Clng = Clat/Math.cos(array_koordinaten[0]*d2r); 
                var Cpoints = []; 
                for (var i=0; i < 33; i++)
                	{ 
                    var theta = Math.PI * (i/16); 
                    var CPlng = array_koordinaten[1] + (Clng * Math.cos(theta)); 
                    var CPlat = array_koordinaten[0] + (Clat * Math.sin(theta)); 
                    var P = new GLatLng(CPlat,CPlng);
                    
					bounds.extend(P);
					Cpoints.push(P); 
                	}
                
                if(radiuskm == 0)
                    map.setZoom(5); // Hier könnte man noch den Standardwert aus dem Seitenelement verfügbar machen.
                else
                    map.setZoom(map.getBoundsZoomLevel(bounds));
                 
                 map.setCenter(new GLatLng(array_koordinaten[0],array_koordinaten[1]));
                
                 map.addOverlay(new GPolyline(Cpoints,cColor,cWidth)); 
                 }
			}
  		} // anzeigen_karte_umkreisfinder

	/**
    *	Google Maps karte mit Routenplanung anzeigen
    */
  	function anzeigen_karte_routenplaner(str_map_id, int_control, control_type, scale_control, zoom_control, overview_control)
  		{
    	if (GBrowserIsCompatible()) 
    		{
	      	var str_map_container = $(str_map_id);
		   	if (str_map_container)
		   		{
		   		map = new GMap2(str_map_container);
				// Steuerelemente
	      		steuerelemente(int_control, control_type, scale_control, zoom_control, overview_control);
				}
    		}
  		} // anzeigen_karte_routenplaner

  	/**
    *	Fehler bei der Routenplanung handeln
    */
  	function routenplaner_handleErrors()
  		{
  		$(str_routenplaner_id + '_ergebnis').style.display = "none";
		alert (str_fehler_msg);
  		} // routenplaner_handleErrors
