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

# A version of dragpics.cgi to show the upton fen photos.
my $picnum;
my $picbig;
my $picclean;
my $next;
my $nextpic;
my $back;
my $backpic;
my $prefix;
my $titlebar="2";
my $heading;
my $indexlink;
my $altext;

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 $len1;		#refers to length of @uptonarray
my @titles; my @dates; my @places;  my @indexes; #list of required captions
my $title; my $place; my $index; my $date;
my $nextlink; my $backlink; my $nextindex; my $direct; #next=1, back=0

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

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

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

while (defined($uptonfield)) {
defined($uptonfield = readline($fh)) or last;
@uptonarray = split(/,/,$uptonfield);
$indexes[$i]=$uptonarray[0];
$titles[$i]=$uptonarray[1];

$i++;
#print $uptonarray[1]." ".$uptonarray[3]." ".$uptonarray[5]."\n";
}

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

$picnum=param('key');
$picnum =~ /(^\d{2}$)/;

if ($1 eq "") {
print "content-type: text/html\n\n";
print '<p>Action not permitted</p>';
print "<p>$back</p><p>$picnum</p>";
die;
} else{
$picclean=$1;
#if (substr($picclean,0,1) eq "0"){
#substr($picclean,0,1,"");
#}
#these next lines strip the leading zeros from numbers less than 10, so these have to be added
$next=$picclean+1;
$back=$picclean-1;
$title=$titles[$picclean];
}
$title=~ s/\"//g;

$direct=param('key1');
$direct=~ /(^\d{1}$)/;
if ($1 eq "") {
print "content-type: text/html\n\n";
print '<p>Action not permitted</p>';
print "<p>$direct</p><p>$picnum</p>";
die;
}

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

#print qq($picnum, $direct);

#print qq($picclean );
#if ($back <1){
#$back++;
#}

if ($next<=9){
$next="0".$next;
}

#print qq(back - $back,);

if ($back<=9){
	$back="0".$back;
	}

$picbig=$picclean.".jpg";
$nextpic=$next.".jpg";

#print qq(next - $nextpic);

#print qq(next - $next);

# make the jump between the seasons, missing out the season heading images. First, next:

if (-e "../august-photos/$nextpic"){
	$nextlink="<A HREF=\"../augustpics.cgi?&key=$next&key1=1\" class=\"chaptext\">";
	}
#elsif ($next<24) {
#		$next=24;
#		$nextlink="<A HREF=\"../augustpics.cgi?&key=$next&key1=0\" class=\"chaptext\">";
#	}
#elsif ($next<54) {
#	$next=54;
#	$nextlink="<A HREF=\"../augustpics.cgi?&key=$next&key1=0\" class=\"chaptext\">";
#	}
#elsif ($next<75) {
#	$next=75;
#	$nextlink="<A HREF=\"../augustpics.cgi?&key=$next&key1=0\" class=\"chaptext\">";
#	}
else {
	$nextlink="";
}


#Now previous:
#print qq($nextlink /n);
#print qq(back - $back, next - $next);

unless ($direct) {
#miss out the season titles
#	if (($back eq "24")||($back eq "54")||($back eq "75")) {
#		$back--;
#		}
	$backpic=$back.".jpg";

#work back to the last photo of the previous season
#	until (-e "../august-photos/$backpic"){
#		$back--;
#		$backpic=$back.".jpg";
#	}
}
#print qq(back - $back, next - $next);
$backlink="<A HREF=\"../augustpics.cgi?&key=$back&key1=0\" class=\"chaptext\">";


if ($back<1){
		$backlink="";
}


$heading="August 2013";
$indexlink="august-gal.cgi";
$altext="Photos from August, 2013";
#reset $picclean to use as index for photo titles
--$picclean;


print q(<!DOCTYPE HTML>
 <html lang="en">
<head>);

print qq(<title>Bob & Sue's Pages</title>);

print q(<STYLE TYPE="text/css">
@import url(../gallery.css);
</STYLE>
<meta charset="UTF-8">
<BASE href="pictures/" />
</head>
<body>);

print qq(<h1>$heading</h1>
	<h2>$title</h2>);

print qq(<div id="navlinks">$backlink Previous</A> <A HREF="../$indexlink" class="chaptext">Index</A> $nextlink Next</A></div>);
#print "<p> $back $1 $next </p>";

print qq(<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="../../index.html" class="chaptext">Home Page</a><br>
<a href="mailto:bob\@raoneill.com">Contact</a>
</div>
</td>
<td align = "left" valign="top">);

# change width for tall photos
if (($picnum eq "06")||($picnum eq "07")||($picnum eq "15")||($picnum eq "16")) {
	print qq (
	<table id="mainpictall">
	<tr><td><img src="/august-photos/$picbig" alt="$altext" ></td></td>);
} else {		#landscape format
	print qq (
	<table id="mainpic">
	<tr><td><img src="/august-photos/$picbig" alt="$altext" ></td></td>);
}
print qq(
</table>
</body>
</html>
</table>);
