// Creates a marker whose info window displays the letter corresponding
// to the given index.
function createMarker(point, index) {

  var currentAdress = list[outerIndex];
  
  // Create a lettered icon for this point using our icon class
  var letter = String.fromCharCode("A".charCodeAt(0) + outerIndex);
  var letteredIcon = new GIcon(baseIcon);
  letteredIcon.image = "../img/at_small.gif";

  // Set up our GMarkerOptions object
  markerOptions = { icon:letteredIcon };
  var marker = new GMarker(point, markerOptions);

  GEvent.addListener(marker, "mouseover", function() {
	marker.openInfoWindowHtml("" + currentAdress + "");
  });
  
  outerIndex++;
  return marker;
}

function loadMaps(adresse) {

		
 	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		var geocoder = new GClientGeocoder();

		var bounds = map.getBounds();
		var southWest = bounds.getSouthWest();
		var northEast = bounds.getNorthEast();
		var lngSpan = northEast.lng() - southWest.lng();
		var latSpan = northEast.lat() - southWest.lat();

		var sw=null;
		var ne=null;
		
		var	adressen=adresse.split(';');

		var bounds=new GLatLngBounds();
				
		//walk through each of the adresses
		for (var i = 0; i < adressen.length; i++) {
			
			var first=true;
			var zoomLevelDef=14;
			
			//skip empty adresses
			if(adressen[i]=='')
				continue;
			
			
			geocoder.getLatLng(adressen[i], function(point) {
				
				if(point) {
					
					/*
					if(first){
						//initialize points
						sw=point;
						ne=point;
						first=false;
					}

					if(point<sw){
						//find the point with the most south west-wards coordinates
						sw=point;
					}

					if(point>ne){
						//find the point with the most north east-wards coordinates
						ne=point;
					}
					*/
					map.addOverlay(createMarker(point, i));
					//init a GLatLngBounds instance with the points
					//var bounds2 = new GLatLngBounds(point);
					bounds.extend(point);
					
					//init the map
					
					
					
					/*if(map.getZoom()<zoomLevelDef)	{
						//if map hasnt the expected zoomLevel just swap the points
						bounds2 = new GLatLngBounds(smallest,tallest);
						map.setCenter(bounds2.getCenter(), map.getBoundsZoomLevel(bounds2));
					}*/
    				
				}
				map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
			}
			
		);
		}
	}
}
