/* -- Variablen -- */
var landregionortauswahl=new CAJAX("landregionortauswahl");
var fulltextsearch=new CAJAX("fulltextsearch");
var userMapControls=null;
var mapChangeEndRunning=false;
var userControls=new Array();
var urlaubertippObject="";

var showMapNamesObject=null;

var map=null;

var tmpMarker=null;
var mapClickObjectHandler=null;
var tmpFotoNumber=1;

/* -- Funktionen -- */

function fncLoad(vMapObject)
{
 map=new GMap2(document.getElementById("map"));
 var mapObject=document.getElementById("map");

 if (GBrowserIsCompatible())
 {
  map.enableContinuousZoom();
  map.enableDoubleClickZoom();
	//enableMouseWheelZoom(document.getElementById("map"), map);

	userControls=new CIdMapControl(map);

	userControls.Add("positionskartemaptype", {align: G_ANCHOR_TOP_RIGHT});
	document.getElementById("positionskartemaptype").style.display="block";

	userControls.Add("positionskartezooming", {align: G_ANCHOR_TOP_LEFT});
	document.getElementById("positionskartezooming").style.display="block";	

	userControls.Add("idTippHinzufuegenLegende", {align: G_ANCHOR_BOTTOM_RIGHT});
	document.getElementById("idTippHinzufuegenLegende").style.display="block";	

	userControls.Add("idTippHinzufuegenText", {align: G_ANCHOR_TOP_LEFT, x: 95, y: 10} );
	userControls.Add("iconinfo", {align: G_ANCHOR_TOP_LEFT, y: 325});
	
	userControls.Add("uploadResult", {align: G_ANCHOR_TOP_LEFT, y: 175} );
	//document.getElementById("uploadResult").style.display="block";	
			
	// Objekt für MapNamen erzeugen
	showMapNamesObject=new CShowMapNames("showMapNamesObject", map);
	showMapNamesObject.onlyShowMapNames=true;
	//showMapNamesObject.rasterizeNames=true;
	showMapNamesObject.eventBeforeShowFunction="fncMapNameClicked";

	/* Erstpositionierung auf Stuttgart, Deutschland */
	//showAddress_gi((vMapObject.googleAddress==""?"Stuttgart, Germany":vMapObject.googleAddress), vMapObject.lng, vMapObject.lat);	
	map.setCenter(new GLatLng(stuttgartDeutschland.lat, stuttgartDeutschland.lng), 5, G_SATELLITE_MAP);
	
	// Evtl. vorhandene Lat-Lng verwenden
	if (typeof vMapObject.geoRect!="undefined" && vMapObject.geoRect.minLat && vMapObject.geoRect.minLng && vMapObject.geoRect.maxLat && vMapObject.geoRect.maxLng)
	{setLatLngRect(map, vMapObject.geoRect);
	}

	if (typeof vMapObject.ort!="undefined" && vMapObject.ort==true && vMapObject.lat!="" && vMapObject.lng!="")
	{map.setCenter(new GLatLng(vMapObject.lat, vMapObject.lng), 15, G_HYBRID_MAP);
	}


	if (vMapObject.land=='' && vMapObject.region=='')
	{
		window.setTimeout("showMapNamesObject.showCountryNames();", 1000);
		window.setTimeout("fncGetGeoInfosMap(null);", 1000);
	}
	if (vMapObject.land!='' && vMapObject.region=='')
	{
		window.setTimeout("showMapNamesObject.showRegionNames('"+vMapObject.land+"');", 1000);
		window.setTimeout("fncGetGeoInfosMap(null);", 1000);
	}
	if (vMapObject.land!="" && vMapObject.region!="")
	{
		event_mapChangeEnd();
		//fncEnableClickEventForMap(map);
	}

	GEvent.addListener(map, "zoomend", event_mapChangeEnd);
	GEvent.addListener(map, "moveend", event_mapChangeEnd);
 }
}

function fncUnload()
{GUnload();
}


