var markersArray = [];
var zoom = 10;
var map;

function storeMapPosition(){
	var mapPosition = map.getCenter();
	var mapPositionX = mapPosition.lng();
	var mapPositionY = mapPosition.lat();
	var mapZoom = map.getZoom();
	$.post("ajax/storeMapPosition.php",{mapPositionX:mapPositionX,mapPositionY:mapPositionY,mapZoom:mapZoom},function(data){
	});
}

function loadMap(positionX,positionY,zoom){
	if(positionX && positionY && zoom){
		var latlng = new google.maps.LatLng(positionY,positionX);
	}
	else{
		var latlng = new google.maps.LatLng(50,9);
		var zoom = 5;
	}

	var myOptions = {
		zoom: zoom,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};

	map = new google.maps.Map(document.getElementById("map"), myOptions);
	infoWindow = new google.maps.InfoWindow;
	google.maps.event.addListener(map, 'tilesloaded', function(){
		bounds = this.getBounds();
		zoom = this.getZoom();
		storeMapPosition();
		if(refreshsperre!=1){
			refreshOverlays();
		}
    });
}

function loadMapEN(positionX,positionY,zoom){
	if(positionX && positionY && zoom){
		var latlng = new google.maps.LatLng(positionY,positionX);
	}
	else{
		var latlng = new google.maps.LatLng(50,9);
		var zoom = 5;
	}

	var myOptions = {
		zoom: zoom,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};

	map = new google.maps.Map(document.getElementById("map"), myOptions);
	infoWindow = new google.maps.InfoWindow;
	google.maps.event.addListener(map, 'tilesloaded', function(){
		bounds = this.getBounds();
		zoom = this.getZoom();
		storeMapPosition();
		if(refreshsperre!=1){
			refreshOverlaysEN();
		}
    });
}

function loadMapStellplatz(id_stellplatz) {
	var requestUrl = 'stellplatz.xml.php?id_stellplatz=' + id_stellplatz;
	$.ajax({
		type:"GET",
		url:requestUrl,
		dataType:"xml",
		success:parseXmlStellplatz
	});
}

function loadMapStellplatzEN(id_stellplatz) {
	var requestUrl = 'stellplatz_en.xml.php?id_stellplatz=' + id_stellplatz;
	$.ajax({
		type:"GET",
		url:requestUrl,
		dataType:"xml",
		success:parseXmlStellplatz
	});
}

function createMarker(point,html,icon,refresh) {
	var shape = {
	  coord: [0, 0, 19, 0, 19, 28, 0 , 28],
	  type: 'poly'
	};
	var marker = new google.maps.Marker({
		map: map,
		position: point,
		shadow: shadow0,
		icon: icon,
		shape: shape
	});
	bindInfoWindow(marker, map, infoWindow, html);
	markersArray.push(marker);
	return marker;
}

function createMarkerEN(point,html,icon,refresh) {
	var shape = {
	  coord: [0, 0, 19, 0, 19, 28, 0 , 28],
	  type: 'poly'
	};
	var marker = new google.maps.Marker({
		map: map,
		position: point,
		shadow: shadow0,
		icon: icon,
		shape: shape
	});
	bindInfoWindowEN(marker, map, infoWindow, html);
	markersArray.push(marker);
	return marker;
}

function refreshOverlays(id_stellplatz){
	deleteOverlays();
	var boundsNE = bounds.getNorthEast();
	var boundsSW = bounds.getSouthWest();
	var maxX = boundsNE.lng();
	var maxY = boundsNE.lat();
	var minX = boundsSW.lng();
	var minY = boundsSW.lat();
	var requestUrl = 'kartendaten.xml.php?minX=' + minX + '&maxX=' + maxX + '&minY=' + minY + '&maxY=' + maxY;
	var settings = $("#settings").serialize();
	var requestUrl = requestUrl + '&' + settings;
	$.ajax({
		type:"GET",
		url:requestUrl,
		dataType:"xml",
		success:parseXml
	});
}

function refreshOverlaysEN(id_stellplatz){
	deleteOverlays();
	var boundsNE = bounds.getNorthEast();
	var boundsSW = bounds.getSouthWest();
	var maxX = boundsNE.lng();
	var maxY = boundsNE.lat();
	var minX = boundsSW.lng();
	var minY = boundsSW.lat();
	var requestUrl = 'kartendaten_en.xml.php?minX=' + minX + '&maxX=' + maxX + '&minY=' + minY + '&maxY=' + maxY;
	var settings = $("#settings").serialize();
	var requestUrl = requestUrl + '&' + settings;
	$.ajax({
		type:"GET",
		url:requestUrl,
		dataType:"xml",
		success:parseXmlEN
	});
}


function callOverlay(id_stellplatz){
		var requestUrl = 'stellplatz.xml.php?id_stellplatz=' + id_stellplatz;
		$.ajax({
			type:"GET",
			url:requestUrl,
			dataType:"xml",
			success:parseXmlStellplatz
		});
}

function refreshOverlaysMerkliste(id_merkliste){
	deleteOverlays();
	var requestUrl = 'merkliste.xml.php?id_merkliste=' + id_merkliste;
	$.ajax({
		type:"GET",
		url:requestUrl,
		dataType:"xml",
		success:parseXmlMerkliste
	});
}

function refreshOverlaysMerklisteEN(id_merkliste){
	deleteOverlays();
	var requestUrl = 'merkliste_en.xml.php?id_merkliste=' + id_merkliste;
	$.ajax({
		type:"GET",
		url:requestUrl,
		dataType:"xml",
		success:parseXmlMerkliste
	});
}

function toggleToolbar(){
	//$("#toolbarcontent").toggle('blind');
	$("#toolbarcontent").dialog('open');
}

