if (GBrowserIsCompatible()) {
	var map;
	var geo;
	var gicons = [];
	var country = '';
	var side_bar_html = "";
	var gmarkers = [];
	var mapname;
	
	var tooltip;
	var updateMap;
	var baseIcon;
	var detailLayer;

	function load(mname) {

		lat = gup('lat', 40);
		lng = gup('lng', -40);
		zoom = parseInt(gup('zoom', 3));
		country = gup('country', '');
		mapname = mname;

		map = new GMap2(document.getElementById("map"));
		map.addMapType(G_PHYSICAL_MAP);
		
		//detailLayer = new GTileLayer(new GCopyrightCollection(''));
		//detailLayer.getTileUrl = function(tile, zoom) {
		//	var tileURL = "feeds/station_lines.php?x="+tile.x+"&y="+tile.y+"&zoom="+zoom;
		//	GLog.writeUrl(tileURL);
		//	return tileURL;
		//};
		//detailLayer.isPng = function() {
		//	return false;
		//}
   
      	var mt = map.getMapTypes();
      	for (var i=0; i<mt.length; i++) {
        	mt[i].getMinimumResolution = function() {return 1;}
        	mt[i].getMaximumResolution = function() {return 17;}
			//detailMapLayers = mt[i].getTileLayers();
			//detailMapLayers.push(detailLayer);
      	}
      	
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());		
        var mini = new GOverviewMapControl(new GSize(100,80));
        map.addControl(mini); 		
        
		map.setCenter(new GLatLng(lat, lng), zoom);
		map.setMapType(G_PHYSICAL_MAP);

      	geo = new GClientGeocoder(); 
      	
		tooltip = document.createElement("div");
		map.getPane(G_MAP_FLOAT_PANE).appendChild(tooltip);
		tooltip.style.visibility="hidden";    

		radSizes = [30,38,46,58,68,80];
		for (n = 0; n < radSizes.length; n++) {
			radius = radSizes[ n ];

			gicons['circle' + radius] = new GIcon();
			gicons['circle' + radius].image = '../gfx/markers/categories/circle-' + radius + '.png';
			gicons['circle' + radius].hoverImage = '../gfx/markers/categories/circle-' + radius + '_hover.png';
			gicons['circle' + radius].iconAnchor = new GPoint(radius / 2, radius / 2);
			gicons['circle' + radius].iconSize = new GSize(radius, radius);
		}

		baseIcon = new GIcon();
		baseIcon.shadow = "gfx/markers/categories/shadow.png";
		baseIcon.iconSize   = new GSize(19, 24);
		baseIcon.shadowSize = new GSize(35, 23);
		baseIcon.iconAnchor = new GPoint(6, 24);
		baseIcon.infoWindowAnchor = new GPoint(2, 2);   

		//create an icon for the pins
		var icons = ["hotel","airport", "cluster"]; 

		for (var i in icons) {
			var iconName = "../gfx/markers/categories/" + icons[i] + ".png";
			gicons[icons[i]]  = new GIcon(baseIcon, iconName);
		}		

		updateMarkers();
		//GEvent.addListener(map,'zoomend',function() {
			//updateMarkers();
		//});
		GEvent.addListener(map,'moveend',function() {
			updateMarkers();
		}); 
		GEvent.addListener(map,'infowindowclose',function() {
			closeInfoWindow();
		}); 		
	}
	
	function updateMarkers() {
		if (!map.getInfoWindow().isHidden()) {
			updateMap = true;
		}
		else {
			updateMap = false;
			//create the boundary for the data
			var bounds = map.getBounds();
			var southWest = bounds.getSouthWest();
			var northEast = bounds.getNorthEast();
			var getVars = 'ne=' + northEast.toUrlValue()
			+ '&sw=' + southWest.toUrlValue() + "&zoom=" + map.getZoom() + "&country=" + country;
			//GLog.writeUrl('feeds/' + mapname + '_markers.php?'+getVars);
			var request = GXmlHttp.create();
			request.open('GET', '../feeds/' + mapname + '_markers.php?'+getVars, true);
			request.onreadystatechange = function() {
				if (request.readyState == 4) {
					var jscript = request.responseText;
					var points;
	 
					eval(jscript);	
					map.clearOverlays();

					var j = 0;
					for (i in points) {
						var latlng = new GLatLng(points[i].lat,points[i].lng);
						var marker = createMarker(latlng,points[i]);
						map.addOverlay(marker);
						
						gmarkers[j++] = marker;					
					}	
				}
			}

			request.send(null);
		}
	}

	function createMarker(latlng, point) {
		if (!gicons[point.icontype]) {
			var iconName = "../gfx/markers/categories/" + point.icontype + ".png";
			gicons[point.icontype]  = new GIcon(baseIcon, iconName);			
		}
		var marker;                                              ;
		
		//create the marker with the appropriate icon
		if (point.icontype.indexOf('circle') >= 0) {
			opts = { 
			  icon: gicons[point.icontype],
			  clickable: true,
			  labelText: point.count,
			  labelOffset: new GSize(-16, -8),          
			  title: point.title
			};
			marker = new LabeledMarker(latlng, opts);

			GEvent.addListener(marker, 'click', function() {
				map.closeInfoWindow();
				map.setCenter(latlng, map.getZoom() + 2);	       
			});

			GEvent.addListener(marker, 'mouseover', function() {
				this.setImage( this.getIcon().hoverImage );
			});
			GEvent.addListener(marker, 'mouseout', function() {
				this.setImage( this.getIcon().image );
			});        
		}
		else {
			marker = new GMarker(latlng, gicons[point.icontype]);
			
			GEvent.addListener(marker, 'click', function() {			
				var getVars = 'code=' + point.code;
				//GLog.writeUrl('feeds/click_marker.php?'+getVars);	
				var request = GXmlHttp.create();
				request.open('GET', '../feeds/click_marker.php?'+getVars, true);
				request.onreadystatechange = function() {
					if (request.readyState == 4) {
						var jscript = request.responseText;
						var markerHTML;
						eval(jscript);
						marker.openInfoWindowHtml(markerHTML);
						setCookie("marker", point.code, 1);
					}
				}

				request.send(null);				
			});			
		}
		
		
		marker.tooltip = '<div class="tooltip"><nobr>'+point.title+'</nobr></div>';	

		GEvent.addListener(marker, "mouseover", function() {
			showTooltip(marker);
		});        
		GEvent.addListener(marker, "mouseout", function() {
			tooltip.style.visibility="hidden"
		});		

		return marker;
	}

	function closeInfoWindow() {
		if (updateMap) {
			updateMarkers();
		}
	}

	function clickMarker(n) {
		GEvent.trigger(gmarkers[n], "click");
	}

	function showTooltip(marker) {
		tooltip.innerHTML = marker.tooltip;
		var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint(0,0),true),map.getZoom());
		var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
		var anchor=marker.getIcon().iconAnchor;
		var width=marker.getIcon().iconSize.width;
		var height=tooltip.clientHeight;
		var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - point.x - anchor.x + width, offset.y - point.y -anchor.y -height)); 
		pos.apply(tooltip);
		tooltip.style.visibility="visible";
	}  

	function zoomMap(lat, lng, zoom) {
		map.closeInfoWindow();
		map.setCenter(new GLatLng(lat, lng), zoom);
	}

	function gup(name, value) {
		name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		var regexS = "[\\?&]"+name+"=([^&#]*)";
		var regex = new RegExp( regexS );
		var results = regex.exec(window.location.href);
		if( results == null )
			return value;
		else
			return results[1];
	}
	
	function searchLocation() {
		var search = document.getElementById("query").value;
		setLocation(search);
	}
	
    function setLocation(search) {      
        geo.getLocations(search, function (result) {
            if (result.Status.code == G_GEO_SUCCESS) {
                var p = result.Placemark[0].Point.coordinates;
                var point = new GLatLng(p[1],p[0]);
        		map.setCenter(point,11); 
            }
          }
        );
    }
    
	function setCookie(c_name,value,expiredays) {
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+expiredays);
		document.cookie=c_name+ "=" +escape(value)+
		((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
	}    
    
}
else {
  alert("Sorry, the Google Maps API is not compatible with this browser");
}