<?
include('includes/classes/AVOMysqli.php');
$db = new AVOMysqli();

$get = $db->check_gpc($_GET, 'full');

$volcname = $get['volcname'];
$offset = $get['offset'];
$station = $get['station'];

// Where the archives are
$dir = '/var/www/html/webicorders/';

// check the offset to see make sure it's either 24 or 48
if($offset != 24 && $offset != 48){
	$offset = '';
}
if($volcname){
	//$globalQ = "SELECT comments FROM tblhelicorderoptions WHERE optionID = 1";
	//$global_comment = $db->db_fetch_object($db->db_query($globalQ))->comments;
	
	$stationQ = '';
	$params[] = array($volcname, 's');
	
	if($station){
		$stationQ = " AND vc.camID = ? ";
		$params[] = array($station, 's');
	}
	
	$sql = "SELECT v.VolcNameID, v.Volcano, hc.comments, vc.* FROM tbllistvolc AS v
		INNER JOIN tblvalvecam AS vc ON (vc.VolcNameID = v.VolcNameID)
		LEFT JOIN tblhelicorderoptions AS hc ON (vc.camID = hc.camID)
		WHERE v.Volcano = ? $stationQ AND vc.plot_available = 1";	
	$stmt = $db->stmt($sql, $params);
	if(is_object($stmt) && $stmt->num_rows > 0){
		$webinfo = $db->fetch_object($stmt);
		/*
		$stationsQ = "SELECT tblvalvecam.camID, tblvalvecam.StationName FROM tblvalvecam WHERE VolcNameID = '$webinfo->VolcNameID' 
			AND tblvalvecam.plot_available = 1";
		$stations = $db->db_create_array($stationsQ);
		*/
		$file = $webinfo->plotPage;
		$loc = '/webicorders/' . $volcname . '/';
		$loc .= ($station) ? $webinfo->StationName . '.php' : '';
		$ok = true;
		$db->cleanup($stmt);
		header("Location:$loc");
	}
	else{
		$ok = false;
	}
}

$allwebiQ = "SELECT DISTINCT tbllistvolc.Volcano FROM tbllistvolc INNER JOIN tblvalvecam ON (tblvalvecam.VolcNameID = tbllistvolc.VolcNameID)
	WHERE tblvalvecam.plot_available = 1 ORDER BY Volcano";
$allwebi = $db->query($allwebiQ);

// check to see if we want to pull camera images from an external url
$type = 'webicorders';
$extQ = "SELECT * FROM offsite_storage WHERE type = '$type' LIMIT 1";
$ext = $db->query($extQ);
$row = $ext->fetch_assoc();
$ext->free();

