# Highslide JS Theme for MJ's Photo Gallery
# (c) 2012 Martin Mares <mj@ucw.cz>; GPL'ed

package UCW::Gallery::Web::HighSlide;

use common::sense;
use utf8;

use UCW::Gallery;
use UCW::Gallery::Web;

our @ISA = qw(UCW::Gallery::Web);

sub theme_head_extras($) {
	my ($self) = @_;
	my $hsdir = $self->get('ThemeUrlPrefix') . "highslide";
	return $self->showing_image ? <<AMEN_MINI : <<AMEN_FULL ;
<link rel="stylesheet" type="text/css" href="$hsdir/custom.css">
AMEN_MINI
<script type="text/javascript" src="$hsdir/highslide-with-gallery.js"></script>
<script type="text/javascript" src="$hsdir/custom.js" charset="utf-8"></script>
<script type="text/javascript">
hs.graphicsDir = '$hsdir/graphics/';
</script>
<link rel="stylesheet" type="text/css" href="$hsdir/highslide.css">
<link rel="stylesheet" type="text/css" href="$hsdir/custom.css">
<!--[if lt IE 7]>
<link rel="stylesheet" type="text/css" href="$hsdir/highslide-ie6.css">
<![endif]-->
AMEN_FULL
}

sub show_links($$$$) {
	my ($self, $prev, $up, $next) = @_;
	my $nav = $self->get('ThemeUrlPrefix') . "highslide/nav";
	print "<p class=parent>";
	print "<a href='$prev'><img class=back prev src='$nav/prev.png'></a>" if $prev ne "";
	printf "<a href='$next'><img class=fwd src='$nav/next.png'></a>" if $next ne "";
	printf "<a href='$up'><img class=up src='$nav/back.png'></a>" if $up ne "";
}

sub show_pre_thumbs($) {
	my ($self) = @_;
	print "\n<div class='highslide-gallery'><ul>\n";
	$self->{hs_thumb_counter} = 0;
}

sub show_post_thumbs($) {
	my ($self) = @_;
	print "</ul></div>\n\n";
}

sub show_thumb($) {
	my ($self, $meta, $photo_id, $click_url) = @_;
	my $m = $meta->{photo}->{$photo_id};
	my $annot = UCW::CGI::html_escape($m->{title});
	my $tf = $self->get('ThumbFormats')->[0];
	my $tm = $meta->{thumb}->{$tf}->{$photo_id} or die "No thumbnails for format $tf found!\n";
	my $tw = $tm->{w};
	my $th = $tm->{h};
	my $thumb = $self->get('ThumbUrlPrefix') . "$tf/$photo_id.jpg";
	# Highslide requires title either for all images, or for none
	my $tit = " title=\"$annot\"";
	my $aid = 'i'.(++$self->{hs_thumb_counter});
	my $photo_url = $self->get('PhotoUrlPrefix') . $self->{gal}->photo_file_name($m, $photo_id);
	print "<li><a id='$aid' href='$click_url' class=highslide onclick='return hs.expand(this, { src: \"$photo_url\" })'>";
	print "<img src='$thumb' width=$tw height=$th alt='Photo'$tit></a>\n";
	if ($self->get('GeoHack')) {
		my ($lat, $lon) = ($m->{lat}, $m->{lon});
		if (defined $lat && defined $lon) {
			my $local = "<a href='map.cgi?i=" . $self->{hs_thumb_counter} . "'>trasa</a>";
			my $osm = "<a href='http://www.openstreetmap.org/?mlat=$lat&amp;mlon=$lon#map=16/$lat/$lon'>OSM</a>";
			print "<div class='highslide-caption'>Ukázat na mapě: $local, $osm</div>\n";
		}
	}
}

sub run($$) {
	my ($class, $gal) = @_;
	my $self = $class->SUPER::attach($gal);
	$self->dispatch();
	return $self;
}

1;
