       
    // ALSAP Map scripts by Tim Kelley 8/1/05, Google Maps API is used
    // Upgrade to be Google Maps API v.2 compliant, Tim Kelley 11/14/06

	// Globals
	var map;
	var request;
	
 	var regionPoints = new Array(20);
 	var regionKeys = new Array(20);
 	var regionZooms = new Array(20);
 	
 	var sitePoints = new Array(150);
 	var siteKeys = new Array(150);
	var siteNames = new Array(150);
	var siteTimes = new Array(150);
	var siteTypes = new Array(150);
	var siteZooms = new Array(150);
	var siteMarkers = new Array(150);
	
	var redicon = new GIcon();
	var blueicon = new GIcon();
	var yellowicon = new GIcon();
	var purpleicon = new GIcon();
	

	// Function that creates site type marker icons
	function initIcons() {
	
		// Downhill site icon
		redicon.image = "http://www.alsap.org/images/mm_20_red.png";
		redicon.shadow = "http://www.alsap.org/images/mm_20_shadow.png";
		redicon.iconSize = new GSize(12, 20);
		redicon.shadowSize = new GSize(22, 20);
		redicon.iconAnchor = new GPoint(6, 20);
		redicon.infoWindowAnchor = new GPoint(5, 1);
	
		// Jump site icon
		blueicon.image = "http://www.alsap.org/images/mm_20_blue.png";
		blueicon.shadow = "http://www.alsap.org/images/mm_20_shadow.png";
		blueicon.iconSize = new GSize(12, 20);
		blueicon.shadowSize = new GSize(22, 20);
		blueicon.iconAnchor = new GPoint(6, 20);
		blueicon.infoWindowAnchor = new GPoint(5, 1);
	
		// Cross country site icon
		yellowicon.image = "http://www.alsap.org/images/mm_20_yellow.png";
		yellowicon.shadow = "http://www.alsap.org/images/mm_20_shadow.png";
		yellowicon.iconSize = new GSize(12, 20);
		yellowicon.shadowSize = new GSize(22, 20);
		yellowicon.iconAnchor = new GPoint(6, 20);
		yellowicon.infoWindowAnchor = new GPoint(5, 1);
	
		// Combination site icon
		purpleicon.image = "http://www.alsap.org/images/mm_20_purple.png";
		purpleicon.shadow = "http://www.alsap.org/images/mm_20_shadow.png";
		purpleicon.iconSize = new GSize(12, 20);
		purpleicon.shadowSize = new GSize(22, 20);
		purpleicon.iconAnchor = new GPoint(6, 20);
		purpleicon.infoWindowAnchor = new GPoint(5, 1);
	}
	
	// Function to call up a regional map
	function gotoMap(regionName) {
	
		for (var i=0; i<20; i++) {
			if (regionKeys[i] == regionName) {
				clearSiteInfo();
    			//v2//map.centerAndZoom(regionPoints[i], regionZooms[i]);
    			map.setCenter(regionPoints[i], regionZooms[i], G_HYBRID_MAP);
				break;
			}
		}
		if (i == 20) {
			alert("The map region named >" + regionName + "< is not defined.");
		}
	}
	
    // Creates a marker whose info window displays the given number
	function createMarker(point, index) {
	
		if (siteTypes[index] == 'D') {
   			//v2//var marker = new GxMarker(point, redicon);
   			var marker = new GxMarker(point, redicon, siteNames[index]);
   		}
   		else if (siteTypes[index] == 'J') {
   			//v2//var marker = new GxMarker(point, blueicon);
   			var marker = new GxMarker(point, blueicon, siteNames[index]);
   		}
   		else if (siteTypes[index] == 'X') {
   			//v2//var marker = new GxMarker(point, yellowicon);
   			var marker = new GxMarker(point, yellowicon, siteNames[index]);
   		}
		else {
   			//v2//var marker = new GxMarker(point, purpleicon);
   			var marker = new GxMarker(point, purpleicon, siteNames[index]);
		}
   		marker.myid = index;
   		siteMarkers[index] = marker;
 
// 		var sz = parseInt(siteZooms[index]);
//  		var mz = parseInt(map.getZoomLevel());
//		if (sz < mz) {
//			marker.setTooltip("Click to Zoom In");
//		}
//		else {
			//v2//marker.setTooltip(siteNames[index]);
//v2//			marker.title(siteNames[index]);
//		}
 		map.addOverlay(marker);
	};
	
	// Set marker tool tips, based on map zoom level
	function reSetMarkerToolTips() {
		for (var i=0; i<150; i++) {
			if (siteMarkers[i]) {
			
			  	var sz = parseInt(siteZooms[i]);
  				var mz = parseInt(map.getZoomLevel());
				if (sz < mz) {
					//v2//siteMarkers[i].tooltip.contents = "Click to Zoom In";
					siteMarkers[i].title = "Click to Zoom In";
				}
				else {
					//v2//siteMarkers[i].tooltip.contents = siteNames[i];
					siteMarkers[i].title = siteNames[i];
				}
			}
		}
	}
	
	// Writes site info to a document elements
	function showSiteInfo(marker){
		var index = marker.myid; 
 		var siteKey = siteKeys[index];
 		var siteURL = "http://www.alsap.org/" + siteKey + "/" + siteKey + ".htm";
 		var htmlString = "<p>" + siteNames[index] + " - Active: " + siteTimes[index] + "    .</p>" +
                   "<p><a target=\"_blank\" href=" + siteURL + ">Go to " + siteNames[index]+ " Web Page.</a></p>";
                   
 		marker.openInfoWindowHtml(htmlString);
                                     
//		document.getElementById('siteName').innerHTML=siteNames[index] + " ";
//		document.getElementById('siteTime').innerHTML=" - " + siteTimes[index];
 	};

	
	// Writes site info to a document elements
	function clearSiteInfo(){ 
 		
 		map.closeInfoWindow();
 		
 		document.getElementById('siteName').innerHTML="Tip: Click markers to select lost ski site, or zoom into clusters of sites.";
 	};

    // Function to load XML data into global arrays
    function loadALSAPxml() {
    // Download the data in alsap.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>
		request.open("GET", "alsap.xml", true);
		request.onreadystatechange = function() {
			if (request.readyState == 4) {
    			var xmlDoc = request.responseXML;
    			var regions = xmlDoc.documentElement.getElementsByTagName("region");
    			for (var i = 0; i < regions.length; i++) {
     				//v2//var point = new GPoint(parseFloat(regions[i].getAttribute("lng")),
                    //v2//         	   	   parseFloat(regions[i].getAttribute("lat")));
     				var point = new GLatLng(parseFloat(regions[i].getAttribute("lat")),
                             	   	   parseFloat(regions[i].getAttribute("lng")));
      				regionPoints[i] = point;
      				regionKeys[i] = regions[i].getAttribute("key");
      				regionZooms[i] = regions[i].getAttribute("zoom");
      				regionZooms[i] = 17 - regionZooms[i];            
      			}
      				
    			var xmlDoc = request.responseXML;
    			var markers = xmlDoc.documentElement.getElementsByTagName("marker");
    			for (var i = 0; i < markers.length; i++) {
     				//v2//var point = new GPoint(parseFloat(markers[i].getAttribute("lng")),
                    //v2//         	   	   parseFloat(markers[i].getAttribute("lat")));
     				var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                             	   	   parseFloat(markers[i].getAttribute("lng")));
 
     				sitePoints[i] = point;
     				siteKeys[i] = markers[i].getAttribute("key");
     				siteZooms[i] = markers[i].getAttribute("zoom");
     				siteZooms[i] = 17 - siteZooms[i];	
      				siteNames[i] = markers[i].getAttribute("siteName");
      				siteTimes[i] = markers[i].getAttribute("siteTime");
      				siteTypes[i] = markers[i].getAttribute("siteType");
       				createMarker(point, i);
 				}
	  		}
		}
	}     


	function onLoad() {
	
		initIcons();
		request = GXmlHttp.create();
		loadALSAPxml();
  	    map = new GMap2(document.getElementById("map"));
 		clearSiteInfo();
   		//v2//map.setMapType(G_HYBRID_MAP);
   	 	map.addControl(new GLargeMapControl());
 		map.addControl(new GMapTypeControl());
        //map.addControl(new GOverviewMapControl(new GSize(200,160))); 
  	    //v2//var point = new GPoint();
    	//v2//map.centerAndZoom(point, 13);
 
      	map.setCenter(new GLatLng(63.8000, -159.4995), 4, G_HYBRID_MAP);
       
 		// Listens for clicks on markers
		GEvent.addListener(map, 'click', function(overlay, point) {
  			if (overlay) {
  				var i = overlay.myid;
  				var sz = parseInt(siteZooms[i]);
  				var mz = parseInt(map.getZoom());
  				//alert('i: ' + i + ' sz: ' + sz + ' mz: ' + mz);
				if (sz > mz) {
 					clearSiteInfo();
//   					reSetMarkerToolTips();
    				//v2//map.centerAndZoom(sitePoints[i], siteZooms[i]);
    				map.setCenter(sitePoints[i], siteZooms[i], G_HYBRID_MAP);
				}
  				else {
 					showSiteInfo(overlay);
 				}
  			}
  			else
  			{
  				clearSiteInfo();
//   				reSetMarkerToolTips();
			}
		});
		request.send(null);
	}
