/* -- 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 google.maps.Map(
		document.getElementById("map"),
		{
			"disableDefaultUI": true,
			"mapTypeId": google.maps.MapTypeId.HYBRID,
			"center": new google.maps.LatLng(stuttgartDeutschland.lat, stuttgartDeutschland.lng),
			"zoom": 5
		}
	);

 // ?
 //var mapObject=document.getElementById("map");

 if (1)
 {
	userControls=new CIdMapControl_V3(map);

	document.getElementById("positionskartemaptype").style.display="block";
	userControls.Add("positionskartemaptype", google.maps.ControlPosition.TOP_RIGHT);
	
	document.getElementById("positionskartezooming").style.display="block";	
	userControls.Add("positionskartezooming", google.maps.ControlPosition.TOP_LEFT);

	document.getElementById("idTippHinzufuegenLegende").style.display="block";	
	userControls.Add("idTippHinzufuegenLegende", google.maps.ControlPosition.RIGHT_BOTTOM);

	//userControls.Add("idTippHinzufuegenText", {align: G_ANCHOR_TOP_LEFT, x: 95, y: 10} );
	userControls.Add("idTippHinzufuegenText", google.maps.ControlPosition.TOP_LEFT);

	//userControls.Add("iconinfo", {align: G_ANCHOR_TOP_LEFT, y: 325});
	//userControls.Add("iconinfo", google.maps.ControlPosition.BOTTOM);
	
	//userControls.Add("uploadResult", {align: G_ANCHOR_TOP_LEFT, y: 175} );
	userControls.Add("uploadResult", google.maps.ControlPosition.BOTTOM);
			
	// Objekt für MapNamen erzeugen
	showMapNamesObject=new CShowMapNames("showMapNamesObject", map);
	showMapNamesObject.onlyShowMapNames=true;
	//showMapNamesObject.rasterizeNames=true;
	showMapNamesObject.eventBeforeShowFunction="fncMapNameClicked";

	// 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 google.maps.LatLng(vMapObject.lat, vMapObject.lng));
		map.setZoom(15);
		map.setMapTypeId(google.maps.MapTypeId.HYBRID);
	}

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

	google.maps.event.addListener(map, "dragend", event_mapChangeEnd);
	google.maps.event.addListener(map, "zoom_changed", event_mapChangeEnd);

	window.setTimeout("event_mapChangeEnd();", 1000);
 }
}

function fncUnload()
{//GUnload();
}


function showAddress_gi(address, vLongitude, vLatitude)
{
	alert("func: showAddress_gi");
	return;
	//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=google.maps.MapTypeId.SATELLITE;

	if (addressParts && address!="Stuttgart, Germany") {
		mapZoom=13;
		mapType=google.maps.MapTypeId.HYBRID;
	}

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


/* ----- */


function fncEnableClickEventForMap(vMapObject)
{
	mapClickObjectHandler=google.maps.event.addListener(vMapObject, "click", event_clickEvent);

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

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

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

function event_clickEvent(event)
{
	if (tmpMarker!=null) {
		tmpMarker.setPosition(event.latLng);
	} else {
		tmpMarker=new google.maps.Marker({
			"map": map,
			"position": event.latLng, 
			"draggable": true, 
			"icon": icons["iconLeer"]
		});

		google.maps.event.addListener(tmpMarker, "dragend", event_tmpMarkerDragEnd);
	}

	fncOpenMarkerEdit(tmpMarker);
}

function event_tmpMarkerDragEnd(event)
{
	p=event.latLng;

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

	smoothScrollTo({newpos: 650});

	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.setMapTypeId(google.maps.MapTypeId.SATELLITE);
		}
		if (tmpZoomLevel>7)
		{map.setMapTypeId(google.maps.MapTypeId.HYBRID);
		}

		// 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=tmpMarker.getPosition();
	
	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";


	smoothScrollTo({newpos: 650});

	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";
	
	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=../"+(typeof lang!="undefined"?"&lang="+lang:"");
	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"+(typeof lang!="undefined"?"&lang="+lang:"");
	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);
}


