var map;
var geocoder;
  
function load() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.setMapType(G_HYBRID_MAP);
		map.enableScrollWheelZoom();
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(40, 25), 1);
		geocoder = new GClientGeocoder();
	}
	
	GEvent.addListener(map, "moveend", function() {
		eventListener();
	});
	
}

function loadCustom(lat,lon) {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.enableScrollWheelZoom();
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(lat, lon), 7);
		geocoder = new GClientGeocoder();
		eventListener();
	}
	
	GEvent.addListener(map, "moveend", function() {
		eventListener();
	});
}

function eventListener(){
	var boundingBox = map.getBounds();
	  var southWest = boundingBox.getSouthWest();
	  var northEast = boundingBox.getNorthEast();
	  lat1 = southWest.lat();
	  lat2 = northEast.lat();
	  lon1 = southWest.lng();
	  lon2 = northEast.lng();	
	  showImagesOnChange(lat1,lat2,lon1,lon2);
}

function showImagesOnChange(lat1,lat2,lon1,lon2) {
	var http_request = false;
	var url = '/include/ajaxMapLoadImages.html?lat1='+lat1+'&lat2='+lat2+'&lon1='+lon1+'&lon2='+lon2;
        if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/plain');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }


    if (!http_request) {
        return false;
    }
    http_request.open('GET', url, true);
    
   http_request.onreadystatechange = function(){
    	
	    if (http_request.readyState == 4) {
        	if(http_request.status == 200){
	        	var resp = http_request.responseText;
	        	document.getElementById('images').innerHTML = resp;
        	}        	  
	    }
    };
    
    http_request.send(null);
}

function addImageMarker(lat, lon, myIcon, clusterer, imageId, imageTitle) {
	point = new GLatLng(lat, lon);
	markerOptions = {icon:myIcon};
    marker = new GMarker(point, markerOptions);
    clusterer.SetIcon(myIcon);
    clusterer.AddMarker(marker, '<img src=\"\/include/image.jpg?imageId=' +imageId+'&size=50" align=\"absmiddle\" style=\"margin-right:5px\" onClick=\"getZoomedView(' +lat +',' +lon+ ')\"/>' +unescape(imageTitle));
}

function getZoomedView(lat, lon) {
	var currentZoom = map.getBoundsZoomLevel(map.getBounds());
	map.setCenter(new GLatLng(lat, lon), currentZoom +2);
}

  function showAddress(address) {
    if (geocoder) {
      geocoder.getLocations(address, addAddressToMap);
    }
  }
  
  function addAddressToMap(response) {
    if (!response || response.Status.code != 200) {
      alert("Sorry, wir konnten den Standort nicht finden. Versuchs nochmal mit einer anderen Anfrage.");
    } else {
    	place = response.Placemark[0];
		createMarker(place.Point.coordinates[1], place.Point.coordinates[0]);  
		marker.openInfoWindowHtml(place.address + '<br>');
    }
  }
  
  function createMarker(lat, lon) {
  	point = new GLatLng(lat, lon);
  	map.setCenter(point,5);
    marker = new GMarker(point);
    GEvent.addListener(marker, "click", function(){ marker.hide(); marker.closeInfoWindow() } );
    map.addOverlay(marker);
  }
  
  function getZIndex() {
  	return 1000;
  }	