var map;

/*
entryPts = [
	['Express Lanes Southend',38.7909,-77.1656],
	['I-95/395',38.7914,-77.1754],
	['Capital Beltway Mainline North of Springfield',38.7999,-77.2062],
	['Braddock Road',38.8112,-77.2178],
	['Gallows Rd',38.8538,-77.2200],
	['I-66',38.8862,-77.2212],
	['Westpark Drive',38.9193,-77.2170],
	['Jones Branch',38.9278,-77.2135],
	['Express Lanes Northend',38.9455,-77.2027],
	['Dulles Toll Road',38.9318,-77.2117],
	['Jones Branch Drive',38.9278,-77.2135],
	['Westpark',38.9193,-77.2170],
	['Leesburg Pike',38.9105,-77.2178],
	['I-66',38.8862,-77.2212],
	['Lee Highway',38.8758,-77.2206],
	['Capital Beltway Mainline North of Springfield',38.7999,-77.2062],
	['I-95/395',38.7914,-77.1754]
];

exitPts = [
	['I-95/395',38.7914,-77.1754],
	['Capital Beltway Mainline North of Springfield',38.7999,-77.2062],
	['Lee Highway',38.8758,-77.2206],
	['I-66',38.8862,-77.2212],
	['Leesburg Pike',38.9105,-77.2178],
	['Westpark',38.9193,-77.2170],
	['Jones Branch',38.9278,-77.2135],
	['Dulles Toll Road',38.9318,-77.2117],
	['Express Lanes Northend',38.9455,-77.2027],
	['Dulles Toll Road',38.9318,-77.2117],
	['Jones Branch Drive',38.9278,-77.2135],
	['Westpark Drive',38.9193,-77.2170],
	['I-66',38.8862,-77.2212],
	['Gallows Rd',38.8538,-77.2200],
	['Braddock Road',38.8112,-77.2178],
	['Capital Beltway Mainline North of Springfield',38.7999,-77.2062],
	['I-95/395',38.7914,-77.1754],
	['Express Lanes Southend',38.7909,-77.1656]
];*/