function showAddress_gi(address, vLongitude, vLatitude)
{
	map.clearOverlays();

 if (vLongitude!="" && vLatitude!="")
 {var point=new GLatLng(vLatitude*1, vLongitude*1);
  callback_showAddressPoint(address, point);
 }
 else
 {var geocoder=new GClientGeocoder();
   geocoder.getLatLng(address,
   function (point)
   {if (point!=null)
    {callback_showAddressPoint(address, point);
    }
   }
  );
 }
}

function callback_showAddressPoint(address, point)
{
	var addressParts=address.match(/,/g);
	
	var mapZoom=(address=="Stuttgart, Germany"?4:6);
	var mapType=G_SATELLITE_MAP;

	if (addressParts && address!="Stuttgart, Germany")
	{
		mapZoom=13;
		mapType=G_HYBRID_MAP;
	}

	if (map.getZoom()!=mapZoom || map.getCurrentMapType()!=mapType)
	{map.setCenter(point, mapZoom, mapType);
	}
	else if (point)
	{//map.panTo(new GLatLng(point.y, point.x));
	}
}


/* ----- */


function fncEnableClickEventForMap(vMapObject)
{
	mapClickObjectHandler=GEvent.addListener(vMapObject, "click", event_clickEvent);

	//document.getElementById("idMarkerEditBeschreibungTipp").style.display="block";
	//document.getElementById("idMarkerEditBeschreibungInfo").style.display="none";
}

function fncDisableClickEventForMap(vMapObject)
{
	fncCloseMarkerEdit();
	if (mapClickObjectHandler!=null)
	{GEvent.removeListener(mapClickObjectHandler);
	}
	mapClickObjectHandler=null;

	//document.getElementById("idMarkerEditBeschreibungTipp").style.display="none";
	//document.getElementById("idMarkerEditBeschreibungInfo").style.display="block";
}

function event_clickEvent(marker, point)
{
	if (marker==null) 
	{
		if (tmpMarker!=null)
		{
			map.removeOverlay(tmpMarker);
			tmpMarker=null;
		}

		tmpMarker=new GMarker(point, {draggable: true, icon: icons["iconLeer"]});
		GEvent.addListener(tmpMarker, "dragend", event_tmpMarkerDragEnd);
		map.addOverlay(tmpMarker);

		fncOpenMarkerEdit(tmpMarker);
  }
	else
	{
		if (marker==tmpMarker)
		{fncCloseMarkerEdit();
		}

		/*if (tmpMarker!=null)
		{
			var tmpMarkerPoint=marker.getPoint();
			tmpMarkerPoint=tmpMarkerPoint.lng()+","+tmpMarkerPoint.lat();

			var tmpTempPoint=tmpMarker.getPoint();
			tmpTempPoint=tmpTempPoint.lng()+","+tmpTempPoint.lat();

			if (tmpMarkerPoint==tmpTempPoint)
			{fncCloseMarkerEdit();
			}
			else
			{fncCloseMarkerEdit();
			}
		}
		*/
	
	}
}

function event_tmpMarkerDragEnd()
{
	p=tmpMarker.getPoint();

	document.frmMarkerEdit.latitude.value=p.lat();
	document.frmMarkerEdit.longitude.value=p.lng();

	//map.panTo(p);
	smoothScrollTo({newpos: 750});

	fncLoadLandRegionOrtAuswahl();
}

function event_mapChangeEnd(oldZoomLevel, newZoomLevel)
{
	if (mapChangeEndRunning==true)
	{return;
	}

	mapChangeEndRunning=true;
	tmpZoomLevel=map.getZoom();

	map.clearOverlays();

	// Aufrufe und Änderungen durchführen
	if (map!=null)
	{
		//alert(tmpZoomLevel);

		if (tmpZoomLevel<6) // Bei einemn Zoom-Level kleiner 8 die Landnamen einblenden
		{window.setTimeout("showMapNamesObject.showCountryNames();", 10);
		}
		if (tmpZoomLevel>=6 && tmpZoomLevel<=8) // Zoom-Level zwischen 6 und 8, die Regionname der Map-Maße einblenden
		{window.setTimeout("showMapNamesObject.showRegionNames('mapview');", 10);
		}
		
		// Map-Typ anhand des Zoom-Faktor setzen
		if (tmpZoomLevel<=7)
		{map.setMapType(G_SATELLITE_MAP);
		}
		if (tmpZoomLevel>7)
		{map.setMapType(G_HYBRID_MAP);
		}

		// Urlaubertipp zulassen
		if (tmpZoomLevel>=10)
		{fncEnableClickEventForMap(map);
		}
		else // Keinen Urlaubertipp zulassen
		{fncDisableClickEventForMap(map);
		}
	}

	// Reiseinfos einblenden
	window.setTimeout("fncGetGeoInfosMap(null);", 10);
}