if($row['use'] == 'yes'){
	$webi_dir = $row['basedir'];
}
else{
	$webi_dir = '/webicorders/';
}
include('includes/doctype.htm');
include('includes/notable_css.php');
?>
<title>AVO Webicorders - <? echo $volcname ?></title>
<meta http-equiv="refresh" content="300" />
<meta http-equiv="description" content="Alaska Volcano Observatory webicorders.  The webicorders displayed here are from seismograph stations at monitored volcanoes in Alaska." />
</head>
<body>
<? include('includes/notable_navigation.php'); ?>
<h1>AVO Webicorders - <? echo $volcname ?></h1>AVO uses several different types of data to monitor volcanoes for signs of unrest or eruptive activity. The data displayed here have not been reviewed in any way, and are preliminary unless reviewed by an AVO geoscientist.</h1>
<div class="container" style="width:15%">
	<div class="info">
		<div class="info_header mediumpurple">Webicorders</div>
		<div class="info_body lightblue">
			<a href="/webicorders/">Introduction</a><br />
			<a href="/webicorders/map.php">Webicorder Map</a>
		</div>
	</div>
	<div class="info">
		<div class="info_header mediumpurple">Available webicorders</div>
		<div class="info_body lightblue">
			<?
			while($row = $allwebi->fetch_object()){
				/*
				 * if we're on the current volcano, we check to see if there's any webicorder archives
				 * yesterday, or the day before yesterday
				 */
				if($volcname == $row->Volcano){
					$dispvolc = 'erupt_selected';
					$arch_48 = $dir . '48hours/' . $filename;
					$arch_24 = $dir . '24hours/' . $filename;
					$arch_string = " ($webinfo->StationName)" . '<div style="margin-left:10px">';
					$href = " <a href=\"?volcname=$volcname\">";
					$href .= ($offset == '') ? '<b>Current</b>' : 'Current';
					$arch_string .= "$href</a><br />";
					if(file_exists($arch_24) && filesize($arch_24) > 0){
						$href = " <a href=\"?volcname=$volcname&offset=24&station=$station\">";
						$href .= ($offset == 24) ? '<b>24 hours</b>' : '24 hours';
						$arch_string .= "$href</a><br />";
					}
					if(file_exists($arch_48) && filesize($arch_48) > 0){
						$href = " <a href=\"?volcname=$volcname&offset=48&station=$station\">";
						$href .= ($offset == 48) ? '<b>48 hours</b>' : '48 hours';
						$arch_string .= "$href</a><br />";
					}
					$arch_string .= '</div>';
					
					// add the other stations as links in the menu bar
					if(is_array($stations) && count($stations) > 1){
						$arch_string .= '<div style="margin-left:10px;margin-top:3px;">Station List:<br />';
						foreach($stations as $id=>$name){
							$style = ($id == $station) ? 'style="font-weight:bold"' : '';
							$arch_string .= "<a $style href=\"?volcname=$volcname&station=$id\">$name</a><br />";
						}
						$arch_string .= '</div>';
					}
				}
				else{
					$arch_string = '';
					$dispvolc = '';
				}
				echo "<div class=\"$dispvolc\"><a href=\"/webicorders/webicorder.php?volcname=$row->Volcano\">$row->Volcano</a>$arch_string</div>\n";
			}
			?>
		</div>
	</div>
	<div class="info">
		<div class="info_header mediumpurple">UTC Conversion</div>
		<div class="info_body lightblue">
			0000 UTC= 3:00 PM AKST on the previous day as UTC <hr />
			0600 UTC= 9:00 PM AKST on the previous day as UTC <hr />
			1200 UTC= 3:00 AM ASKT on the same day as UTC <hr />
			1800 UTC= 9:00 AM AKST on the same day as UTC 
		</div>
	</div>
	<div class="info">
		<div class="info_header mediumpurple">Red Lines</div>
		<div class="info_body lightblue">
		<span style="font-weight:bold;color:#FF0000">RED LINES</span> indicate either a calibration pulse, or where the signal was clipped 
			(too large to be recorded on the scale of the webicorder).
		</div>
	</div>
</div>
<div class="container" style="width:85%;overflow:auto">
	<div class="info">
	<? if($ok){ ?>
		<div class="info_header mediumpurple"><? echo $volcname ?> 24 hour webicorder</div>
		<div class="info_body lightblue">
			(<span style="font-size:10px;"><i><b>Station: <? echo $webinfo->StationName ?></b></i></span>)<br />
			<?
			echo ($global_comment) ? "<br />$global_comment" : '';
			echo ($webinfo->comments) ? "<br />$webinfo->comments" : '';
			if($offset != ''){
				if($offset == 24){
					$testfile = $dir . '24hours/' . $file;
					$image = '24hours/' . $file;
				}
				elseif($offset == 48){
					$testfile = $dir . '48hours/' . $file;
					$image = '48hours/' . $file;
				}
				echo '<br /><span style="color:#0000ff;font-weight:bold">Image created: ' . date ('F d Y H:i:s', filemtime($testfile)) . '</span><br />';
				echo "<img src=\"$image\" alt=\"$volcname webicorder archive\" />";	
			}
			else{
				$dotpos = strpos($file, '.');
				$testfile = substr($file, 0, $dotpos) . '_temp.png';
				$testfile = '/var/www/html/webicorders/' . $file;
				$now = date('U');
				$filedate = date ('U', filemtime($testfile));
				$diff = $now - $filedate;
				$diff_min = $diff/60;
				//if($diff_min < 25 && exif_imagetype($testfile) == IMAGETYPE_PNG){
					// make source local ore remote
					$src = $webi_dir . $webinfo->plotPage;
					echo '<br /><span style="color:#0000ff;font-weight:bold">Image last updated: ' . date ('F d Y H:i:s', filemtime($testfile)) . '</span><br />';
					echo "<img src=\"$src\" alt=\"$volcname 24 hour webicorder\" />";	
				//}
				//else{
					//echo '<img src="WebicorderBroken.gif" alt="webicorder unavailable" />';
				//}
			}
	}
	else{
		echo "Unknown Volcano - $volcname.";
	}
	?>
		</div>
	</div>
</div>
<? include('includes/notable_footer.php'); ?>
</body>
</html>