function deleteOverlays() {
  if (markersArray) {
    for (i in markersArray) {
      markersArray[i].setMap(null);
    }
    markersArray.length = 0;
  }
}

function parseXmlStellplatz(xml){
	$(xml).find("marker").each(function(){
		var point = new google.maps.LatLng(parseFloat($(this).attr("lat")),parseFloat($(this).attr("lng")));
		var entsorgung = parseFloat($(this).attr("entsorgung"));
		var html = $(this).find("beschreibung").text();
		var id_stellplatz = $(this).find("id_stellplatz").text();
		var zeichen = $(this).find("art").text();
		var myOptions = {
			zoom: 10,
			center: point,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		map = new google.maps.Map(document.getElementById("map"), myOptions);
		infoWindow = new google.maps.InfoWindow;
		google.maps.event.addListener(map, 'tilesloaded', function(){
			bounds = this.getBounds();
			zoom = this.getZoom();
			refreshOverlays();
			var markerStellplatz = createMarker(point,html,eval(zeichen),0);
		    infoWindow.setContent(html);
			infoWindow.open(map, markerStellplatz);
		});
	});
}

function parseXmlMerkliste(xml){
	var minX = $(xml).find("markers").attr("minX");
	var minY = $(xml).find("markers").attr("minY");
	var maxX = $(xml).find("markers").attr("maxX");
	var maxY = $(xml).find("markers").attr("maxY");
	var sw = new google.maps.LatLng(parseFloat(minY),parseFloat(minX));
	var ne = new google.maps.LatLng(parseFloat(maxY),parseFloat(maxX));
	var bounds = new google.maps.LatLngBounds(sw,ne);
	var point = bounds.getCenter();
	var myOptions = {
		zoom: 10,
		center: point,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	map = new google.maps.Map(document.getElementById("map"), myOptions);
	map.fitBounds(bounds);
	var zoom = map.getZoom();
	map.setZoom(zoom-1);
	$(xml).find("marker").each(function(){
		var point = new google.maps.LatLng(parseFloat($(this).attr("lat")),parseFloat($(this).attr("lng")));
		var entsorgung = parseFloat($(this).attr("entsorgung"));
		var html = $(this).find("beschreibung").text();
		var id_stellplatz = $(this).find("id_stellplatz").text();
		var zeichen = $(this).find("art").text();
		infoWindow = new google.maps.InfoWindow;
		google.maps.event.addListener(map, 'tilesloaded', function(){
			var markerStellplatz = createMarker(point,html,eval(zeichen),0);
		});
	});
}

function parseXml(xml){
	var anzahl = $(xml).find("markers").attr("anzahl");
	$(xml).find("marker").each(function(){
		var point = new google.maps.LatLng(parseFloat($(this).attr("lat")),parseFloat($(this).attr("lng")));
		var entsorgung = parseFloat($(this).attr("entsorgung"));
		var html = $(this).find("beschreibung").text();
		var id_stellplatz = $(this).find("id_stellplatz").text();

		var zeichen = $(this).find("art").text();
		if(entsorgung!='0' && zoom>=9){
			var zeichen = zeichen + 'VE';
		}

		var marker = createMarker(point,html,eval(zeichen),0);
	});
	var statusmeldung = anzahl + ' Stellpl&auml;tze im aktuellen Kartenbereich.';
	if(anzahl>=40){
		var statusmeldung = statusmeldung + ' Achtung: es wurden mehr Stellpl&auml;tze gefunden, als in der aktuellen Karte dargestellt werden k&ouml;nnen!';
	}
	if(zoom>=9){
		var statusmeldung = statusmeldung + ' Entsorgungsstationen eingeblendet.';
	}
	else{
		var statusmeldung = statusmeldung + ' Entsorgungsstationen ausgeblendet.';
	}
	$("#status").html(statusmeldung);
}

function parseXmlEN(xml){
	var anzahl = $(xml).find("markers").attr("anzahl");
	$(xml).find("marker").each(function(){
		var point = new google.maps.LatLng(parseFloat($(this).attr("lat")),parseFloat($(this).attr("lng")));
		var entsorgung = parseFloat($(this).attr("entsorgung"));
		var html = $(this).find("beschreibung").text();
		var id_stellplatz = $(this).find("id_stellplatz").text();

		var zeichen = $(this).find("art").text();
		if(entsorgung!='0' && zoom>=9){
			var zeichen = zeichen + 'VE';
		}

		var marker = createMarkerEN(point,html,eval(zeichen),0);
	});
	var statusmeldung = anzahl + ' sites in the current map view.';
	if(anzahl>=40){
		var statusmeldung = statusmeldung + ' Warning: the search has found more sites than can be displayed in the map!';
	}
	if(zoom>=9){
		var statusmeldung = statusmeldung + ' Disposal places are displayed.';
	}
	else{
		var statusmeldung = statusmeldung + ' Disposal places are hidden.';
	}
	$("#status").html(statusmeldung);
}

function bindInfoWindow(marker, map, infoWindow, html) {
  google.maps.event.addListener(marker, 'click', function() {
  	refreshsperre = 1;
    infoWindow.setContent(html);
    infoWindow.open(map, marker);
  });
  google.maps.event.addListener(infoWindow, 'closeclick', function() {
  	refreshsperre = 0;
  });
}

function bindInfoWindowEN(marker, map, infoWindow, html) {
  google.maps.event.addListener(marker, 'click', function() {
  	refreshsperre = 1;
    infoWindow.setContent(html);
    infoWindow.open(map, marker);
  });
  google.maps.event.addListener(infoWindow, 'closeclick', function() {
  	refreshsperre = 0;
  });
}



