	var map;
	var moveListener;
	var clickListener;
	var	infowindowopenListener;
	var	infowindowcloseListener;
	
	var show_on_map = false;
	
	var markers = new Array();
	var map_markers = new Array();

	var directions;
	var directions_from_to = 'from';
	
	var pin_normal;
	var pin_preferred;
	
	var capital = {AF:"Kabul",
AL:"Tirana",
DZ:"Algiers",
AD:"Andorra la Vella",
AO:"Luanda",
AG:"St. John's",
AZ:"Baku",
AR:"Buenos Aires",
AU:"Canberra",
AT:"Vienna",
BS:"Nassau",
BH:"Manama",
BD:"Dhaka",
AM:"Yerevan",
BB:"Bridgetown",
BE:"Brussels",
BM:"Hamilton",
BT:"Thimphu",
BO:"La Paz",
BA:"Sarajevo",
BW:"Gaborone",
BR:"Brasilia",
BZ:"Belmopan",
SB:"Honiara",
VG:"Road Town",
BN:"Bandar Seri Begawan",
BG:"Sofia",
MM:"Rangoon",
BI:"Bujumbura",
BY:"Minsk",
KH:"Phnom Penh",
CM:"Yaoundé",
CA:"Ottawa",
CV:"Praia",
KY:"George Town",
CF:"Bangui",
LK:"Colombo",
TD:"N'Djamena",
CL:"Santiago",
CN:"Beijing",
TW:"Taipei",
CO:"Bogotà ",
KM:"Moroni",
CG:"Brazzaville",
CD:"Kinshasa",
CK:"Avarua",
CR:"San José",
HR:"Zagreb",
CU:"Havana",
CY:"Nicosia",
CZ:"Prague",
BJ:"Porto-Novo",
DK:"Copenhagen",
DM:"Roseau",
DO:"Santo Domingo",
EC:"Quito",
SV:"San Salvador",
GQ:"Malabo",
ET:"Addis Ababa",
ER:"Asmara",
EE:"Tallinn",
FO:"Tòrshavn",
FK:"Stanley",
FJ:"Suva",
FI:"Helsinki",
FR:"Paris",
GF:"Cayenne",
PF:"Papeete",
DJ:"Djibouti",
GA:"Libreville",
GE:"Tbilisi",
GM:"Banjul",
DE:"Berlin",
GH:"Accra",
GI:"Gibraltar",
KI:"Tarawa",
GR:"Athens",
GL:"Nuuk",
GD:"St. George's",
GP:"Basse-Terre",
GU:"Agana",
GT:"Guatemala City",
GN:"Conakry",
GY:"Georgetown",
HT:"Port-au-Prince",
VA:"Vatican City",
HN:"Tegucigalpa",
HU:"Budapest",
IS:"Reykjavik",
IN:"New Delhi",
ID:"Jakarta",
IR:"Tehran",
IQ:"Baghdad",
IE:"Dublin",
IL:"Jerusalem",
IT:"Rome",
CI:"Yamoussoukro",
JM:"Kingston",
JP:"Tokyo",
KZ:"Astana",
JO:"Amman",
KE:"Nairobi",
KP:"Pyongyang",
KR:"Seoul",
KW:"Kuwait",
KG:"Bishkek",
LA:"Viangchan",
LB:"Beirut",
LS:"Maseru",
LV:"Riga",
LR:"Monrovia",
LY:"Tripoli",
LI:"Vaduz",
LT:"Vilnius",
LU:"Luxembourg",
MG:"Antananarivo",
MW:"Lilongwe",
MY:"Kuala Lumpur",
MV:"Malé",
ML:"Bamako",
MT:"Valletta",
MQ:"Fort-de-France",
MR:"Nouakchott",
MU:"Port Louis",
MX:"Mexico City",
MC:"Monaco-ville",
MN:"Ulan Bator",
MD:"Chisinau",
MS:"Plymouth",
MA:"Rabat",
MZ:"Maputo",
OM:"Muscat",
NA:"Windhoek",
NR:"Yaren",
NP:"Kathmandu",
NL:"Amsterdam",
AN:"Willemstad",
AW:"Oranjestad",
NC:"Nouméa",
VU:"Port-Vila",
NZ:"Wellington",
NI:"Managua",
NE:"Niamey",
NG:"Abuja",
NU:"Alofi",
NF:"Kingston",
NO:"Oslo",
MP:"Saipan",
FM:"Palikir",
MH:"Majuro",
PW:"Koror",
PK:"Islamabad",
PA:"Panama City",
PG:"Port Moresby",
PY:"Asuncion",
PE:"Lima",
PH:"Manila",
PN:"Adamston",
PL:"Warsaw",
PT:"Lisbon",
GW:"Bissau",
PR:"San Juan",
QA:"Doha",
RE:"Saint-Denis",
RO:"Bucharest",
RU:"Moscow",
RW:"Kigali",
SH:"Jamestown",
KN:"Basseterre",
AI:"The Valley",
LC:"Castries",
PM:"St.-Pierre",
VC:"Kingstown",
SM:"San Marino",
ST:"São Tomé",
SA:"Riyadh",
SN:"Dakar",
SC:"Victoria",
SL:"Freetown",
SG:"Singapore City",
SK:"Bratislava",
VN:"Hanoi",
SI:"Ljubljana",
SO:"Mogadishu",
ZA:"Cape Town",
ZW:"Harare",
ES:"Madrid",
SD:"Khartoum",
SR:"Paramaribo",
SJ:"Longyearbyen",
SZ:"Mbabane",
SE:"Stockholm",
CH:"Bern",
SY:"Damascus",
TJ:"Dushanbe",
TH:"Bangkok",
TG:"Lomé",
TO:"Nuku'alofa",
TT:"Port-of-Spain",
AE:"Abu Dhabi",
TN:"Tunis",
TR:"Ankara",
TM:"Ashgabat",
TC:"Cockburn Town",
TV:"Funafuti",
UG:"Kampala",
UA:"Kiev",
MK:"Skopje",
EG:"Cairo",
GB:"London",
US:"Washington",
BF:"Ouagadougou",
UY:"Montevideo",
UZ:"Tashkent",
VE:"Caracas",
TZ:"Dodoma",
VI:"Charlotte Amalie",
WF:"Mata-Utu",
WS:"Apia",
YE:"Sanaa",
YU:"Belgrade",
ZM:"Lusaka"};

	// Call this function when the page has been loaded
	function initialize() {
		map = new google.maps.Map2(document.getElementById("map"));
        map.addControl(new GLargeMapControl());

		pin_normal = new GIcon();
		pin_normal.image = "pin.png";
//		pin_normal.image = "http://maps.gstatic.com/intl/en_ALL/mapfiles/marker.png";
//		pin_normal.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
		pin_normal.shadow = "http://maps.gstatic.com/intl/en_ALL/mapfiles/shadow50.png";
		pin_normal.iconSize = new GSize(34, 34);
		pin_normal.shadowSize = new GSize(34, 34);
		pin_normal.iconAnchor = new GPoint(1, 1);
		pin_normal.infoWindowAnchor = new GPoint(1, 1);
		
// 		pin_preferred = new GIcon();
// 		pin_preferred.image = "pin_preferred.gif";
// 		pin_preferred.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
// 		pin_preferred.iconSize = new GSize(20, 34);
// 		pin_preferred.shadowSize = new GSize(22, 20);
// 		pin_preferred.iconAnchor = new GPoint(1, 1);
// 		pin_preferred.infoWindowAnchor = new GPoint(1, 1);
		
		//Center the map
		var startPoint = new GLatLng(startLat, startLng);
		map.setCenter(startPoint, start_zoom_level);

		geocoder = new GClientGeocoder();
		
		//geocoder.getLocations(capital[map_country] + ',' + map_country, showCountry);

		//Draw a circle on map around startPoint, with radius 2 miles and 40 points

		directions = new GDirections(map);
		//directions.load("from: 500 Memorial Drive, Cambridge, MA to: 4 Yawkey Way, Boston, MA 02215 (Fenway Park)");
// 	    GEvent.addListener(directions, "load", function() {
// 			alert('load event');
// 			});
// 	    GEvent.addListener(directions, "addoverlay", function() {
// 			alert('addoverlay event');
// 			});
	    GEvent.addListener(directions, "error", function() {
			alert(localized_no_directions_found);
			});
		setmarkers();

		dragstartListener = GEvent.addListener(map, "dragstart", function() {
			map.closeInfoWindow();
		});
		
		dragListener = GEvent.addListener(map, "drag", function() {
			drawCircle(map, map.getCenter(), 20, 40);
			//setmarkers();
		});
	
		moveListener = GEvent.addListener(map, "moveend", function() {
		  setmarkers();
		});

		infowindowopenListener = GEvent.addListener(map, "infowindowopen", function() {
 		 	GEvent.removeListener(moveListener);
		});
	
		infowindowcloseListener = GEvent.addListener(map, "infowindowclose", function() {
			if(show_on_map) {
				// Window closing for reopening another one
				//alert('close window show_on_map');
				show_on_map = false;
			} else {
			  	setmarkers();
				moveListener = GEvent.addListener(map, "moveend", function() {
				  setmarkers();
				});
			}
		});

		GEvent.addListener(directions,"error", function() { 
		       //alert("Failed: "+directions.getStatus().code);
				//alert('address not found');
		      });
		
// 		clickListener = GEvent.addListener(map, "click", function(overlay, latlng, overlaylatlng) {
// 			var latinput = document.getElementById("geotag_latitude");
// 			latinput.value = latlng.lat();
// 			var lnginput = document.getElementById("geotag_longitude");
// 			lnginput.value = latlng.lng();
// 		});

// 		GEvent.addListener(map, "zoomend", function() {
// 		  alert("zoomend");
// 		});
// 	
// 		GEvent.addListener(map, "dragend", function() {
// 		  alert("dragend");
// 		});
	}
	google.setOnLoadCallback(initialize);

	var poly = [] ; 
	var line ; 

	// Draw a circle on map around center (radius in km)
	// Modified by Jeremy Schneider based on http://maps.huge.info/dragcircle2.htm
	function drawCircle(map, center, radius, numPoints)
	{
		
		poly = [] ; 
		var lat = center.lat() ;
		var lng = center.lng() ;
		var d2r = Math.PI/180 ;                // degrees to radians
		var r2d = 180/Math.PI ;                // radians to degrees
		var Clat = (radius/6378) * r2d ;      //  using 6378 as earth's radius
		var Clng = Clat/Math.cos(lat*d2r);
		
		//Add each point in the circle
		for (var i = 0 ; i < numPoints ; i++)
		{
			var theta = Math.PI * (i / (numPoints / 2)) ;
			Cx = lng + (Clng * Math.cos(theta)) ;
			Cy = lat + (Clat * Math.sin(theta)) ;
			poly.push(new GLatLng(Cy,Cx)) ;
		}

		//Remove the old line if it exists
		if(line)
		{
			map.removeOverlay(line) ;
		}
		
		//Add the first point to complete the circle
		poly.push(poly[0]) ;

		//Create a line with teh points from poly, red, 3 pixels wide, 80% opaque
		//line = new GPolyline(poly,'#FF0000', 3, 0.8) ;
		line = new GPolygon(poly,'#FFFFFF', 3, 0.8, '#000000', 0.2);
		
		map.addOverlay(line) ;
	}

	// Download the data in data.xml and load it on the map. The format we
	// expect is:
	// <markers>
	//   <marker lat="37.441" lng="-122.141"/>
	//   <marker lat="37.322" lng="-121.213"/>
	// </markers>
	function setmarkers(){

		var bounds = map.getBounds();
		var southWest = bounds.getSouthWest();
		var northEast = bounds.getNorthEast();
		var lngSpan = northEast.lng() - southWest.lng();
		var latSpan = northEast.lat() - southWest.lat();
		//alert('lngSpan='+lngSpan+' latSpan='+latSpan);
		var center = map.getCenter();
		var url = 'data.php?lat=' + center.lat() + '&lng=' + center.lng() + '&country=' + map_country + '&lang=' + lang;
		//alert(url);
		GDownloadUrl(url, function(data, responseCode) {
		
			for (var i = 0; i < map_markers.length; i++) {
				map.removeOverlay(map_markers[i]);
				//map.removeOverlay(eval("marker_"+i));
			}

			drawCircle(map, map.getCenter(), 20, 40);
			
			map_markers = new Array();
			$('stores_container').update();

			var xml = GXml.parse(data);
			
			if (xml.documentElement) {
			
				markers = xml.documentElement.getElementsByTagName("marker");
				//alert("anz marker="+markers.length);
				//map.clearOverlays();
	
				
				for (var i = 0; i < markers.length; i++) {
					var store_address = markers[i].firstChild.data;
						
					var list_store_address = '<div id="marker_list_'+i+'" class="list_node">';
					list_store_address += store_address + "<div class='show_on_map'><a href='javascript:showOnMap(" + i + ");'>" + localized_show_on_map + "</a></div>";
					list_store_address += '</div>';
					
					$('stores_container').update($('stores_container').innerHTML + list_store_address);
										
					var bubble_store_address = '<div id="marker_bubble_'+i+'" class="bubble_node">';
					bubble_store_address += '<img class="schleich_logo" src="logo.gif" alt="Schleich" />' + store_address;
					bubble_store_address += '<div id="directions_wrapper_' + i + '" style="height:30px;"><a href="javascript:void(0);" onclick="javascript:$(\'directions_' + i + '\').show();$(\'directions_from_' + i + '\').addClassName(\'directions_bold\');">' + localized_get_directions + '</a><div id="directions_' + i + '" style="display:none;"><a id="directions_from_' + i + '" href="javascript:void(0);" onclick="javascript:directionsFrom(' + i + ');">' + localized_from_here +'</a> <a id="directions_to_' + i + '" href="javascript:void(0);" onclick="javascript:directionsTo(' + i + ');">' + localized_to_here + '</a><input type="text" id="directions_place_' + i + '" name="directions_place_' + i + '" value="" /><input type="submit" value="' + localized_go +'" onclick="javascript:showDirections(\'' + addslashes(markers[i].getAttribute("Strasse")) + ', ' + addslashes(markers[i].getAttribute("PLZ")) + ' ' + addslashes(markers[i].getAttribute("Ort")) + ', ' + addslashes(markers[i].getAttribute("Country")) + '\', $(\'directions_place_' + i + '\').getValue());" /></div></div>';
					bubble_store_address += '</div>';
									
					var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng")));
	
					map_markers[i] = createMarker(point, bubble_store_address);
					
					map.addOverlay(map_markers[i]);
				}
			}
			
		});
      }
      
      function showOnMap(id) {
      
      		show_on_map = true;
      		
		 	GEvent.removeListener(moveListener);
			var point = new GLatLng(parseFloat(markers[id].getAttribute("lat")), parseFloat(markers[id].getAttribute("lng")));
			map.setCenter(point, 12);
			//map_markers[id].openInfoWindowHtml();
			GEvent.trigger(map_markers[id], "click");

// 			moveListener = GEvent.addListener(map, "moveend", function() {
// 			  setmarkers();
// 			});
      }
      
      function createMarker(point, html) {
      	var marker = new GMarker(point, pin_normal);
      	GEvent.addListener(marker, "click", function() {
     		marker.openInfoWindowHtml(html);
      	});
      	return marker;
      }

      function showDirections(marker_address, address) {
		if(directions_from_to=='from') {
	      	var query = "from: " + address + " to: " + marker_address;
	    } else {
	      	var query = "from: " + marker_address + " to: " + address;
	    }
      	//alert(query);
		directions.load(query);
      }
      
	  function directionsFrom(id) {
		directions_from_to = 'from';
		$('directions_to_' + id).removeClassName('directions_bold');
		$('directions_from_' + id).addClassName('directions_bold');
// 		alert("directions_from: "+$('directions_from_' + id).classNames().inspect());
// 		alert("directions_to: "+$('directions_to_' + id).classNames().inspect());
// 		$('directions_to_' + id).setStyle({
// 		  'font-weight': 'normal'
// 		});
// 		$('directions_from_' + id).setStyle({
// 		  'font-weight': 'bold'
// 		});
	  }

	  function directionsTo(id) {
		directions_from_to = 'to';
		$('directions_to_' + id).addClassName('directions_bold');
		$('directions_from_' + id).removeClassName('directions_bold');
// 		alert("directions_from: "+$('directions_from_' + id).classNames().inspect());
// 		alert("directions_to: "+$('directions_to_' + id).classNames().inspect());
// 		$('directions_to_' + id).setStyle({
// 		  'font-weight': 'bold'
// 		});
// 		$('directions_from_' + id).setStyle({
// 		  'font-weight': 'normal'
// 		});
	  }
	  
      function showAddress() {
			var countryselect = document.getElementById("country");
			//alert(countryselect.value);
			var addressinput = document.getElementById("address");
			//alert(addressinput.value);

		 	GEvent.removeListener(moveListener);

			geocoder.getLocations(addressinput.value, addToMap);
		}
		
		function addToMap(response)
		{

			if (!response || response.Status.code != 200) {
				var addressinput = document.getElementById("address");
				alert("\"" + addressinput.value + "\" " + localized_not_found);
			} else {		   	  
				  // Retrieve the object
				  place = response.Placemark[0];
			
				  // Retrieve the latitude and longitude
				  point = new GLatLng(place.Point.coordinates[1],
									  place.Point.coordinates[0]);
			
				  // Center the map on this point
				  map.setCenter(point, 13);
				  
				  setmarkers();
	
				moveListener = GEvent.addListener(map, "moveend", function() {
				  setmarkers();
				});
			
				  // Create a marker
				  marker = new GMarker(point);
			
				  // Add the marker to map
				  map.addOverlay(marker);
			
				  // Add address information to marker
				  marker.openInfoWindowHtml(place.address + '<br />Latitude: ' + place.Point.coordinates[1]+ '<br />Longitude: ' + place.Point.coordinates[0]);
			}
		}

