// JavaScript Document
	var map;
	var markerflag;
	var fullMap = true;
	var mui;
	var lmapcontrol;
	var mtypecontrol;
	var activetown = 'capecod';
	var spm_name ='a';

	function initializeMap() {
		if (GBrowserIsCompatible()) {
			map = new GMap2(document.getElementById("map_canvas"));
			map.setCenter(new GLatLng(41.69605,-70.36139), 9);
			lmapcontrol = new GLargeMapControl3D();
			map.addControl(lmapcontrol);
			mtypecontrol = new GMapTypeControl();
			map.addControl(mtypecontrol);
			map.savePosition();
			mui = new GMapUIOptions(468,468);
			setupUI(); 
			// GMG  testing
			getActiveTown();
		}
	}
  
// this section for new individual maps 4/12/10  
	function initializeSingleMap(proptown, proplat, proplong, propcat, proptitle) {
		var el = document.getElementById("map_canvas");
		if (el != null){
			el.style.display = "block";
			el.style.height = 468 + "px";
		}
		if (GBrowserIsCompatible()) {
			map = new GMap2(el);
			map.setCenter(new GLatLng(41.69605,-70.36139), 9);
			lmapcontrol = new GLargeMapControl3D();
			map.addControl(lmapcontrol);
			mtypecontrol = new GMapTypeControl();
			map.addControl(mtypecontrol);
			map.savePosition();
			mui = new GMapUIOptions(468,468);
			setupUI(); 
			activetown = proptown;
			getActiveTown();
			showPropMarker(proplat, proplong, proptitle);
			show(propcat);
		}
	}


	function hideMap() {
		var el = document.getElementById("map_canvas");
		if (el != null){ 
			// el.style.height = 60 + "px";
			el.style.display = "none";
			setupUI();
			map.removeControl(lmapcontrol);
			map.removeControl(mtypecontrol);
		}
		map.checkResize();
	}


	function showPropMarker (spm_lat, spm_long, spm_title) {
		var spm_i = 0; //legacy
		var spm_latlng = new GLatLng(parseFloat(spm_lat),parseFloat(spm_long));
		spm_name = spm_title;
		var spm_address = "";
		var spm_town = "";
		var spm_action1 = "";
		var spm_action2 = "";
		spm_markerflag = "white";
		map.addOverlay(createSingleMarker(spm_latlng, spm_i, spm_name, spm_address, spm_town, spm_action1, spm_action2, spm_markerflag));
	}
		
	function importanceOrder (marker,b) {	
		return GOverlay.getZIndex(marker.getPoint().lat()) + marker.importance*1000000;
	}

	// Creates a basic marker of the specified color
	function createSingleMarker(point, index, name, address, town, action1, action2, markerflag) {
		// Create a colored icon for this point using our icon class
		var colorIcon = new GIcon(G_DEFAULT_ICON);
		colorIcon.image = "http://www.capecodvisitorsdirectory.com/images/" + markerflag + "marker.png";
		markerOptions = {icon:colorIcon, zIndexProcess:importanceOrder};
		var marker = new GMarker(point, markerOptions);
		marker.importance = 8;
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml('<b>' + name + '</b><br>' + address + '<br>' + town + '<br><a href=http://' + action2 + ' target=new >' + action1 + '</a>');
		});
		return marker;
	}
	
