<?php
	require_once('classes.php');
	
	session_start();
	
	if(isset($_SESSION['error'])) {
		echo $_SESSION['error'];
		echo 'Please <a href="index.php">go back</a> to re-enter the information correctly.';
		session_destroy();
		die();
	}
	
	if(!isset($_SESSION['populationZCTA'])) {
		echo 'Please <a href="index.php">go back</a> to re-enter the information correctly.';
		session_destroy();
		die();
	}
	
	$populationZCTA = $_SESSION['populationZCTA'];
	$existingSites = $_SESSION['existingSites'];
	$selectedSites = $_SESSION['selectedSites'];
	$radius = $_SESSION['radius'];
	$existingSitesStats = $_SESSION['existingSitesStats'];
	$selectedSitesStats = $_SESSION['selectedSitesStats'];
	$totalStats = $_SESSION['totalStats'];
	
	session_destroy();

	$existingSiteBorderColor = '#00FF00';
	$existingSiteFillColor = '#009900';
	$selectedSiteBorderColor = '#FF0000';
	$selectedSiteFillColor = '#FF6600';
?>

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8" />
		<title>University of Iowa Site Placement Calculator Results</title>
		<link rel="shortcut icon" href="../favicon.ico" />
		<link rel="stylesheet" type="text/css" href="../style.css" />

		<style type="text/css">
			#content {
				width: 1000px;
				margin-left: auto;
				margin-right: auto;
			}
			
			tr.site {
				text-align: center;
			}
			
			tr.sitesList {
				vertical-align: top;
			}

			#map_canvas {
				height: 800px;
				width: 1000px;
			}
		</style>

		<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>

		<script type="text/javascript">
			var markers = [];
			var existingSites = {};
			var selectedSites = {};

<?php	
	foreach($existingSites as $existingSite) {
		$centroid = $populationZCTA->getZCTACentroid($existingSite);
		$latitude = $centroid->getLatitude();
		$longitude = $centroid->getLongitude();
		$populationCoverage = $populationZCTA->getTotalPopulation($populationZCTA->getZCTAsWithinRadius($existingSite, $radius));
		
		echo "existingSites['{$existingSite}'] = { center: new google.maps.LatLng({$latitude}, {$longitude}), populationCoverage: {$populationCoverage} };\n";
	}

	foreach($selectedSites as $selectedSite) {
		$centroid = $populationZCTA->getZCTACentroid($selectedSite);
		$latitude = $centroid->getLatitude();
		$longitude = $centroid->getLongitude();
		$populationCoverage = $populationZCTA->getTotalPopulation($populationZCTA->getZCTAsWithinRadius($selectedSite, $radius));
	
		echo "selectedSites['{$selectedSite}'] = { center: new google.maps.LatLng({$latitude}, {$longitude}), populationCoverage: {$populationCoverage} };\n";
	}