function fncOpenMarkerEdit(vMarkerObject)
{
	var tmpMarkerPos=vMarkerObject.getPoint();
	
	document.frmMarkerEdit.longitude.value=tmpMarkerPos.lng();
	document.frmMarkerEdit.latitude.value=tmpMarkerPos.lat();

	document.getElementById("idLandRegionOrtAuswahl").innerHTML=landregionortLoaderText;
	//document.getElementById("idMarkerEditBeschreibung").style.display="none";
	//document.getElementById("iconinfo").style.display="none";
	
	document.getElementById("idMarkerEditError").style.display="none";
	document.getElementById("idMarkerEdit").style.display="block";


	//map.panTo(new GLatLng(document.frmMarkerEdit.latitude.value, document.frmMarkerEdit.longitude.value));
	smoothScrollTo({newpos: 750});

	fncLoadLandRegionOrtAuswahl();
}

function fncOpenMarkerEditLatLng(vLat, vLng)
{
	map.closeInfoWindow();

	document.frmMarkerEdit.latitude.value=vLat;
	document.frmMarkerEdit.longitude.value=vLng;

	document.getElementById("idLandRegionOrtAuswahl").innerHTML=landregionortLoaderText;
	//document.getElementById("idMarkerEditBeschreibung").style.display="none";
	
	document.getElementById("idMarkerEditError").style.display="none";
	document.getElementById("idMarkerEdit").style.display="block";
	
	//map.panTo(new GLatLng(document.frmMarkerEdit.latitude.value, document.frmMarkerEdit.longitude.value));
	smoothScrollTo({newpos: 750});


	fncLoadLandRegionOrtAuswahl();
}

function fncLoadLandRegionOrtAuswahl()
{
	landregionortauswahl.URL="../reiseinfo/geoinfos_inc/landregionort_auswahl.php";
	landregionortauswahl.SendRequestData="lat="+escape(document.frmMarkerEdit.latitude.value)+"&lng="+escape(document.frmMarkerEdit.longitude.value)+"&ajaxRelativeDirPath=../";
	landregionortauswahl.CallbackFunction="callback_fncLoadLandRegionOrtAuswahl";
	landregionortauswahl.Call();
}

function callback_fncLoadLandRegionOrtAuswahl()
{
	document.getElementById("idLandRegionOrtAuswahl").innerHTML=landregionortauswahl.Data;
	
	// Evtl. Javascript-Code ausführen
	htmlJsExecution(landregionortauswahl.Data);
}

function fncCloseMarkerEdit()
{
	document.getElementById("idMarkerEditError").style.display="none";
	document.getElementById("idMarkerEdit").style.display="none";

	//document.getElementById("idMarkerEditBeschreibung").style.display="block";

	document.frmMarkerEdit.reset();
	
	if (tmpMarker!=null)
	{
		map.removeOverlay(tmpMarker);
		tmpMarker=null;
	}

	tmpFotoNumber=0;
	document.getElementById("idNewMarkerFotos").innerHTML="<div id='idNewMarkerFoto'></div>";
}


function fncMarkerAddFoto()
{
	document.getElementById("idNewMarkerFoto").innerHTML="<table cellpadding='0px' cellspacing='0px' border='0'><tr><td valign='top' align='center' width='25px'><font size='+1'>"+(++tmpFotoNumber)+"</font></td><td valign='top'><input type='file' name='markerFoto[]'></td></tr></table><div id='idNewMarkerFotoTmp'><br></div>";
	document.getElementById("idNewMarkerFoto").id="";
	document.getElementById("idNewMarkerFotoTmp").id="idNewMarkerFoto";
}


