<?

include("conf.php");

$db = mysql_connect($host, $user, $password);
mysql_select_db($usebase,$db);


// SHOW 5 OLDER IMAGES BY DEFAULT
if (!isset($nimages)) {
  $nimages=5;
}

?>

<HTML>
<HEAD>
<TITLE>MOTION DETECT</TITLE>
</HEAD>
<BODY>

<?
echo " <FORM METHOD=\"GET\" ACTION=\"$PHP_SELF\">\n";
echo "<TABLE WIDTH=\"800\" BORDER=\"1\" CELLSPACING=\"1\" CELLPADDING=\"5\" ALIGN=\"center\">\n";

echo "<TR><TH COLSPAN=\"2\">MOTION DETECT : $host</TH></TR>\n";

echo "<TR><TD COLSPAN=\"2\">[Browsing] - [Movie]</TD></TR>\n";

echo "<TR>";

echo "<TD WIDTH=\"300\">";
echo "Last Update :<BR>\n";
echo "Last archived<BR>\n";
echo "<P>";
echo "Show previous <SELECT style=\"width:50px;\" name=nimages size=1 >\n";
for ($i = 1; $i <= 100; $i++) {
  if ($i==$nimages) {
    echo "<OPTION SELECTED>$i";  
  } else {
    echo "<OPTION>$i";  
  }
}
echo "</SELECT> images\n";
echo "<BR>";
echo " <INPUT TYPE=\"submit\" NAME=\"show\" VALUE=\"SHOW SELECTED FILE\" >\n";
echo "</TD>";

if (isset($imageselect)) {
  list ($width,$height,$type,$timestamp)=getimageinfo($imageselect);
  list ($year,$month,$day,$hour,$minute,$second)=timestamp($timestamp);
  $year= $timestamp[0] .$timestamp[1] . $timestamp[2] . $timestamp[3];
  printf("<TH CLASS=\"header\">%s-%s-%s %sh%sm%ss<BR>",$year,$month,$day,$hour,$minute,$second);
echo "<A HREF=\"\"><--</A>";
echo "<IMG SRC=\"getbindata.php?imageselect=$imageselect\">";
echo "<A HREF=\"\">--></A>";
echo "<BR>";
listbox_images($imageselect,20);
  echo "</TH>\n";
}



echo "</TABLE>";
echo "</FORM>";


if (isset($nimages)) {
  $itemsperrow=5;
  echo "<TABLE WIDTH=\"800\" ALIGN=\"CENTER\" BORDER=\"1\">";
  echo "<TR>";
  $count=0;
  for ($i=$imageselect-$nimages;$i<=($imageselect);$i++) {
    $count++;
    list ($width,$height,$type,$timestamp)=getimageinfo($i);
    list ($year,$month,$day,$hour,$minute,$second)=timestamp($timestamp);
    $year= $timestamp[0] .$timestamp[1] . $timestamp[2] . $timestamp[3];
    if ($newTR==1) {
      echo "<TR>";
      $newTR=0;
    }

	// SNAP SHOT SIZE
	// AT SOME POINT THESE IMAGES SHOULD BE GENERATED DYNAMICALLY
    $w=100;
    $h=$w*$height/$width;
    printf("<TH COLSPAN=\"3\" CLASS=\"header\"><FONT SIZE=\"-2\">%s-%s-%s %sh%sm%ss</FONT><BR>\n",$year,$month,$day,$hour,$minute,$second);
    echo "<A HREF=\"$PHP_SELF?imageselect=$i&nimages=$nimages\"><IMG ALIGN=\"CENTER\" WIDTH=\"$w\" HEIGHT=\"$h\" SRC=\"getbindata.php?imageselect=$i\"></A></TH>";
    if (($count/$itemsperrow)==round($count/$itemsperrow)) {
      echo "</TR>";
      $newTR=1;
    }
  }
  echo "</TR></TABLE>";
}



?>

</BODY>
</HTML>


<?

function listbox_images($imageselect,$limit) {
require("conf.php");
 
if ($limit<1) {
  $limit=5;
}
 
 $db = mysql_connect($host, $user, $password);
 mysql_select_db($usebase,$db);

$result = mysql_query("SELECT id,timestamp FROM images ORDER BY timestamp DESC LIMIT 0,$limit",$db);
$nresults= mysql_affected_rows();

echo "<SELECT style=\"width:200px;\" name=imageselect size=1 >\n";
if ($myrow = mysql_fetch_array($result)) {
do {



      $id=$myrow["id"];
      $timestamp=$myrow["timestamp"];

      list ($year,$month,$day,$hour,$minute,$second)=timestamp($timestamp);
      $year= $timestamp[0] .$timestamp[1] . $timestamp[2] . $timestamp[3];
      $name=sprintf("%s-%s-%s %sh%sm%ss",$year,$month,$day,$hour,$minute,$second);


      if ($imageselect==$id) {
        printf(" <OPTION SELECTED value=%s>%s\n",$id,$name);
                } else {
        printf(" <OPTION value=%s>%s\n",$id,$name);

        }

    } while ($myrow = mysql_fetch_array($result));
} else {
        echo "No records found";
}
echo "</SELECT>\n";
}



function getimageinfo($id) {
  require("conf.php");
  $db = mysql_connect($host, $user, $password);
  mysql_select_db($usebase,$db);

  $result = mysql_query("SELECT width,height,type,timestamp FROM images WHERE id=$id ",$db);
  if ($row = mysql_fetch_array($result)) {
    $width=$row["width"];
    $height=$row["height"];
    $type=$row["type"];
    $timestamp=$row["timestamp"];
    return array($width,$height,$type,$timestamp);

  }
}


function timestamp($timestamp) {
  $year= $timestamp[0] .$timestamp[1] . $timestamp[2] . $timestamp[3];
  $month= $timestamp[4] .$timestamp[5];
  $day= $timestamp[6] .$timestamp[7];

  $hour= $timestamp[8] .$timestamp[9];
  $minute= $timestamp[10] .$timestamp[11];
  $second= $timestamp[12] .$timestamp[13];

  return array($year,$month,$day,$hour,$minute,$second);

}






?>