/* ------------------------------------------------------------------------------------------ Google maps */

var geocoder = null;
function map(map_array) {
	if (GBrowserIsCompatible()) {
		bereich = '';
		id = 1;
		w = '';
		h = '';
		pos_x = 0;
		pos_y = 0;
		pos_x_selected = 0;
		pos_y_selected = 0;
		center_x = 0;
		center_y = 0;
		zoom = 10;
		strasse = '';
		stadt = '';
		land = '';
		icon = '';
		icon_selected = '';
		selected = -1;
		maptype = 'G_PHYSICAL_MAP';
		controls = 'small';
		controlstatus = 0;
		error = 'Keine Karte verfügbar';
		padding = "12px 0px 15px 0px";
		lat = 0;
		lng = 0;
		
		for (var eigenschaft in map_array) {
			/* alert (eigenschaft + ": " + map_array[eigenschaft]); */
			window[eigenschaft] = map_array[eigenschaft];
		}
		
		/* alert(lat); */
		
		geocoder = new GClientGeocoder();
		
		document.getElementById('map' + id).style.height = h + "px";
		var map = new GMap2(document.getElementById('map' + id));
		
		/*
		if (controls == 'large') {
			map.addControl(new GLargeMapControl());
		} else {
			map.addControl(new GSmallMapControl());
		}
		
		
        	
		if (controlstatus == 1) {
			map.addControl(new GMapTypeControl());
		}
		*/
		
		var places_x = lat.split("###");
		var places_y = lng.split("###");
		var titles = title.split("###");
		
		map.setCenter(new GLatLng(center_x, center_y), 12);
		
      var bounds = new GLatLngBounds();
      var points = [];
  		
  		for(i=0; i<places_x.length; i++) {
		   var point = new GLatLng(places_x[i], places_y[i]);
			points.push(new GLatLng(places_x[i],places_y[i]));
                            
         if(selected >= 0 && i==selected) {
				var new_icon_selected = new GIcon();
				new_icon_selected.image = icon_selected;
				new_icon_selected.iconAnchor = new GPoint(pos_x_selected, pos_y_selected);
				var marker = new GMarker(point, {title: titles[i], icon:new_icon_selected });
				
			} else {
				var new_icon = new GIcon();
				new_icon.image = icon;
				new_icon.iconAnchor = new GPoint(pos_x, pos_y);
				var marker = new GMarker(point, {title: titles[i], icon:new_icon });
			}
			
			map.addOverlay(marker);
			bounds.extend(point);
		}
		
		// optimalen zoomlevel für die points setzen
		map.setZoom(map.getBoundsZoomLevel(bounds));
		
		map.addControl(new GSmallMapControl());
      /* map.addControl(new GMapTypeControl()); */
      
      map.addOverlay(new GPolyline(points, "#333", 1, 0.7));
      
		// zusaetzlich soll die karte auch noch optimal zentriert werden
		//var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
		//var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;

		//map.setCenter(new GLatLng(clat,clng));

		
      
		//alert(controlstatus + controls);
		
		map.addMapType(maptype);
		map.setMapType(maptype);
		
		document.getElementById('id_map' + id).style.display = "block";
	}
}

function map_open() {
	var height = document.getElementById('map').style.height;
	
	if (height == "0px") {
		document.getElementById('map').style.height = "340px";
	} else {
		document.getElementById('map').style.height = "0px";
	}
}

function map_close() {
	document.getElementById('map').style.display = "none";
}

function map_show() {
	if (GBrowserIsCompatible()) {
		geocoder = new GClientGeocoder();
		map = new GMap2(document.getElementById('map'));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(20.3, 10.7), 1);
	}
}
