#!/usr/bin/perl5
## Copyright (c) 2001 by FUNIX BV All rights reserved.
##   PROJECT
##     picbrows
##   NAME
##     $Id: slideshow.pl,v 1.4 2007/02/01 08:30:30 maarten Exp $
##   DESCRIPTION
##     Show slideshow of gallery
package slideshow_pl;

use strict;
use vars qw(%g);

use CGI;
$CGI::POST_MAX=1;     # no posts
$CGI::DISABLE_UPLOADS = 1;  # no file uploads
use CGI::Carp qw(fatalsToBrowser);

require Gen::DBIWrap;
require IniConf;

if (!$ENV{'FASTCGIHANDLER'}){
   unshift(@INC,'../lib');
}

require PBGeneric;
require PBGen;

if (!$ENV{'FASTCGIHANDLER'}){
   my $lCgi=new CGI;
   my $lCfg=new IniConf('../conf/picbrows.ini');
   main([$lCgi,$lCfg]);
}

return 1;

sub main
{
   my ($aParams)=@_;
   
   %g=(
      'cgi'=>$aParams->[0],
      'cfg'=>$aParams->[1],
      'dbh'=>undef,
      'now'=>new CDateTime
   );
   my %actions=(
      'default'=>\&show
   );
   my $action=$g{cgi}->param('a');
   if (!$action || !exists $actions{$action}){
      $action='default';
   }
   $g{'dbh'}=Gen::DBIWrap::openConnection($g{'cfg'}->get('db_connection'));
   &{$actions{$action}}();
   $g{dbh}->disconnect();
}

sub show
{
   my $template=PBGeneric::loadTempl($g{cfg},'slideshow.html',1);

   my $lGalleryId=$g{'cgi'}->param('g') || 0;
   my $lGallery=new Gallery($lGalleryId);
   if (!$lGallery->read($g{'dbh'})){
      die "Illegal action!";
   }

   my $lPictureList=new PictureList;
   $lPictureList->readByGallery($g{'dbh'},$lGalleryId);
   my $lPictures=$lPictureList->pictures();

   my $lPictureNr=$g{'cgi'}->param('p') || 0;
   if (!defined ${$lPictures}[$lPictureNr]){
      # try first picture
      $lPictureNr=0;
   }
   # start at page nr
   if ($lPictureNr==0){
      my $lPage=$g{'cgi'}->param('page')||0;
      if ($lPage>0) {
         $lPage-=1;
         $lPictureNr=$lPage*$g{'cfg'}->get('pic_nr_cols')*$g{'cfg'}->get('pic_nr_rows');
      }
   }
   if (defined ${$lPictures}[$lPictureNr]){
      $lGallery->setTemplate($template);

      my $lPicture=${$lPictures}[$lPictureNr];
      $lPicture->read($g{'dbh'});
      $lPicture->setTemplate($template);

      setSlideshowTemplate($template,$lPictureNr,$#{$lPictures}+1);
      PBGen::setDirectoryTemplate($template,$g{'dbh'},$lGalleryId);
      PBGeneric::showTempl($template);
   } else {
      # still not defined
      die "Illegal action!";
   }
}

sub setSlideshowTemplate
{
   my ($template,$aIndex,$aTotal)=@_;

   my $refresh=$g{'cgi'}->param('refresh')||0;
   # if last picture then don't refresh and don't show next
   if ($aIndex>=($aTotal-1)){
      $refresh=0;
      $template->SetVar('SlideshowRefreshTime',$refresh);
      $template->SetVar('SlideshowNext','');
   } else {
      $template->SetVar('SlideshowRefreshTime',$refresh);
      $template->SetVar('SlideshowPicNr',($aIndex+1));
      $template->Parse('SlideshowNext','False');
   }
   # if first picture don't show previous
   if ($aIndex==0){
      $template->SetVar('SlideshowPrevious','');
   } else {
      $template->SetVar('SlideshowPicNr',($aIndex-1));
      $template->Parse('SlideshowPrevious','False');
   }

   if ($refresh>0){
      $template->SetVar('SlideshowPicNr',($aIndex+1));
      $template->Parse('SlideshowRefresh','False');
   } else {
      $template->SetVar('SlideshowRefresh','');
   }
   $template->SetVar('SlideshowPicNr',$aIndex);
}