function addslashes(str) {
str=str.replace(/\\/g,'\\\\');
str=str.replace(/\'/g,'\\\'');
str=str.replace(/\"/g,'\\"');
str=str.replace(/\0/g,'\\0');
return str;
}
function stripslashes(str) {
str=str.replace(/\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\0/g,'\0');
str=str.replace(/\\\\/g,'\\');
return str;
}


//    		function changeCountry() {
// 
// 			if(this.country.value=='**') {
// 
// 			  point = new GLatLng(-4.215, 14.0625);
// 		
// 			  // Center the map on this point
// 			  map.setCenter(point, 1);
// 
// 			} else {
// 
// 				map_country = this.country.value;
// 				geocoder.getLocations(capital[this.country.value] + ',' + this.country.value, showCountry);
// 	
// 			}
// 
// 			new Ajax.Updater('online_links_container', 'getCountryLinks.php', {
// 			  parameters: { links_country: this.country.value }
// 			});
// 			
//    		}
// 
// 		function showCountry(response)
// 		   {
// 		   	  
// 			  // Retrieve the object
// 			  place = response.Placemark[0];
// 		
// 			  // Retrieve the latitude and longitude
// 			  point = new GLatLng(place.Point.coordinates[1],
// 								  place.Point.coordinates[0]);
// 		
// 			  // Center the map on this point
// 			  map.setCenter(point, 10);
// 			  
// 			setmarkers();
// 			  
// 		   }
// 
// 					
// 					
// 					