/*function fncMarkerPictureStep(vGeoPosNr, vStep)
{
	currentMarkerPic+=vStep;

	if (currentMarkerPic<0)
	{currentMarkerPic=markerPictures.length-1;
	}
	if (currentMarkerPic>markerPictures.length-1)
	{currentMarkerPic=0;
	}

	document.getElementById("idGeoInfoBildPos"+vGeoPosNr).innerHTML=(currentMarkerPic+1);
	document.getElementById("idGeoInfoBild"+vGeoPosNr).src=markerPictures[currentMarkerPic];
}*/

function fncSchnellsucheSearch()
{
	fulltextsearch.URL="../global/search_europakarte_fulltext.php";
	fulltextsearch.SendRequestData="fulltext="+escape(document.frmSchnellsucheFulltext.lroSchnellsucheFulltext.value)+"&geoinfo=true";
	fulltextsearch.CallbackFunction="callback_fncSchnellsucheSearch";

	fncShowHide("idSchnellsucheDiv"); fncShowHide("idSchnellsucheWaiter");

	fulltextsearch.Call();	

	return false;
}

function callback_fncSchnellsucheSearch()
{
	//document.frmGeoInfoFulltext.eingabeFeldFulltext.value="";
	//alert(fulltextsearch.Data);
	eval(fulltextsearch.Data);
}


function fncSwitchTippLegende()
{
	// Zentrieren, nur wenn angezeigt wird
	if (document.getElementById("idTippHinzufuegenText").style.display=="none" && 0) // Nicht sichtbar, wird auf sichtbar gestellt
	{
		w=document.getElementById("idTippHinzufuegenText").style.width;
		w=w.replace(/[^\d]/g, "");

		document.getElementById("idTippHinzufuegenText").style.left=((window.innerWidth?window.innerWidth:window.document.body.offsetWidth)/2)-(w/2);
	}

	document.getElementById("iconinfo").style.display="none";

	document.getElementById("idTippHinzufuegenText").style.display=(document.getElementById("idTippHinzufuegenText").style.display=="block"?"none":"block");
	document.getElementById("idTippHinzufuegenLegende").style.display=(document.getElementById("idTippHinzufuegenLegende").style.display=="block"?"none":"block");
}

function fncCancelKey27(e)
{
	if (e==null || e.keyCode==27) // Bei Escape ...
	{
		document.getElementById("iconinfo").style.display="none";
		document.getElementById("idTippHinzufuegenLegende").style.display="block";

		document.getElementById("idTippHinzufuegenText").style.display="none";

		fncCloseMarkerEdit();
	}
}

function fncLROChanged(vDataObject)
{
	if (vDataObject.formFieldName=="land")
	{
		clearComboBox(document.frmMarkerEdit.region); 
		CDropDownBox_SetDefaultValue('region')

		clearComboBox(document.frmMarkerEdit.ort);
		CDropDownBox_SetDefaultValue('ort')
		
		fillComboBox(document.frmMarkerEdit.region, "../ajax/getinfo.php?infotype=region&land="+vDataObject.value+"&useID=true&useOldIDs=true&keineZahlen=true");
	}

	if (vDataObject.formFieldName=="region")
	{
		clearComboBox(document.frmMarkerEdit.ort);
		CDropDownBox_SetDefaultValue('ort')
		
		fillComboBox(document.frmMarkerEdit.ort, "../ajax/getinfo.php?infotype=ort&land="+document.frmMarkerEdit.land.value+"&region="+vDataObject.value+"&useID=true&useOldIDs=true&keineZahlen=true");
	}


}

function fncShowUploadResult()
{
	document.getElementById("uploadResult").style.display="block";
	window.setTimeout(function () {
		document.getElementById("uploadResult").style.display="none";
		} , 5000);
}
