#!/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 $uptonfield;	#single line from csv file
my @arrayline;		#split into an array
my @uptonarray;	#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;

$uptonfield = "1";	#ensure variable is defined
$datafile = "../photos.csv";

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

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

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

while (defined($uptonfield)) {
	defined($uptonfield = readline($fh)) or last;
	@uptonarray = split(/,/,$uptonfield);
	$indexes[$i]=$uptonarray[0];
	$titles[$i]=$uptonarray[1];
#	remove inverted commas from titles - these allow commas in the records
	$titles[$i]=~ s/\"//g;
	$i++;
	#print $uptonarray[0]." ".$uptonarray[1]."\n";
}

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


$headtext="Upton Fen and Marshes";
$descriptive="A collection of Photographs of Upton Fen and Marshes, Norfolk";
$setindex="00";
$titletext="The Upton Gallery";

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

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

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


	#print qq($count " "."\n");
	#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="UTF8">
</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">
<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="upton_gal.cgi?&key=00" class="chaptext">Slide Show</a><br>
	<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" class="chaptext">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++){
	if ($k<=9){
		$keystring="0".$k;
	}else{
		$keystring=$k;
	}
	$testpic=$indexes[$k]."s.jpg";
	$altext=$titles[$k];
	$idstring=$titles[$k];
	
#print qq ($testpic.$keystring."\n");

if (-e "../photos/$testpic") {
print qq(
<td class="pic">
	<div class="picold"><a href="uptonpics.cgi?&key=$keystring&key1=0">
	<img src="../photos/$testpic" alt="$altext" ></a>
		<div class="caption">
		<cite> $idstring </cite>
		</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; R A O'Neill  2003 - 2016</p></td><td class="bottom">&nbsp</td></tr>


</table>

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

