//
if (GBrowserIsCompatible()) {
	
	var iconLodge = new GIcon(); 
	iconLodge.image = 'map_icons/lodge1.png';
	iconLodge.shadow = 'map_icons/shadow.png';
	iconLodge.iconSize = new GSize(42, 62);
	iconLodge.shadowSize = new GSize(104, 58);
	iconLodge.iconAnchor = new GPoint(15, 62);
	iconLodge.infoWindowAnchor = new GPoint(5, 1);
	
	var iconLodge2 = new GIcon(); 
	iconLodge2.image = 'map_icons/lodge2.png';
	iconLodge2.shadow = 'map_icons/shadow.png';
	iconLodge2.iconSize = new GSize(21, 31);
	iconLodge2.shadowSize = new GSize(52, 29);
	iconLodge2.iconAnchor = new GPoint(7, 20);
	iconLodge2.infoWindowAnchor = new GPoint(5, 1);
	
	var iconFood = new GIcon(); 
	iconFood.image = 'map_icons/food.png';
	iconFood.shadow = 'map_icons/shadow.png';
	iconFood.iconSize = new GSize(21, 31);
	iconFood.shadowSize = new GSize(52, 29);
	iconFood.iconAnchor = new GPoint(7, 20);
	iconFood.infoWindowAnchor = new GPoint(5, 1);
	
	var iconFish = new GIcon(); 
	iconFish.image = 'map_icons/fishing.png';
	iconFish.shadow = 'map_icons/shadow.png';
	iconFish.iconSize = new GSize(21, 31);
	iconFish.shadowSize = new GSize(52, 29);
	iconFish.iconAnchor = new GPoint(7, 20);
	iconFish.infoWindowAnchor = new GPoint(5, 1);
	
	var iconSki = new GIcon(); 
	iconSki.image = 'map_icons/sski.png';
	iconSki.shadow = 'map_icons/shadow.png';
	iconSki.iconSize = new GSize(21, 31);
	iconSki.shadowSize = new GSize(52, 29);
	iconSki.iconAnchor = new GPoint(7, 20);
	iconSki.infoWindowAnchor = new GPoint(5, 1);
	
	var iconHike = new GIcon(); 
	iconHike.image = 'map_icons/hike.png';
	iconHike.shadow = 'map_icons/shadow.png';
	iconHike.iconSize = new GSize(21, 31);
	iconHike.shadowSize = new GSize(52, 29);
	iconHike.iconAnchor = new GPoint(7, 20);
	iconHike.infoWindowAnchor = new GPoint(5, 1);

	var iconHorse = new GIcon(); 
	iconHorse.image = 'map_icons/horse.png';
	iconHorse.shadow = 'map_icons/shadow.png';
	iconHorse.iconSize = new GSize(21, 31);
	iconHorse.shadowSize = new GSize(52, 29);
	iconHorse.iconAnchor = new GPoint(6, 20);
	iconHorse.infoWindowAnchor = new GPoint(5, 1);

	var iconOther = new GIcon(); 
	iconOther.image = 'map_icons/horse.png';
	iconOther.shadow = 'map_icons/shadow.png';
	iconOther.iconSize = new GSize(21, 31);
	iconOther.shadowSize = new GSize(52, 29);
	iconOther.iconAnchor = new GPoint(6, 20);
	iconOther.infoWindowAnchor = new GPoint(5, 1);

	var iconPicture = new GIcon(); 
	iconPicture.image = 'map_icons/scenic.png';
	iconPicture.shadow = 'map_icons/shadow.png';
	iconPicture.iconSize = new GSize(21, 31);
	iconPicture.shadowSize = new GSize(52, 29);
	iconPicture.iconAnchor = new GPoint(6, 20);
	iconPicture.infoWindowAnchor = new GPoint(5, 1);
	
	var gicons = [];
	gicons["gglodge"] = iconLodge;
	gicons["wmlodge"] = iconLodge2;
	gicons["restaurant"] = iconFood;
	gicons["fishing"] = iconFish;
	gicons["skiing"] = iconSki;
	gicons["hiking"] = iconHike;
	gicons["horse"] = iconHorse;
	gicons["other"] = iconOther;
	gicons["picture"] = iconPicture;
	
	var map;
	var gmarkers = [];

	// A function to create the marker and set up the event window
	function createMarker(point,name,html,category) {
    	var marker = new GMarker(point,{icon:gicons[category],title:name});
        // === Store the category and name info as a marker properties ===
        marker.mycategory = category;                                 
        marker.myname = name;
		
        GEvent.addListener(marker, "click", function() {
			map.savePosition();
			marker.openInfoWindowHtml(html);
			map.recenterOrPanToLatLng(point)
        });
        GEvent.addListener(marker, "infowindowclose", function() {
		  map.returnToSavedPosition();
        });
		
        gmarkers.push(marker);
    	return marker;
	}

	function changeBodyClass(from, to) {
		document.body.className = document.body.className.replace(from, to);
		return false;
	}

	function init()	{
		var centerLatitude = 34.015975;
		var centerLongitude = -109.455038;
		var startZoom = 11;
		var description = 'Greer Glen Lodge';
		
		changeBodyClass('loading', 'standby');
		
		// create the map
		map = new GMap2(document.getElementById("map"), G_NORMAL_MAP);
		var location = new GLatLng(centerLatitude, centerLongitude);
		map.setCenter(location, startZoom);
		// CONTROLS
		//map.setMapType(G_HYBRID_MAP);	// turn off for speed
		map.addMapType(G_PHYSICAL_MAP);
		map.setMapType(G_PHYSICAL_MAP);	// turn off for speed
		map.addControl(new GLargeMapControl() );
		map.addControl(new GScaleControl() );
		map.addControl(new GMapTypeControl() );
		map.enableContinuousZoom();
		map.enableScrollWheelZoom()
		//map.addControl(new TextButtons());
		//map.addOverlay(new LatLonGraticule()); // turn off for speed.

		var bounds = new GLatLngBounds();

		// Read the data
		GDownloadUrl("domxml.php", function(doc) {
			var xmlDoc = GXml.parse(doc);
			var markers = xmlDoc.documentElement.getElementsByTagName("marker");
		//alert("DEBUG: xml loaded");
	
			// CYCLE through XML makers
			for (var i = 0; i < markers.length; i++) {
				// obtain the attribues of each marker
				var name = markers[i].getAttribute("name");
				var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
										parseFloat(markers[i].getAttribute("lng")));
				var address = markers[i].getAttribute("address");
				var phone = markers[i].getAttribute("phone");
				var category = markers[i].getAttribute("type");
				var image = markers[i].getAttribute("image");
				
				if ( image == '')	{
					//no image yo
					//var image;
				}else if ( address == '')	{
//					alert("DEBUG: address is NULL");
					address = '&nbsp;';
				}else {
				}
					//get the image size
					var Size = new Array();
					Size = markers[i].getAttribute("image").split(':');
					var image = Size[0];
					var img_x = Size[1];
					var img_y = Size[2];
					
					address += "<br /><img src='map_icons/"+image+"' width="+img_x+"px height="+img_y+"px alt='"+name+"' /> ";
				//}
				
				var html = "<b>"+name+"</b>"+
							"<p>"+phone+"<br />"+address+"</p>";
				
				
				// create the marker
				var marker = createMarker(point,name,html,category);
				map.addOverlay(marker);
				bounds.extend(point);
				map.setZoom(map.getBoundsZoomLevel(bounds));
				map.setCenter(bounds.getCenter());
				map.savePosition();
			}
			//alert("DEBUG: markers fin");
			// == show or hide the categories initially ==
			//show("all");
			//show("lodge");
			//hide("restaurant");
			//hide("fishing");
			//hide("hiking");
			//hide("skiing");
			//hide("other");
			// == create the initial sidebar ==
			//makeSidebar();
		});
	} // end init
}else {
	alert("Sorry, the Google Maps API is not compatible with this browser");
}

// STARTUP AND SHUTDOWN
window.onload = init;
window.onunload = GUnload;

//