// END this section for new individual maps 4/12/10    
  

	function getActiveTown() {
		var temptitle;
		if (activetown != 'capecod') {
			temptitle = activetown;
		} else {
			temptitle = document.title;       // get the page title
			temptitle = temptitle.split(" ",1);    // extract the first word
		}
		if (temptitle == "Falmouth"){
			falmouth();
		} else if (temptitle == "Bourne"){
			bourne();
		} else if (temptitle == "Barnstable"){
			barnstable();
		} else if (temptitle == "Brewster"){
			brewster();
		} else if (temptitle == "Chatham"){
			chatham();
		} else if (temptitle == "Dennis"){
			dennis();
		} else if (temptitle == "Eastham"){
			eastham();
		} else if (temptitle == "Harwich"){
			harwich();
		} else if (temptitle == "Martha's"){
			marthasvineyard();
		} else if (temptitle == "Martha\'s Vineyard"){
			marthasvineyard();
		} else if (temptitle == "Mashpee"){
			mashpee();
		} else if (temptitle == "Nantucket"){
			nantucket();
		} else if (temptitle == "Orleans"){
			orleans();
		} else if (temptitle == "Provincetown"){
			provincetown();
		} else if (temptitle == "Sandwich"){
			sandwich();
		} else if (temptitle == "Truro"){
			truro();
		} else if (temptitle == "Wellfleet"){
			wellfleet();
		} else if (temptitle == "Yarmouth"){
			yarmouth();
		} else {
			// do nothing - leave cape cod map - this should never happen
		}
		// document.write(temptitle);
	}


	function dennis() {
		map.setZoom(12);		// zoom must be first
		map.panTo(new GLatLng(41.7082756,-70.1781391));
		activetown = 'dennis';
	}

	function falmouth() {
		map.setZoom(11);		// zoom must be first
		map.panTo(new GLatLng(41.5879269,-70.5930511));
		activetown = 'falmouth';
	}
 
 	function bourne() {
		map.setZoom(11);		// zoom must be first
		map.panTo(new GLatLng(41.721922,-70.538055));
		activetown = 'bourne';
	}
	
	function mashpee() {
		map.setZoom(11);		// zoom must be first
		map.panTo(new GLatLng(41.648906,-70.481987));
		activetown = 'mashpee';
	}
	
	function sandwich() {
		map.setZoom(11);		// zoom must be first
		map.panTo(new GLatLng(41.757724,-70.500139));
		activetown = 'sandwich';
	}

	function barnstable() {
		map.setZoom(11);		// zoom must be first
		map.panTo(new GLatLng(41.6918497,-70.3360691));
		activetown = 'barnstable';
	}

	function yarmouth() {
		map.setZoom(12);		// zoom must be first
		map.panTo(new GLatLng(41.688339,-70.230451));
		activetown = 'yarmouth';
	}

	function brewster() {
		map.setZoom(12);		// zoom must be first
		map.panTo(new GLatLng(41.760956,-70.082934));
		activetown = 'brewster';
	}

	function chatham() {
		map.setZoom(12);		// zoom must be first
		map.panTo(new GLatLng(41.67714,-69.9642));
		activetown = 'chatham';
	}

	function harwich() {
		map.setZoom(12);		// zoom must be first
		map.panTo(new GLatLng(41.6716498,-70.0622231));
		activetown = 'harwich';
	}		

	function orleans() {
		map.setZoom(12);		// zoom must be first
		map.panTo(new GLatLng(41.792795,-70.000485));
		activetown = 'orleans';
	}

	function eastham() {
		map.setZoom(12);		// zoom must be first
		map.panTo(new GLatLng(41.852402,-69.973726));
		activetown = 'eastham';
	}

	function provincetown() {
		map.setZoom(12);		// zoom must be first
		map.panTo(new GLatLng(42.05252,-70.186957));
		activetown = 'provincetown';
	}

	function truro() {
		map.setZoom(12);		// zoom must be first
		map.panTo(new GLatLng(42.0106926,-70.0814153));
		activetown = 'truro';
	}

	function wellfleet() {
		map.setZoom(12);		// zoom must be first
		map.panTo(new GLatLng(41.917405,-70.027136));
		activetown = 'wellfleet';
	}

	function nantucket() {
		map.setZoom(10);		// zoom must be first
		map.panTo(new GLatLng(41.283208,-70.09923));
		activetown = 'nantucket';
	}

	function marthasvineyard() {
		map.setZoom(10);		// zoom must be first
		map.panTo(new GLatLng(41.3804981,-70.645473));
		activetown = 'marthasvineyard';
	}


	function setupUI() {
		mui.maptypes.normal = true;   
		mui.maptypes.satellite = true;
		mui.maptypes.hybrid = false;
		mui.maptypes.physical = true;
		mui.zoom.scrollwheel = false;
		mui.zoom.doubleclick = true;
		mui.controls.largemapcontrol3d = false;
		mui.controls.smallzoomcontrol3d = false;
		mui.controls.maptypecontrol = false;
		mui.controls.menumaptypecontrol = false;		   
		mui.controls.scalecontrol = true;
		map.setUI(mui);
	}
 
     // Creates a basic marker of the specified color
    function createBasicMarker(point, index, name, address, town, action1, action2, markerflag) {
      // Create a colored icon for this point using our icon class
      var colorIcon = new GIcon(G_DEFAULT_ICON);
      colorIcon.image = "http://www.capecodvisitorsdirectory.com/images/" + markerflag + "marker.png";
      markerOptions = { icon:colorIcon };
      var marker = new GMarker(point, markerOptions);
      GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml('<b>' + name + '</b><br>' + address + '<br>' + town + '<br><a href=http://' + action2 + ' target=new >' + action1 + '</a>');
      });
      return marker;
    }
 
    // Creates a marker that displays the letter corresponding to the given index.
    function createMarker(point, index, name, address, town, action1, action2, markerflag) {
      // Create a lettered icon for this point using our icon class
      var letter = String.fromCharCode("A".charCodeAt(0) + index);
      var letteredIcon = new GIcon(G_DEFAULT_ICON);
      letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";
      markerOptions = { icon:letteredIcon };
      var marker = new GMarker(point, markerOptions);
      GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml('<b>' + name + '</b><br>' + address + '<br>' + town + '<br><a href=http://' + action2 + ' target=new >' + action1 + '</a>');
      });
      return marker;
    }


	function showMarkers (srcdata) {
		// document.write(srcdata);
		GDownloadUrl(srcdata, function(data) {
			var xml = GXml.parse(data);
			var markers = xml.documentElement.getElementsByTagName("marker");
			for (var i = 0; i < markers.length; i++) {
				var latlng = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
										 parseFloat(markers[i].getAttribute("lng")));
				var name = markers[i].getAttribute("name");
				var address = markers[i].getAttribute("address");			
				var town = markers[i].getAttribute("town"); 
				var action1 = markers[i].getAttribute("action1");
				var action2 = markers[i].getAttribute("action2");
				markerflag = markers[i].getAttribute("color");
				if (markerflag == "letter") {
					map.addOverlay(createMarker(latlng, i, name, address, town, action1, action2, markerflag));
				} else {
					map.addOverlay(createBasicMarker(latlng, i, name, address, town, action1, action2, markerflag));
				}
		}   // end of function(data) call
        });  // end of GDownloadUrl
	}
	

	function show(fn) {
	  if (activetown != 'capecod') {
      var datasrc = 'http://www.capecodvisitorsdirectory.com/' + activetown + fn + '.xml';
	  // document.write(datasrc);
	  showMarkers(datasrc);
	  }
    }					
	
    function clearMarkers() {
      map.clearOverlays();
    }
	