function initialize() {
	pathCoord = [
		[38.9455,-77.2027],	//Northend
		[38.9427,-77.2044],
		[38.9409,-77.2054],
		[38.9403,-77.2057],
		[38.9389,-77.2064],
		[38.9374,-77.2073],
		[38.9360,-77.2081],
		[38.9349,-77.2089],
		[38.9336,-77.2100],
		[38.9318,-77.2117],  //Dulles
		[38.9302,-77.2128],
		[38.9278,-77.2135],  //Jones branch
		[38.9262,-77.2141],
		[38.9256,-77.2144],
		[38.9249,-77.2148],
		[38.9238,-77.2154],
		[38.9230,-77.2158],
		[38.9223,-77.2161],
		[38.9211,-77.2166],
		[38.9202,-77.2169],
		[38.9193,-77.2170],  //Westpark
		[38.9187,-77.2172],
		[38.9179,-77.2173],
		[38.9170,-77.2175],
		[38.9160,-77.2176],
		[38.9148,-77.2177],
		[38.9137,-77.2178],
		[38.9127,-77.2178],
		[38.9117,-77.2178],
		[38.9105,-77.2178],   //Leesburg Pike
		[38.9093,-77.2177],
		[38.9083,-77.2175],
		[38.9074,-77.2174],
		[38.9060,-77.2170],
		[38.9046,-77.2166],
		[38.9029,-77.2160],
		[38.9011,-77.2154],
		[38.9001,-77.2151],
		[38.8992,-77.2149],
		[38.8985,-77.2148],
		[38.8977,-77.2148],
		[38.8968,-77.2149],
		[38.8958,-77.2151],
		[38.8952,-77.2153],
		[38.8943,-77.2156],
		[38.8936,-77.2160],
		[38.8927,-77.2165],
		[38.8918,-77.2170],
		[38.8909,-77.2176],
		[38.8898,-77.2184],
		[38.8887,-77.2193],
		[38.8877,-77.2202],
		[38.8862,-77.2212],   //I-66
		[38.8851,-77.2214],
		[38.8843,-77.2217],
		[38.8834,-77.2219],
		[38.8827,-77.2219],
		[38.8818,-77.2218],
		[38.8808,-77.2216],
		[38.8797,-77.2215],
		[38.8787,-77.2213],
		[38.8772,-77.2210],
		[38.8758,-77.2206],   //Lee Hwy
		[38.8747,-77.2204],
		[38.8738,-77.2202],
		[38.8727,-77.2202],
		[38.8713,-77.2202],
		[38.8692,-77.2202],
		[38.8681,-77.2201],
		[38.8666,-77.2202],
		[38.8648,-77.2202],
		[38.8629,-77.2201],
		[38.8602,-77.2201],
		[38.8582,-77.2201],
		[38.8570,-77.2201],
		[38.8558,-77.2200],
		[38.8548,-77.2200],
		[38.8538,-77.2200],  //Gallows Rd
		[38.8523,-77.2200],
		[38.8509,-77.2200],
		[38.8499,-77.2201],
		[38.8483,-77.2201],
		[38.8472,-77.2201],
		[38.8462,-77.2201],
		[38.8453,-77.2201],
		[38.8443,-77.2201],
		[38.8436,-77.2199],
		[38.8428,-77.2198],
		[38.8419,-77.2197],
		[38.8413,-77.2196],
		[38.8403,-77.2194],
		[38.8389,-77.2190],
		[38.8377,-77.2186],
		[38.8363,-77.2183],
		[38.8355,-77.2181],
		[38.8348,-77.2181],
		[38.8340,-77.2180],
		[38.8332,-77.2181],
		[38.8323,-77.2182],
		[38.8315,-77.2183],
		[38.8308,-77.2185],
		[38.8301,-77.2187],
		[38.8295,-77.2190],
		[38.8286,-77.2194],
		[38.8258,-77.2208],
		[38.8247,-77.2213],
		[38.8239,-77.2217],
		[38.8231,-77.2220],
		[38.8222,-77.2222],
		[38.8214,-77.2223],
		[38.8205,-77.2223],
		[38.8198,-77.2222],
		[38.8192,-77.2222],
		[38.8184,-77.2220],
		[38.8178,-77.2219],
		[38.8171,-77.2216],
		[38.8163,-77.2213],
		[38.8154,-77.2208],
		[38.8148,-77.2204],
		[38.8135,-77.2196],
		[38.8112,-77.2177],   //Braddock Rd
		[38.8089,-77.2161],
		[38.8070,-77.2147],
		[38.8045,-77.2127],
		[38.8035,-77.2121],
		[38.8029,-77.2114],
		[38.8023,-77.2109],
		[38.8018,-77.2102],
		[38.8011,-77.2093],
		[38.800855,-77.20843],  //Springfield
		[38.8006,-77.2082],
		[38.8002,-77.2072],
		[38.7999,-77.2062],   
		[38.7990,-77.2026],
		[38.7984,-77.2000],
		[38.7977,-77.1965],
		[38.7968,-77.1926],
		[38.7963,-77.1904],
		[38.7958,-77.1887],
		[38.7952,-77.1867],
		[38.7947,-77.1853],
		[38.7942,-77.1839],
		[38.7932,-77.1815],
		[38.7927,-77.1803],
		[38.7922,-77.1789],
		[38.7918,-77.1775],
		[38.7915,-77.1761],
		[38.7914,-77.1754],   
		[38.7910,-77.1727],
		[38.7908,-77.1698],
		[38.7908,-77.1671],
		[38.7909,-77.1656]   //Southend
	];

	var zoomLevel;
	var entryPt = $('#entryPtSel option:selected').val();
	var exitPt = $('#exitPtSel option:selected').val();

	if (!isNaN(entryPt) && !isNaN(exitPt)) {
		//zoomLevel = Math.floor (-20*(Math.abs(exitPts[exitPt][1]-entryPts[entryPt][1])) + 13);
		zoomLevel = Math.floor (-20*(Math.abs(gantry_data.interchanges[exitPt].latitude-gantry_data.interchanges[entryPt].latitude)) + 13);
		if (zoomLevel < 10) {
			zoomLevel = 10;
		}
		avgLat = (gantry_data.interchanges[entryPt].latitude + gantry_data.interchanges[exitPt].latitude) / 2;
		avgLong = (gantry_data.interchanges[entryPt].longitude + gantry_data.interchanges[exitPt].longitude) / 2;
		var myLatlng = new google.maps.LatLng(avgLat,avgLong);
	} else {
		zoomLevel = 10;
		var myLatlng = new google.maps.LatLng(38.862,-77.200);
	}
	var myOptions = {
		zoom: zoomLevel,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		center: myLatlng
	}
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	
}

