var map;            // Global map object.
var centroid;       // Global centerpoint for map.
var bounds;         // Constraining rectangle.
var Locations = 8;  // Number of meeting locations to plot.
var zoomStd = 11;   // Initial zoom level.
var zoomMtg = 16;   // Zoom in to a single location.
var gmarkers = [];  // Array of markers
var baseUrl = 'http://www.wsdot.wa.gov/projects/'; 

function GLoad() {
	if (GBrowserIsCompatible()) {
		buildMap();
	}
}

function buildMap() {
	map = new GMap2(document.getElementById('map_canvas'));
	map.addControl(new GLargeMapControl());  // zoom, pan
	map.addControl(new GMapTypeControl());   // map, satellite, hybrid
	map.addControl(new GScaleControl());     // scalebar
	map.enableScrollWheelZoom();             // allow zooming with wheel

	// Must setCenter before starting to add markers
	map.setCenter(new GLatLng(0,0),0);  
	
	// Create an empty bounds object to constrain initial zoom.
	bounds = new GLatLngBounds();

	// Create a base icon for all of our markers that specifies the
	// shadow, icon dimensions, etc.
	var baseIcon = new GIcon();
	baseIcon.shadow = 'http://www.google.com/mapfiles/shadow50.png';
	baseIcon.iconSize = new GSize(20, 34);
	baseIcon.shadowSize = new GSize(37, 34);
	baseIcon.iconAnchor = new GPoint(9, 34);
	baseIcon.infoWindowAnchor = new GPoint(9, 2);
	baseIcon.infoShadowAnchor = new GPoint(18, 25);
	
	// Creates a marker whose info window displays the letter
	// corresponding to the given index.
	function createMarker(point, index, html) {
		// Create a lettered icon for this point using our icon class
		var letteredIcon = new GIcon(baseIcon);
		letteredIcon.image = 'http://www.google.com/mapfiles/marker' + getLetter(index) + '.png';
		
		// Set up our GMarkerOptions object
		markerOptions = { icon:letteredIcon };
		var marker = new GMarker(point, markerOptions);
		
		// Extend map bounds to include this point.
		bounds.extend(point);
		// Persist a global reference to this marker.
		gmarkers[index] = marker;
		
		// Create an event handler for clicks.
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(html);
		});

		// Return new marker object.		
		return marker;
	}
	
	// Add familiar meeting locations
	for (var i = 0; i < Locations; i++) {	
		var location = getLocation(i);
		var point = new GLatLng(location.lat, location.lng);
		map.addOverlay(createMarker(point, i, getBalloon(location)));
	}
	
	// Zoom and Center to bounding rectangle that includes all points.
	zoomStd = map.getBoundsZoomLevel(bounds) - 1;
	map.setZoom(zoomStd);
	centroid = bounds.getCenter();
	map.setCenter(centroid);

	// Make sure "Zoom To All" is visible in non-default situations.
	GEvent.addListener(map, "moveend", function() {
		showZoomAll(true);
	});	
}

function formatXY(x, y) {
	return "(" + x + ", " + y + ")";
}

function getBalloon(location) {
	return '<b>' + location.project + '</b>' +
		'<br><small>' + location.desc + 
		' <a href="' + baseUrl + location.url + '" target="_blank">WSDOT&nbsp;Project&nbsp;Page</a></small>';
}

function getBalloon2(location) {
	return '<div style="width:400px; height: 300px; overflow:auto"><h3>' + location.project + '</h3>' +
		'<small>' + location.desc + 
		' <a href="' + baseUrl + location.url + '" target="_blank">WSDOT&nbsp;Project&nbsp;Page</a></small></div>';
}

function getLetter(index) {
	 return String.fromCharCode("A".charCodeAt(0) + index);
}

