var map;
var baseIcon;
var geoLocationServiceURL = 'http://www.marcatweb.de/maps/tours.xml';

// (Page load) Loads the map   
function load() {
	if (GBrowserIsCompatible()) {
    	map = new GMap2(document.getElementById('map'));
        map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		
        map.setCenter(new GLatLng(46.4378568950242, -4.921875), 3, G_HYBRID_MAP);		
		
		// Create a base icon for all of our markers that specifies the
		// shadow, icon dimensions, etc.
		baseIcon = new GIcon();
		baseIcon.shadow = 'http://www.marcatweb.de/maps/shadow.png';
		baseIcon.iconSize = new GSize(25, 36);
		baseIcon.shadowSize = new GSize(38, 23);
		baseIcon.iconAnchor = new GPoint(9, 34);
		baseIcon.infoWindowAnchor = new GPoint(9, 2);
		baseIcon.infoShadowAnchor = new GPoint(18, 25);
		
		// Load the initial markers from db
		loadMarkers();		
	} else {
	  	alert('Ihr Browser unterstützt GoogleMaps nicht.');
	}
} 

// Loads the markers from database
function loadMarkers() {
	var request = GXmlHttp.create();
	request.open('GET', geoLocationServiceURL, true);
	request.onreadystatechange = function() {
  		if (request.readyState == 4) {
			var xmlDoc = request.responseXML;
			var markers = xmlDoc.documentElement.getElementsByTagName('marker');
			for (var i = 0; i < markers.length; i++) {
  				var point = new GPoint(parseFloat(markers[i].getAttribute('lng')),
					parseFloat(markers[i].getAttribute('lat')));
  				
				var addr = '<strong>' + markers[i].getAttribute('title') + '</strong><br />';
				addr+= markers[i].getAttribute('location') + '<br />';
				addr+= markers[i].getAttribute('date') + '<br />';
				addr+= '<p align=right><small><a href="/bergwelt' + markers[i].getAttribute('id') + '">Tour anzeigen</a></small></p>';
   
				map.addOverlay(createMarker(point, i + 1, addr));
			}
  		}
	}
	request.send(null);
}

// Creates a marker at the given point with the given number label
function createMarker(point, number, text) {
	var icon = new GIcon(baseIcon);
  	icon.image = "http://www.marcatweb.de/maps/pointer.png";
  
  	var marker = new GMarker(point, icon);
  	GEvent.addListener(marker, "click", function() {
    	marker.openInfoWindowHtml(text);
  	});
		
  	return marker;
}

// Returns the location and zoom
function getCenterCoords() {
	map.getCenter();
	document.getElementById('cords').innerHTML = map.getCenter() + ',' + map.getZoom();
}

// Moves the viewport to the region
function gotoRegion(region) {
	var lat = 0;
	var lng = 0;
	var zoom = 1;
	
	switch(region) {
		case 1: // world
			lat = '37.16031654673677'; lng = '-49.21875'; zoom = 1;
			break;
		case 2: // europe
			lat = '46.4378568950242'; lng = '-4.921875'; zoom = 3;
			break;
		case 3: // alps west
			lat = '46.37725420510028'; lng = '7.899169921875'; zoom = 8;
			break;
		case 4: // alps center
			lat = '47.245678021018755'; lng = '9.635009765625'; zoom = 8;
			break;
		case 5: // alps east
			lat = '46.822616668804926'; lng = '11.6455078125'; zoom = 7;
			break;
		case 6: // usa west
			lat = '35.782170703266075'; lng = '-118.037109375'; zoom = 5;
			break;
		case 7: // atlantic
			lat = '30.883369321692267'; lng = '-16.89697265625'; zoom = 6;
			break;
		case 8: // scottland
			lat = '56.298253152913865'; lng = '-5.218505859375'; zoom = 7;
			break;
		case 9: // la reunion
			lat = '-21.143430503912906'; lng = '55.4644775390625'; zoom = 9;
			break;
		case 10: // corse
			lat = '42.09007006868398'; lng = '9.0252685546875'; zoom = 8;
			break;	
	}
		
	map.setCenter(new GLatLng(lat, lng));
	map.setZoom(zoom);
}