initialized = false;
function initializeOnce() {
	if (!initialized) {
		initialize();
		plotInitialPath();
		initialized = true;
	}
}

function plotInitialPath () {
	//Plots the path of the exp. lanes on initial load
	var etPt = gantry_data.interchanges[0];
	var extPt = gantry_data.interchanges[10];
	var buildPath = [];

	var j = 0;

	while (pathCoord[j][0] != extPt.latitude && pathCoord[j][1] != extPt.longitude) {
		buildPath.push(new google.maps.LatLng(pathCoord[j][0], pathCoord[j][1]));
		j++;
	}

	buildPath.push(new google.maps.LatLng(extPt.latitude, extPt.longitude));

	var plotPath = new google.maps.Polyline({
		path: buildPath,
		strokeColor: "#0000FF",
		strokeOpacity: 0.5,
		strokeWeight: 8
	});
		
	plotPath.setMap(map);
}

function calcRoute() {
	initialize();

	var entryPt = $("#entryPtSel").val();
	var exitPt = $("#exitPtSel").val();

	var etPt = gantry_data.interchanges[entryPt];
	var extPt = gantry_data.interchanges[exitPt];

	if (entryPt) {
		var entryPtLatLng = new google.maps.LatLng(etPt.latitude,etPt.longitude);
		var imageStart = new google.maps.MarkerImage('images/map-marker-start.png',
			// This marker is 35 pixels wide by 26 pixels tall.
			new google.maps.Size(35, 26),
			// The origin for this image is 0,0.
			new google.maps.Point(0,0),
			// The anchor for this image is at the base but to the rt 9 px so 9,32.
			new google.maps.Point(9, 26)
		);

		var markerEntry = new google.maps.Marker({
			position: entryPtLatLng,
			map: map,
			icon: imageStart,
			title: etPt.label
		});

		// To add the marker to the map, call setMap();
		markerEntry.setMap(map);
	}

	if (exitPt.length > 0) {
		var exitPtLatLng = new google.maps.LatLng(extPt.latitude,extPt.longitude);
		var imageEnd = new google.maps.MarkerImage('images/map-marker-end.png',
			// This marker is 35 pixels wide by 26 pixels tall.
			new google.maps.Size(35, 26),
			// The origin for this image is 0,0.
			new google.maps.Point(0,0),
			// The anchor for this image is at the base but to the rt 9 px so 9,32.
			new google.maps.Point(9, 26)
		);

		var markerExit = new google.maps.Marker({
			position: exitPtLatLng,
			map: map,
			icon: imageEnd,
			title: extPt.label
		});

		// To add the marker to the map, call setMap();
		markerExit.setMap(map);
	}

	var infoWindowEntry = new google.maps.InfoWindow({
		content: etPt.label
	});

	var infoWindowExit = new google.maps.InfoWindow({
		content: extPt.label
	});

	google.maps.event.addListener(markerEntry, 'click', function() {
		infoWindowEntry.open(map,markerEntry);
	});

	google.maps.event.addListener(markerExit, 'click', function() {
		infoWindowExit.open(map,markerExit);
	});

	var buildPath = [];

	if (parseInt(exitPt) < parseInt(entryPt)) {
		pathCoord.reverse();
	}

	//get the starting pt for the path
	for (i=0;i<pathCoord.length;i++) {
		if (pathCoord[i][0] == etPt.latitude && pathCoord[i][1] == etPt.longitude) {
			startPt = i;
			break;
		}
	}
	var j = startPt;

	while (pathCoord[j][0] != extPt.latitude && pathCoord[j][1] != extPt.longitude) {
		buildPath.push(new google.maps.LatLng(pathCoord[j][0], pathCoord[j][1]));
		j++;
	}

	buildPath.push(new google.maps.LatLng(extPt.latitude, extPt.longitude));

	var plotPath = new google.maps.Polyline({
		path: buildPath,
		strokeColor: "#0000FF",
		strokeOpacity: 0.5,
		strokeWeight: 8
	});

	plotPath.setMap(map);
}
