var $MapContainer = null;
var $MapTitle = null;
var $MapCloser = null;
var $MapsDiv = null;
var $Map	 = null;
var $Coder	 = null;

if (window.attachEvent) {window.attachEvent("onload", InitMaps); } else { window.addEventListener('load', InitMaps, false); }
if (window.attachEvent) {window.attachEvent("onunload", GUnload); } else { window.addEventListener('unload', GUnload, false); }


function InitMaps()
{
$MapContainer 	= document.createElement("div");
$MapTitle 		= document.createElement("div");
$MapCloser 		= document.createElement("div");
$MapsDiv 		= document.createElement("div");

$MapContainer.className = "MapContainer";
$MapTitle.className 	= "MapTitle";
$MapCloser.className 	= "MapCloser";
$MapsDiv.className		= "MapDiv";
$MapCloser.innerHTML	= "Karte schließen";

if ($MapCloser.attachEvent) {$MapCloser.attachEvent("onclick", HideMap); } else { $MapCloser.addEventListener('click', HideMap, false); }

$MapContainer.appendChild($MapTitle);
$MapContainer.appendChild($MapCloser);
$MapContainer.appendChild($MapsDiv);
document.body.appendChild($MapContainer);

MapsCenterWindow($MapContainer);

if (GBrowserIsCompatible()) { 	$Map = new GMap2($MapsDiv); 
								$Map.setCenter(new GLatLng(37.4419, -122.1419), 13);
								$Map.enableContinuousZoom();
								$Map.addControl(new GMapTypeControl());		
								$Map.addControl(new GSmallMapControl());	
								$Coder = new GClientGeocoder(null);
								$Coder.getLatLng("Lillers-Straße 8, 34431 Marsberg", function(point) {$Map.setCenter(point, 16);} );
							}
}

function Encode($Adresse, $Name)
{	$Coder.getLocations($Adresse, CheckMap); 
	$MapTitle.innerHTML = $Name;
}

function CheckMap($Status)
{
if (($Status.Status.code == 200) && ($Status.Placemark[0].AddressDetails.Accuracy >= 7))	{ GotoTo(new GLatLng($Status.Placemark[0].Point.coordinates[1], $Status.Placemark[0].Point.coordinates[0])); }
	else { window.alert("Der Veranstaltungsort konnte auf der Karte NICHT lokalisiert werden!"); }
}


function GotoTo($Position)
{
if ($Position)	{	$Marker = new GMarker($Position);
					MapsCenterWindow($MapContainer);
					$MapContainer.style.visibility = "visible";
					$Map.panTo($Position);
					$Map.addOverlay($Marker);
					$Map.showMapBlowup($Position,{ zoomLevel: 17, mapType: G_SATELLITE_MAP });
					$Map.checkResize;
				}
}						

function HideMap()
{
$MapContainer.style.visibility = "hidden";
}

function MapsCenterWindow($MyWindow)
{
var $ScrolledX 	= window.pageXOffset || document.body.scrollLeft;
var $ScrolledY 	= window.pageYOffset || document.body.scrollTop;

var $BodyX		= document.body.clientWidth || window.innerWidth;
var $BodyY		= document.body.clientHeight || window.innerHeight;

var MyoffsetWidth = 0;
var $MyoffsetHeight = 0;
if ($MyWindow.offsetWidth) $MyoffsetWidth = $MyWindow.offsetWidth;
if ($MyWindow.offsetHeight) $MyoffsetHeight = $MyWindow.offsetHeight;

$PosX		= Math.floor($ScrolledX + (($BodyX - $MyoffsetWidth) / 2));
$PosY		= Math.floor($ScrolledY + (($BodyY - $MyoffsetHeight) / 2));
$MyWindow.style.top = $PosY;
$MyWindow.style.left= $PosX;
}

function Locate($Nummer)
{
Request = CreateAjaxObject('ReturnAdr');
Request.open("POST", '/check/locate.php', true);
Request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
Request.send('Nummer=' + $Nummer);
}

function ReturnAdr($Request)
{
eval($Request.responseText);
}