#!/usr/bin/perl
use strict;
use lib '/usr/lib/perl5/5.8.3';
use CGI ':standard';

my $idstring;		#title of photo for display
my $testindex;		#dummy to check whether there is a multiple of 4 photos, and if not to fill additional table divisions with blanks
my $testpic;		#dummy to check whether a photo of the $testindex exists, made by appending ".jpg" to $idstring
my $headtext;		#text string for introductory text
my $titletext;		#text string for http title
my $descriptive;
my $setindex;		#value of first photo, prefix to be added to $idstring, 

my $count;
my $prefix;
my $altext;
#variables above copied from common.cgi

my $dragonfield;	#single line from csv file
my @arrayline;		#split into an array
my @dragonarray;	#array created from each line of csv file, as an array
my $datafile;		#name of csv file
my $fh;			#file handle
my $i; my $j; my $k;	#loop index
my @titles; my @dates; my @places;  my @indexes; my @gridref; #list of required captions
my $title; my $place; my $index;		#
my $keystring;

$dragonfield = "1";	#ensure variable is defined
$datafile = "../dragonflies.csv";

open $fh, "$datafile" or die "Can't open dragonflies.csv: $!";

#open data file and extract each line of data, placing it in an array
$i = 0;

while (defined($dragonfield)) {
defined($dragonfield = readline($fh)) or last;
@dragonarray = split(/,/,$dragonfield);
$indexes[$i]=$dragonarray[0];
$titles[$i]=$dragonarray[1];
$dates[$i]=$dragonarray[3];
$places[$i]=$dragonarray[5];
$gridref[$i]=$dragonarray[6];
$i++;
#print $dragonarray[1]." ".$dragonarray[3]." ".$dragonarray[5]."\n";
}

#there are newlines at the end of each record. get rid of them.
foreach $place (@gridref) {
chop ($place);
}
#get rid of index heading
#shift(@indexes);


$headtext="Photos of Dragonflies";
$descriptive="A collection of dragonflies from Norfolk";
$setindex="00";
$titletext="The Dragonfly gallery";

$count=1;
$testpic=$count."s.jpg";

print "content-type: text/html\n\n";
while (-e "../dragpics/$testpic") {
++$count;

$testpic=$count."s.jpg";


#print qq($count " ");
#print qq($testpic", .");
}

--$count;
#print qq($count " ");
while (($count/3)-(int $count/3)>0.0001){
++$count;
}

#print "content-type: text/html\n\n";

print q(
<!DOCTYPE HTML>
<html lang="en">
<head>);
print qq(
<title>$titletext</title>);
print q(
<STYLE TYPE="text/css">
@import url(../gallery.css);
</STYLE>
<meta charset="UTF-8">
</head>
<body>);

print qq(
<h1>$titletext</h1>
<h2>
$headtext
</h2>
<p>$descriptive</p>
<p>Click the thumbnails to see a bigger picture</p>
<table cellspacing="0" id="inform">
<tr>
<td id="navbuttons" valign="top">
<div class="pics">
<a href="../danhayley.html" class="chapter"><img src="../dan-and-hayley-web.jpg" alt="Dan and Hayley" ></a>
<a href="../danhayley.html" class="chaptext">Dan & Hayley</a>
</div>
<div class="pics">
<a href="../julesomar-index.html" class="chapter"><img src="../jules-and-omar-web.jpg" alt="Jules and Omar at the fair"></a>
<a href="../julesomar-index.html" class="chaptext">Jules & Omar</a>
</div>
<div class="pics">
	<a href="../newgal.html" class="chaptext">Gallery</a><br>
	<a href="../index.html" class="chaptext">Home Page</a><br>
	<a href="mailto:bob\@raoneill.com">Contact</a>
</div>
</td>
<td id="main-image-space">
<table id="pictures">
<tr>);

#Get the number of photos, but ignore the first one ($k=1)
$j=@indexes;
#print "number of photos is ".$j;

for ($k=1; $k<$j; $k++){
$testpic=$indexes[$k]."s.jpg";
$altext=$titles[$k];
$idstring=$titles[$k];
if ($k<=9){
$keystring="0".$k;
}else{
$keystring=$k;
}
#print $testpic."\n";

if (-e "../dragpics/$testpic") {
print qq(
<td class="pic">
<div class="picnew"><a href="dragpics.cgi?&key=$keystring">
<img src="../dragpics/$testpic" alt="$altext" ></a>
<div class="caption">
<cite> $idstring </cite>
</div>
<div class="caption"> $places[$k]
</div>
<div class="caption">$dates[$k]
</div>
</div>
</div>
</td>);
}else{
print qq(
<td class="pic">&nbsp</td>);
}
$testindex=$k;

if (($testindex/3)-(int $testindex/3)<0.0001 and $testindex>0) {
print qq(
</tr><tr>);
}
}

print qq(
</tr>
</td>
</table>
<tr>
<td class="bottom">
&nbsp
<div id="logo" align="center">
<img src="http://www.raoneill.com/powered_by_suse.gif" width=105 height=37 border=0></div>
</td><td class="bottom"><p>&copy; R A & S M O'Neill  2004</p><p>All photographs &copy; The O'Neill family 2003, 2004, 2005,2006,2007,2008</p></td><td class="bottom">&nbsp</td></tr>


</table>

</body>
</html>);
#die;