?>
			
			function initialize() {
				var options = {
					zoom: 4,
					center: new google.maps.LatLng(40, -95), //roughly the center of the US
					mapTypeId: google.maps.MapTypeId.ROADMAP
				};
				var map = new google.maps.Map(document.getElementById("map_canvas"), options);

				var infoWindow = new google.maps.InfoWindow({
					maxWidth: 200
				});

				//close info window if it's open if user clicks on map
				google.maps.event.addListener(map, 'click', function() {
					infoWindow.close();
				});

				//draw radius and marker for existing sites
				for(var site in existingSites) {
					var center = existingSites[site].center;
					var populationCoverage = existingSites[site].populationCoverage;
				
					//add circle
					var circle = new google.maps.Circle({
						strokeColor: '<?php echo $existingSiteBorderColor; ?>',
						strokeOpacity: 0.8,
						strokeWeight: 2,
						fillColor: '<?php echo $existingSiteFillColor; ?>',
						fillOpacity: 0.35,
						map: map,
						center: center,
						radius: <?php echo $radius * 1609.344; //convert miles to meters ?>
					});

					//add marker
					var marker = new google.maps.Marker({
						position: center,
						map: map,
						site: site,
						populationCoverage: populationCoverage
					});

					//display info if marker is clicked
					google.maps.event.addListener(marker, 'click', function() {
						infoWindow.close();
						infoWindow.setContent('<i>Existing Site</i><br/><b>ZCTA:</b> ' + this.site + '<br/><b>Population Coverage:</b> ' + this.populationCoverage);
						infoWindow.open(map, this);
					});
					markers[site] = marker;
				}

				//draw radius and marker for selected sites
				for(var site in selectedSites) {
					var center = selectedSites[site].center;
					var populationCoverage = selectedSites[site].populationCoverage;

					var circle = new google.maps.Circle({
						strokeColor: '<?php echo $selectedSiteBorderColor; ?>',
						strokeOpacity: 0.8,
						strokeWeight: 2,
						fillColor: '<?php echo $selectedSiteFillColor; ?>',
						fillOpacity: 0.35,
						map: map,
						center: center,
						radius: <?php echo $radius * 1609.344; //convert miles to meters ?>
					});

					//add marker
					var marker = new google.maps.Marker({
						position: center,
						map: map,
						site: site,
						populationCoverage: populationCoverage
					});

					//display info if marker is clicked
					google.maps.event.addListener(marker, 'click', function() {
						infoWindow.close();
						infoWindow.setContent('<i>Selected Site</i><br/><b>ZCTA:</b> ' + this.site + '<br/><b>Population Coverage:</b> ' + this.populationCoverage);
						infoWindow.open(map, this);
					});
					markers[site] = marker;
				}
			}

			//allow clicking a link to trigger the relevant marker
			function triggerMarker(site) {
				google.maps.event.trigger(markers[site], 'click');	
			}
		</script>
	</head>
	<body onload="initialize();">
		<div id="container">
			<div id="header">
				<b>University of Iowa Site Placement Calculator Results</b>
			</div>
			
			<div id="content">
				<table>
					<tr>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>
							<span style="font-size: large; text-decoration: underline; font-weight: bold;">Statistics</span><br />
<?php
	if(!$populationZCTA->isMultiState()) {
?>
							<b>State Population:</b> <?php echo $populationZCTA->getStatePopulation(); ?><br />
<?php
	}
?>
							<b>Existing Sites:</b> <?php echo $existingSitesStats; ?><br />
							<b>Selected Sites:</b> <?php echo $selectedSitesStats; ?><br />
							<b>Total:</b> <?php echo $totalStats; ?><br />
						</td>
					</tr>
					<tr class="sitesList">
						<td>
							<table>
								<tr>
									<td><span style="text-align: center; font-size: large; text-decoration: underline; font-weight: bold; color: <?php echo $existingSiteBorderColor; ?>">Existing Sites</span></td>
								</tr>
								
<?php
	foreach($existingSites as $existingSite) {
		echo '<tr class="site">';
		echo "<td><a href=\"javascript:triggerMarker($existingSite);\">$existingSite</a></td>\n";
		echo '</tr>';
	}
?>

							</table>
						</td>
						<td>
							<table>
								<tr>
									<td><span style="text-align: center; font-size: large; text-decoration: underline; font-weight: bold; color: <?php echo $selectedSiteBorderColor; ?>">Selected Sites</span></td>
								</tr>
								
<?php
	foreach($selectedSites as $selectedSite) {
		echo '<tr class="site">';
		echo "<td><a href=\"javascript:triggerMarker($selectedSite);\">$selectedSite</a></td>\n";
		echo '</tr>';
	}
?>

							</table>
						</td>
						<td>
							<div id="map_canvas"></div>
						</td>
					</tr>
				</table>
			</div>
			
			<div id="footer">
				<a href="http://compepi.cs.uiowa.edu/"><img src="../compepi_logo.png" alt="compepi_logo.png" height="67" width="260" /></a>
			</div>
		</div>
	</body>
</html>
