#!/usr/bin/perl -w

package Gallery;

use strict;
use warnings;

use mmHTMLGen;
use TreeMenu;
use Carp;

sub new
{
    my $class = shift;
    my $this = {};

    $this->{ 'htops' } = {  'lang' => 'cs', 'charset' => 'iso-8859-2', 
                            'title' => 'Gallery contents',
                            'author' => 'Martin Matousek', 'css' => 'top.css',
                            'rawhead' => "<base target=\"body\">\n",
                        };

    $this = bless( $this, ref( $class ) || $class );

    return $this;
}


sub ScalarChildren
{
    my $this = shift;
    my $Tree = shift;

    return [ 'files' => $Tree->{'children'} ];
}

sub Contents
{
    my $this = shift;
    my $seltx = shift;
    my $galfile = shift;
    my $args = shift;
    my @sel = split( '/', $seltx );

    HTMLPrintHead(%{$this->{'htops'}});
    print "<body>";

    $this->{'args'} = $args;
    my $tree = do( $galfile ) or 
        croak "Read of $galfile not successful ($@)";

    $tree = new TreeMenu( $tree, $this );
    $tree->Process( \@sel );

    $this->Options( $args );
    print "</body>\n</html>\n";
}

sub ChildArray
{
    my $this = shift;
    my $tree = shift;

    return { 'title' => $tree->[0], 'children' => "$tree->[1]/*.jpg" };
}
    

sub Options
{
    my $this = shift;
    my $args = $this->{'args'};
    
# options tn, full
    print "<hr>\n";
    my %nargs;

    %nargs = %{$args};
    $nargs{'tn'} = !$nargs{'tn'};
    print( "<a href=\"". $this->MenuHref($args->{'lastnode'}, \%nargs) .
           "\" target=\"_self\">[" . 
           ( $nargs{'tn'} ? 'thumbs' : 'no thumbs' ) . "]</a>" );

    %nargs = %{$args};
    $nargs{'full'} = !$nargs{'full'};
    print( "<a href=\"". $this->MenuHref($args->{'lastnode'}, \%nargs) .
           "\" target=\"_self\">[" . 
           ( $nargs{'full'} ? 'full size' : 'normal size' ) . "]</a>" );

    if( defined( $args->{'back'} ) )
    {
        print "<br><a href=\"$args->{'back'}\" target=\"_parent\">[back]</a>\n";
    }

}

sub Node
{
    my $this = shift;
    my $tree = shift;
    my $opts = $this->{'args'};
#    print STDERR ref( $this ), "<<\n";

    print( ( '&nbsp;&nbsp;' x ( $tree->{'level'} ) ) . 
           "<a class=node href=\"". $this->MenuHref($tree, $opts) .
           "\" target=\"_self\">" .
           "<img align=bottom border = 0 src=\"gr/open.gif\">" .
           "$tree->{'title'}</a><br>\n" );
    $opts->{'lastnode'} = $tree;
    if( $opts->{'tn'} )
    {
        exists( $tree->{'tnpre'} ) and $opts->{'tnpre'} = $tree->{'tnpre'};
        exists( $tree->{'tnpost'} ) and $opts->{'tnpost'} = $tree->{'tnpost'};
    }
    if( $opts->{'full'} )
    {
        exists( $tree->{'fullpre'} ) and $opts->{'pre'} = $tree->{'fullpre'};
        exists( $tree->{'fullpost'} ) and $opts->{'post'} = $tree->{'fullpost'};
    }
}

sub Leaf
{
    my $this = shift;
    my $tree = shift;
    my $opts = $this->{'args'};
    print( ( '&nbsp;&nbsp;' x ( $tree->{'level'} ) ) . 
           "<a class=node href=\"". $this->MenuHref($tree, $opts) .
           "\" target=\"_self\">" .
           "<img align=bottom border = 0 src=\"gr/closed.gif\">" .
           "$tree->{'title'}</a><br>\n" );
}

sub text {  
    my $this = shift;
    print "<p class=comment>$_[0]</p>\n"; }

sub head {
    my $this = shift;
    print "<p class=gcaption>$_[0]</p>\n"; }

sub files
{
    my $this = shift;
    my $files = shift;
    my $tree = shift;
    my $opts = $this->{'args'};
    local *GFFILE;
    my $path = $tree->{'path'};

    if( ref( $files ) ) # pairs 'name' - 'filename'
    {
    }
    else
    {
        ($path, $files) = Repath( $path, $files );

        open GFFILE, "find $path -maxdepth 1 -iname \"$files\" | sort |";
        if( !eof( GFFILE ) )
        {
            while( <GFFILE> )
            {
                chomp;
                Image( $_, $opts );
            }
        }
        close GFFILE;
    }
}


sub Repath
{
    my ($path, $file) = @_;

    $file =~ /\/([^\/]+)$/ and do { $path .= $` . '/'; $file = $1; };
    return ($path, $file );

}


sub Image
{
    my $image = shift;
    my $opts = shift;

    $image =~ /([^\/]*)(\.[^\.]+)$/;
    my $name = $1;
    my $pre = $`;
    my $suf = $2;

    $image = "\"" . $pre . ( $opts->{ 'pre' } || '' ) . $name . 
        ( $opts->{ 'post' } || '' ) . $suf . "\"";

    if( $opts->{ 'tnpre' } || $opts->{ 'tnpost' } )
    {
        my $tn = "\"" . $pre . ( $opts->{ 'tnpre' } || '' ) . $name . 
            ( $opts->{ 'tnpost' } || '' ) . $suf . "\"";

        printf "&nbsp;&nbsp;<a href=$image>" .
            "<img border=3 src = $tn class=\"thumbimg\"></a><br>\n";
        printf "&nbsp;&nbsp;<a href=$image class=\"caption\">" .
            "$name</a><br><br>\n";
    }
    else
    {
        printf "&nbsp;&nbsp;<a href=$image class=\"caption\">" .
            "$name</a><br>\n";
    }
}

sub StartLeaves{}
sub EndLeaves{}


1;
