package ShowPhotoEJH;

use strict;
use vars qw( @ISA @EXPORT $VERSION );
use Exporter;
use Apache qw( exit );

$VERSION = '1.00';
@ISA = qw( Exporter );
@EXPORT = qw( do_showphoto );

sub do_showphoto {
	my $title = shift;
	my $commentpattern = shift;

	my $photo = 1;
	my $data = '';
	my $next;
	my $previous;
	my $photoname;
	my $commentname;
	my @photos = ();

	my $r = Apache->request;

	$r->content_type('text/html');
	$r->no_cache();
#	$r->send_http_header();

	if ( $r->method() ne 'GET' ) {
	        print '<html><body>Error: use the GET method.</body></html>\n';
	        exit 0;
	}

	unless ( opendir( DIR, '.') ) {
	        print '<html><body>Error: can\'t read current directory.</body></html>\n';
	        exit;	
	}
	@photos = sort grep { /\.jpg$/i } readdir(DIR);
	closedir DIR;

	my $max = scalar @photos;			# number of photos
	unshift @photos, "";				# so offset 1 is 1st photo

	my %args = $r->args;

	if ( exists $args{photo} and $args{photo} ne '' ) {
		$photo = $args{photo};
	}

	if ( $photo < 1 ) {
		$photo = 1;
	} elsif ( $photo > $max ) {
		$photo = $max;
	}

	$next = $photo + 1;
	$previous = $photo - 1;
	
	if ( $previous == 0 ) {
		$previous = 1;
	} elsif ( $next == $max + 1 ) {
		$next = $max;
	}

	$photoname = $photos[$photo];

	$photoname =~ /^(.*)\.jpg$/i;

	$commentname = sprintf($commentpattern, $1);

	if ( exists $args{data} ) {
		$data = $args{data};
		open OUT, ">$commentname" or die "Cannot write comments directory";
		print OUT $data;
		close OUT;
	} elsif ( -f $commentname ) {
		open IN, $commentname;
		$data=join('',<IN>);
		close IN;
	}


#
# #################################################################
#

	print <<"EOT"
<html>
<head>
<title>
$title; $photoname
</title>
<script language="javascript">
<!--

function setstatus(status) {
	self.status=status;
	return false;
}

//-->
</script>
</head>
<body bgcolor="#000051" text="white" link="white" vlink="white">
<dl>
<dd>
	<form name="updateform" method="get" action="index.pl">
	<input type="hidden" name="photo" value="$photo">
		<table border="1">
		<tr>
		<td>
			<table border="0" cellspacing="5" cellpadding="5" align="center">
			<tr>
			<td colspan="5">
			<a href="index.pl?photo=$next" onMouseOver="return setstatus('Next Photo');">
			<img src="$photoname" width="50%"></a>
			</td>
			</tr>
			<tr>
			<td colspan="5" align="center">
			<textarea name="data" rows="3" cols="50" wrap="hard">$data</textarea>
			</td>
			</tr>
			<tr>
			<td>
			<a href="index.pl?photo=$previous" onMouseOver="return setstatus('Previous Photo');">
			<img src="/icons/arrowleft.gif" border="0"></a>
			</td>
			<td align="center">
			<input type="button" name="update" onClick="document.updateform.submit()" value="Update Description">
			</td>
	</form>
	<form name="photoform" method="get" action="index.pl">
			<td align="center">
			Nr&nbsp;<input type="text" name="photo" size="3" value="$photo">&nbsp;of&nbsp;$max&nbsp;
			<input type="button" name="show" onClick="document.photoform.submit()" value="Show">
			</td>
			<td align="right">
			<a href="index.pl?photo=$next" onMouseOver="return setstatus('Next Photo');">
			<img src="/icons/arrowright.gif" border="0"></a>
			</td>
			</tr>
			</table>
		</td>
		</tr>
		</table>
	</form>
</dd>
</dl>
</body>
</html>
EOT
	; 

}

1;