function getLocation(index) {
	var locations = new Array();
	locations[0] = { lat:45.788055, lng:-122.537300, project:'North Parkway Improvement', url:'local/north-parkway-improvement-n-5th-to-onsdorff', desc:'Improvements and reconstruction to N Parkway Avenue from N Onsdorff Boulevard to N 5th Street. The street is currently a two lane road without curb &amp; gutter or sidewalk. The road will be rebuilt with three lanes including a center left turn lane. Curb &amp; gutter, sidewalks, planter strips, and street lighting will be added.' };
	locations[1] = { lat:45.614067, lng:-122.446663, project:'Leadbetter Drive', url:'local/leadbetter-drive-lake-rd-to-parker-st', desc:'This project will provide a continuous corridor from Parker Street to Lake Road including one travel lane in each direction, bike lanes, sidewalk, and widened intersections to accommodate left turn pockets.' };
	locations[2] = { lat:45.693599, lng:-122.542281, project:'NE 99th Street', url:'local/ne-99th-street-sr-503-to-ne-137th-avenue', desc:'Upgrade of a narrow, 2-lane road to a 3-lane minor arterial between SR 503 (a major state highway) and NE 137th Ave, a County arterial. The new road will have a center left-turn lane, bicycle lanes and sidewalks.' };
	locations[3] = { lat:45.641228, lng:-122.707497, project:'West Vancouver Freight Access Project', url:'local/west-vancouver-freight-access-project', desc:'Construction of dual carrier, unit train access through the existing port facilities and into the new Terminal 5 (west end unit train capacity). Includes new rail infrastructure within existing facilities, and new track at Terminal 5.' };
	locations[4] = { lat:45.624718, lng:-122.677345, project:'Downtown Vancouver Waterfront Access Project', url:'local/downtown-vancouver-waterfront-access-project', desc:'Construct two bridges for streets which shall cross under a BNSF mainline at Grant and Esther Streets.' };
	locations[5] = { lat:45.575996, lng:-122.353867, project:'SR 14 Pedestrian Tunnel', url:'local/sr-14-pedestrian-tunnel-285276-te', desc:'The project will construct a pedestrian tunnel underneath SR 14 to create a pedestrian/bicycle connection between the newly reconstructed downtown Washougal city core and the Columbia River waterfront.' };
	locations[6] = { lat:45.801460, lng:-122.727798, project:'Hillhurst Road Overlay Project', url:'local/hillhurst-road-overlay-project', desc:'Asphalt overlay of Hillhurst Road between S. 15th Circle and S. 22nd Circle.' };
	locations[7] = { lat:45.815655, lng:-122.685883, project:'SR 501/Ridgefield Interchange', url:'i5/sr501interchange/', desc:'The section of I-5 in the vicinity of the SR 501 Ridgefield interchange needs relief from congestion. This is a partnership project with the City of Ridgefield. Stage 1 will construct a new, wider bridge across I-5 and reconstruct the I-5 on and off ramps. Stage 2 will construct improvements to the connecting roads and adjacent intersections at 56th and 65th Avenues when funding is available.' };
	/*
	locations[0] = { lat:45.631368, lng:-122.677567, desc:'Clark County Public Service Center', addr:'1300 Franklin St, Vancouver, WA 98660', url:'http://www.rtc.wa.gov' };
	locations[1] = { lat:45.632138, lng:-122.677200, desc:'Clark County Elections / Auto Licensing', addr:'1408 Franklin St, Vancouver, WA 98660', url:'' };
	locations[2] = { lat:45.643405, lng:-122.703774, desc:'Port of Vancouver USA Administrative Office', addr:'3103 NW Lower River Rd, Vancouver, WA 98660', url:'http://www.portvanusa.com/about/drivingdir.html' };
	locations[3] = { lat:45.631510, lng:-122.670113, desc:'Vancouver City Hall', addr:'210 E 13th St, Vancouver, WA 98660', url:'http://www.cityofvancouver.us/' };
	locations[4] = { lat:45.630038, lng:-122.663023, desc:'Clark Public Utilities', addr:'1200 Fort Vancouver Way, Vancouver, WA 98663', url:'http://www.clarkpublicutilities.com/contactus/offices' };
	locations[5] = { lat:45.639052, lng:-122.657920, desc:'Clark County Public Health', addr:'1601 East Fourth Plain Blvd, Vancouver, WA 98661', url:'http://www.clark.wa.gov/health/' };
	locations[6] = { lat:45.594472, lng:-122.505375, desc:'Fisher\'s Landing Transit Center', addr:'3510 SE 164th Ave, Vancouver, WA 98683', url:'http://www.c-tran.com/' };
	locations[7] = { lat:45.660079, lng:-122.559308, desc:'WSDOT SW Region Headquarters', addr:'11018 NE 51st Cir, Vancouver, WA 98682', url:'http://wsdot.wa.gov/Regions/SouthWest/' };
	locations[8] = { lat:45.527495, lng:-122.660272, desc:'Metro Regional Center', addr:'600 NE Grand Ave, Portland, OR 97232', url:'http://www.oregonmetro.gov/index.cfm/go/by.web/id=3315' };
	*/
	return locations[index];	
}

function showZoomAll(show) {
	var zout = document.getElementById("zoomout");
	if (show) {
		zout.style.display = "block";
	}
	else {
		zout.style.display = "none";
	}
}

function zoomLocation(index) {
	showZoomAll(true);
	map.setZoom(zoomMtg);
	map.setMapType(G_HYBRID_MAP);
	// Trigger a click event, which pops the balloon.
	GEvent.trigger(gmarkers[index], "click");
	map.panTo(gmarkers[index].getPoint());
}

function zoomAll() {
	map.setCenter(centroid, zoomStd);  
	map.setMapType(G_NORMAL_MAP);
	map.closeInfoWindow();
	showZoomAll(false);
}
