# Simple Photo Gallery: Web Interface
# (c) 2003--2005 Martin Mares <mj@ucw.cz>

package Gallery::Web;
import Gallery qw(%CF);

use UCW::CGI;

my $show_img;
my $send_archive;

my %args = (
	'i'	=> { 'var' => \$show_img, 'check' => '\d+' },
	'a'	=> { 'var' => \$send_archive }
);

sub new() {
	UCW::CGI::parse_args(\%args);
	if ($send_archive && $CF{'AllowArchives'}) {
		require Gallery::Archive;
		return new Gallery::Archive($send_archive);
	} else {
		return bless {};
	}
}

sub error($) {
	print "<p style='color:red'>Bad luck, the script is broken. Sorry.\n<p>$_[0]\n";
	print "</body></html>\n";
}

our (@images, %widths, %heights, %widths_orig, %heights_orig, %imgno);

sub load_notes() {
	@images = ("");
	%widths = ();
	%heights = ();
	%imgno = ();
	my $notes = $CF{'MetaDataDir'} . "/notes";
	open NOTES, $notes or die "Unable to read notes ($notes): $!";
	while (<NOTES>) {
		chomp;
		my ($n, $w, $h, $wo, $ho) = split /\s+/;
		push @images, $n;
		$widths{$n} = $w;
		$heights{$n} = $h;
		$widths_orig{$n} = $wo;
		$heights_orig{$n} = $ho;
		$imgno{$n} = $#images;
	}
	close NOTES;
}

sub show_links($$$) {
	my ($prev, $up, $next) = @_;
	print "<p class=parent>";
	print "<span class=back style='width: $CF{'BackW'}px; height: $CF{'BackH'}px'>";
	print "<a href='$prev'><img src='$CF{'BackImg'}' width=$CF{'BackW'} height=$CF{'BackH'} alt='Back'></a>" if $prev ne "";
	print "</span>\n";
	print "<span class=fwd style='width: $CF{'FwdW'}px; height: $CF{'FwdH'}px'>";
	print "<a href='$next'><img src='$CF{'FwdImg'}' width=$CF{'FwdW'} height=$CF{'FwdH'} alt='Forward'></a>" if $next ne "";
	print "</span>\n";
	print "<a href='$up'><img src='$CF{'ParentImg'}' width=$CF{'ParentW'} height=$CF{'ParentH'} alt='Up'></a>\n" if $up ne "";
}

sub Start($) {
	my $title = UCW::CGI::html_escape($CF{"Title"});
	print <<EOF ;
Content-Type: text/html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head>
$CF{"HeadExtras"}
<link rel=stylesheet href="$CF{"StyleSheet"}" type="text/css" media=all>
<title>$title</title>
</head><body>
$CF{"TopExtras"}
EOF
	$UCW::CGI::error_hook = \&error;
	load_notes();
	if ($show_img ne "" && $show_img > 0 && $show_img <= $#images) {
		show_links(($show_img > 1 ? ("?i=".($show_img-1)) : ""),
			   "?",
			   ($show_img < $#images ? ("?i=".($show_img+1)) : ""));
	} else {
		show_links($CF{'BackURL'}, $CF{'ParentURL'}, $CF{'FwdURL'});
		print "<h1>$CF{'Title'}</h1>\n";
		print "<h2>$CF{'SubTitle'}</h2>\n" if defined $CF{'SubTitle'};
	}
}

sub Finish($) {
	print "$CF{'BotExtras'}\n</body></html>\n";
}

sub RawHTML($$) {
	my ($obj, $h) = @_;
	print $h if $show_img eq "";
}

sub img($$$) {
	my ($this, $orig, $annot) = @_;
	my ($base, $ext) = ($orig =~ /^(.*)\.([^.]+)$/) or die "Unable to dissect name $orig";
	$annot = UCW::CGI::html_escape($annot);
	my $id = $imgno{$base} || 0;

	if ($show_img ne "") {
		if ($id == $show_img) {
			print "<h1>$annot</h1>\n" if $annot ne "";
			my $img = $CF{'PhotoUrlPrefix'} . $orig;
			print "<p class=large><img src='$img' width=$widths_orig{$base} height=$heights_orig{$base} alt='$orig'>\n";
		}
		return;
	}

	my $thumb = $CF{'ThumbUrlPrefix'} . "$base-thumb.jpg";
	my $w = $widths{$base} || 10;
	my $h = $heights{$base} || 10;
	my $side_w = $CF{"ThumbW"} + 2*$CF{"InteriorMargin"};
	my $side_h = $CF{"ThumbH"} + 2*$CF{"InteriorMargin"};
	my $box_w = $CF{"LeftW"} + $side_w + $CF{"RightW"};
	my $box_h = $CF{"TopH"} + $side_h + $CF{"BotH"};
	print "<div class=thf><div class=thumb>\n";
	print "<img src='$CF{'TopImg'}' width=$box_w height=$CF{'TopH'} alt='' class=tt>\n";
	print "<img src='$CF{'LeftImg'}' width=$CF{'LeftW'} height=$side_h alt='' class=tl>\n";
	my $ol = $CF{'LeftW'} + $CF{'InteriorMargin'} + int(($CF{'ThumbW'} - $w)/2);
	my $ot = $CF{'TopH'} + $CF{'InteriorMargin'} + int(($CF{'ThumbH'} - $h)/2);
	my $tit = ($annot ne "") ? " title=\"$annot\"" : "";
	my $url = ($CF{"ImageSubpages"} ? "?i=$id" : $orig);
	print "<a href='$url'><img src='$thumb' width=$w height=$h alt='$orig'$tit class=ti style='left: ${ol}px; top: ${ot}px'></a>\n";
	print "<img src='$CF{'RightImg'}' width=$CF{'RightW'} height=$side_h alt='' class=tr>\n";
	print "<img src='$CF{'BotImg'}' width=$box_w height=$CF{'BotH'} alt='' class=tb>\n";
	print "</div>\n";
#	if ($annot ne "") {
#		print "<p class=annot>$annot\n";
#	}
	print "</div>\n\n";
}

1;
