var map;

function initialize() {
  if (GBrowserIsCompatible()) {
    // Create map
    map = new GMap(document.getElementById("mapCanvas"));
    map.addControl(new GScaleControl());
    map.addControl(new GLargeMapControl());
    // Create the map type and add it to the map
    var campusMap = getCampusMapType();
    map.addMapType(campusMap);
		map.removeMapType(G_SATELLITE_MAP);
		map.removeMapType(G_NORMAL_MAP);
		// G_NORMAL_MAP.getName = function() {return "Roads"} 
		map.removeMapType(G_HYBRID_MAP);		
		map.addMapType(G_SATELLITE_MAP);    
		// map.addMapType(G_PHYSICAL_MAP); // There's no imagery at the default campus map zoom level.
    var mapTypeControl = new GMapTypeControl();
    map.addControl(mapTypeControl);
    
    // Limit zoom
		var mapTypes = map.getMapTypes();
		for (var i = 0; i < mapTypes.length; i++) {
		  mapTypes[i].getMaximumResolution = function() {return 18;}
		}
		
    map.setCenter(new GLatLng(42.25565, -72.57358), 16, campusMap);
  }
}

function showLocation(latitude, longitude, html) {
	 var latlng = new GLatLng(latitude, longitude);
   var marker = new GMarker(latlng) 
   // var marker = new GMarker(latlng, {title:'title shown on hover'})   
   map.clearOverlays();
   map.addOverlay(marker);   
	 marker.bindInfoWindowHtml(html);
	 map.setZoom(17);
	 map.setCenter(marker.getLatLng());
}

function getCampusMapType() {
    // Establish copyright on our tiles
    var copyright = new GCopyright(1,  // index
                                   new GLatLngBounds(new GLatLng(-90, -180), new GLatLng(90, 180)),
                                   12, // minZoom
                                   "&copy;2009 Mount Holyoke College");
    var copyrightCollection = new GCopyrightCollection('Campus Map');
    copyrightCollection.addCopyright(copyright);

    // Define tile layers
    var tilelayers = G_NORMAL_MAP.getTileLayers().concat(new GTileLayer(copyrightCollection , 12, 18));
    tilelayers[1].getTileUrl = customGetTileUrl;

		return new GMapType(tilelayers, new GMercatorProjection(19), "Campus", {errorMessage:"No data available"});
}

// Define the tile retrieval function which maps to our tile names
function customGetTileUrl(a,b) {
  if ((b==18 && a.x >= 78220 && a.x <= 78232 && a.y >= 97056 && a.y <= 97066) ||
  	  (b==17 && a.x >= 39110 && a.x <= 39116 && a.y >= 48526 && a.y <= 48533) ||
      (b==16 && a.x >= 19555 && a.x <= 19558 && a.y >= 24263 && a.y <= 24266) ||
      (b==15 && a.x >=  9777 && a.x <=  9779 && a.y >= 12131 && a.y <= 12133) ||
      (b==14 && a.x >=  4888 && a.x <=  4889 && a.y >=  6065 && a.y <=  6066) ||
      (b==13 && a.x ==  2444                 && a.y >=  3032 && a.y <=  3033) ||
      (b==12 && a.x ==  1222                 && a.y ==  1516                )) {
    return "/campusmap/tiles/" + b + "_" + a.x + "_" + a.y + ".gif";
  } else {
    return G_NORMAL_MAP.getTileLayers()[0].getTileUrl(a,b);
  }
}