#!/usr/local/bin/perl

use CGI;
use File::Basename;
use Image::Info qw(image_info);
use Data::Dumper;

use strict;

REQUEST:
foreach my $q (new CGI)
{
   my $which = $q->param('which');

   unless ($which && -f "images/$which")
   {
      print $q->redirect("./");
      next REQUEST;
   }

   my @images =
	sort
	map { basename $_ }
	<images/*.jpg>;

#		print STDERR Dumper(\@images);

   my ($prev, $next);

   if ($which eq $images[0])
   {
      $next = $images[1];
   }
   else
   {
      my $junk = shift @images while (@images && $images[1] ne $which);

      unless ($images[1] eq $which)
      {
#		print STDERR Dumper(\@images);
         die "Blargh!";
      }

      $prev = $images[0];
      $next = $images[2];

   }

   my $imginfo = image_info("images/$which");

   print $q->header, $q->start_html;

   printf qq{
<center>
<p>%s <a href="index.asp">Thumbnails</a> %s</p>
<img width=%s height=%s src="images/%s">
</center>
},
	$prev ? sprintf(q{<a href="image.pl?which=%s">Previous</a>}, $prev) : 'Previous',
	$next ? sprintf(q{<a href="image.pl?which=%s">Next</a>}, $next) : 'Next',
	$imginfo->{width}, $imginfo->{height},
	$which,
	;

   print $q->end_html;

}


