<?php
require_once '../../config/settings.inc.php';
$JQUERYUI = "1.12.1";
require_once '../../include/myview.php';
require_once "../../include/cameras.inc.php";
require_once "../../include/forms.php";
define("IEM_APPID", 117);
$t = new MyView();

$java = get_str404("java", 'script');
$cid = get_str404('cid', 'ISUC-006');

$frames  = get_int404('frames', 12);
$interval = get_int404('interval', 300);
$day = get_int404("day", date("d", time() - 3600));
$month = get_int404("month", date("m", time() - 3600));
$hour = get_int404("hour", date("H", time() - 3600));
$minute = get_int404("minute", date("i", time() - 3600));
$minute = $minute - ($minute % 5);
$year = get_int404("year", date("Y", time() - 3600));
$nts = mktime($hour, $minute, 0, $month, $day, $year);

$width = 320; $height = 240;
$szSwitch = mktime(14,0,0,5,12,2011);
if ($nts > $szSwitch){
    $width = 640;
    $height = 480;
}

$now = time();
$wmessage = "";
if ($nts < $cameras[$cid]["sts"] || $nts > $cameras[$cid]["ets"]){
  $wmessage = "<p><div class=\"warning\"><b>Error:</b> Your choice of time and station is outside of the archive available for this site!</div>\n";
  $day = date("d", $cameras[$cid]["ets"] - 3600);
  $month = date("m", $cameras[$cid]["ets"] - 3600);
  $hour = date("H", $cameras[$cid]["ets"] - 3600);
  $minute = date("i", $cameras[$cid]["ets"] - 3600);
  $year = date("Y", $cameras[$cid]["ets"] - 3600);
  $minute = $minute - ($minute % 5);
}

$nts = mktime($hour, $minute, 0, $month, $day, $year);

$images = Array();
$labels = Array();
$files = "";
for ($i=0; $i < (intval($frames));$i++){
  $ts = $nts + ($i * (intval($interval) ));
  if ($ts > $now) { /* We are in the future! */
    $frames = $i;
    break;
  }
  $uri = "/archive/data/". gmdate('Y/m/d/', $ts) ."camera/$cid/". $cid ."_". gmdate('YmdHi', $ts) .".jpg";
  $labels[] = date("d M Y H:i a", $ts);
  $images[] = $uri;
  $files .= $uri .",";
}

$cselect = "<select name=\"cid\">";
foreach($cameras as $k => $v){
    if ($v["network"] == "IDOT"){ continue; }
    $lbl = sprintf("%s (%s)", $v["name"], date("d M Y", $v["sts"]) );
    $cselect .= "<option value=\"$k\"";
    if ($cid == $k) $cselect .= "selected=\"selected\"";
    $cselect .= ">". $lbl ."\n";
}
$cselect .= "</select>";
$ys = yearSelect(2003, $year, "year");
$ms = monthSelect($month);
$ds = daySelect($day);
$hs = hourSelect($hour, "hour");
$mi = minuteSelect($minute, "minute",5);

$ar = Array(
        "script" => "Java Script",
        "none" => "List Images",
);
$loopselect = make_select("java", $java, $ar);

$ar = Array(
        "60" => "1 minute",
        "300" => "5 minutes",
        "900" => "15 minutes",
        "3600" => "1 hour",
        "86400" => "1 day",
        "604800" => "1 week",
);
$intselect = make_select("interval", $interval, $ar);

$jsextra = "";
$headextra = "";
if ($java == "script") {
    $t->jsani = true;
    $sz = sizeof($images);
    $hidden_list = '<ul id="iemjsani_frames" style="display: none;">';
    foreach($images as $key => $val){
        $hidden_list .= sprintf("<li>%s</li>", $val);
    }
    $hidden_list .= "</ul>";
    $control = <<<EOM
<div id="iemjsani" style="width: {$width}px; height: {$height}px;"></div>
{$hidden_list}
EOM;
} else {
    $control = "";
    foreach($images as $key => $val){
        $control .= sprintf("<img src=\"%s\" style=\"margin: 2px;\">", $val);
    }
}

$imglinks = "";
reset($images);
foreach($images as $key => $value){
    $imglinks .= "<li><a href=\"". $images[$key] ."\">". $labels[$key] ."</a></li>";
}

$t->jsextra = $jsextra;
$t->headextra = $headextra;
$t->content = <<<EOM
{$wmessage}

<p>Still images are saved from the SchoolNet8 web cameras every 5 minutes.
This page gives you a tool to build animations from this archive of images.</p>


<form method="GET" action="bloop.phtml" name="imageform">

<table>
<tr><th>Camera (Archive Starts / Ends):</th>
<th>Year:</th><th>Month:</th><th>Day:</th><th>Hour:</th><th>Minute:</th></tr>
 <tr>
  <td>{$cselect}
</td>

<td>{$ys}</td>
<td>{$ms}</td>
<td>{$ds}</td>
<td>{$hs}</td>
<td>{$mi}</td>
</tr>
</table>

<table>
<tr>
<th>Frames:</th>
<td><input type="text" maxsize="2" size="3" name="frames" value="{$frames}"></td>
<th>Loop Tool:</th>
<td>{$loopselect}</td>
<th>Frame Interval:</th><td>
{$intselect}
</td></tr></table>
<input type="submit" value="Gen Loop"></form>

<div style="text-align: center;">
{$control}
</div>

<p>Images used in this loop.<br>
<ul>
{$imglinks}
</ul>

EOM;
$t->render('single.phtml');
