#!/usr/bin/perl -w
#################################################
# Program : gallery.pl                          #
# Author  : Neil Wallis                         #
# Date:   : 06/04/2005                          #
# Purpose : To present a categorised view of    #
#           my photos                           #
#################################################

use strict;
use diagnostics;
use CGI;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use IO::File;

my $fhHTML;
my $fhBODY;
my $cat;
my $catno;
my $fname;
my $desc;
my %catlist;
my $imagesperpage = 6;
my $imagesperrow = 6;

$fhHTML = new IO::File;
$fhBODY = new IO::File;

    my $q = new CGI;


# Retrieve parameter values

    my $catId = $q->param("catId");
    my $page  = $q->param("page");
    my $image = $q->param("image");

    if (!$page) { $page=1; }
    if (!$catId) { $catId=''; }
    if (!$image) { $image=''; }
    

# Main processing

    htmlBegin();

    galleryBegin();

    if ($catId) { displayCat(); }

    galleryEnd();

#------------------------------------------------------------------
# Display Category 
#------------------------------------------------------------------
sub displayCat
{
    my $maxpages;
    my @catimages;
    my $imagedesc;
    my $imagecount;
    my $nextpage;
 
    #-----------------------------------------------------------------
    # re-read the gallery file storing items for the selected category
    #-----------------------------------------------------------------
    open(GALLERY, "gallery.csv")|| die ("Can't open gallery file");
      while ( <GALLERY> ) 
        {
            ($catno, $cat, $fname, $desc) = split /,/, $_;
            if ($catno eq $catId) {
              push(@catimages,join("&",$fname,$desc));
              
	      #Store the image title
              if ($image eq $fname) {
                $imagedesc=$desc;
              }
            }
        }
    close(GALLERY);

    $imagecount=@catimages;

    $maxpages = int(($imagecount-1) / $imagesperpage)+1;

    if ($page > $maxpages or $page == 0) { $page=1; }

    $nextpage=$page+1;

    if ($nextpage > $maxpages) {$nextpage=$maxpages; }


    #--------------------
    # show navigation bar
    #--------------------

    printf "<td class='page_bodytext' valign='top'>Pages: &nbsp;";   
       printf "<a href='gallery.pl?catId=$catId&page=1&image=$image'>First</a>&nbsp;&nbsp;"; 
       for (my $i=1; $i<=$maxpages; $i++) {
          if ($i == $page) {
             printf "<b>$i</b>&nbsp;&nbsp;";
          } else {
             printf "<a href='gallery.pl?catId=$catId&page=$i&image=$image'>$i</a>&nbsp;&nbsp;";
          }
       }
       printf "<a href='gallery.pl?catId=$catId&page=$nextpage&image=$image'>Next</a>&nbsp;&nbsp;"; 
       printf "<a href='gallery.pl?catId=$catId&page=$maxpages&image=$image'>Last</a>"; 
    printf "<br><br>\n";


    # calculate the starting and ending indices
    my $firstimage = ($page-1) * $imagesperpage;
    my $lastimage = $firstimage + $imagesperpage - 1;
    if ($lastimage > @catimages-1) { $lastimage = @catimages-1; }
    
    #-------------------------------------------------------------------
    # display a grid of images for the current page within this category
    #-------------------------------------------------------------------
    printf "<table cellspacing='3' border='1' bordercolor='#BB7AF8' cellpadding='2'>\n";

    my $currentimage = $firstimage;
    
    for (my $a=0; $a< int(($imagesperpage-1)/$imagesperrow)+1; $a++) {
      printf "<tr>\n";
      for (my $b=0; $b<$imagesperrow; $b++) {
        printf "<td class='gallery' align='middle' bgcolor='#BB7AF8' valign='middle' width='95' height='95'>";
        if ($currentimage<=$lastimage) {
          ($fname,$desc) = split /&/, $catimages[$currentimage];
          
	  # If no image passed then assume first on page
	  if ($currentimage==$firstimage && !$image) {
            $image=$fname;
            $imagedesc=$desc;
	  }

	  if ($fname) {
            printf "<a href='gallery.pl?catId=$catId&page=$page&image=$fname'><img vspace='0' alt='$desc' border='0' src='/gallery/thumbs/$fname'></a>";
          } else {
            printf "&nbsp;";
          }
        }
        printf "</td>";
        $currentimage++;
      }
      printf "</tr>\n";
    }
    printf "<tr></tr>";

    printf "<tr><td valign='middle' align='middle' colspan='$imagesperrow'><br><b>$imagedesc</b><br><img src='/gallery/images/$image'></td></tr>";

    printf "</table>\n";



    printf "</td>";

}
#------------------------------------------------------------------
# Display page header 
#------------------------------------------------------------------
sub htmlBegin
{
	print header();
	print <<HEADER;
<html>
<head>
<title>wallisbrides.co.uk - Gallery</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="/wb.css" type="text/css">
</head>

<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<table border="0" width="100%" cellpadding="0" cellspacing="0" height="100%">
  <tr>
    <td width="140" height="100" bgcolor="#E50187"><img src="/images/pinkdot.jpg" width="140" height="100"></td>
    <td width="40" height="100" bgcolor="#E50187"><img src="/images/pinkdot.jpg" width="30"></td>
    <td height="100" align="right" bgcolor="#E50187"><img border="0" hspace="0" src="/images/horiz.gif" align="right" width="488" height="100"></td>
  </tr>
  <tr>
    <td width="140" height="40" bgcolor="#E50187"></td>    
    <td width="40"><img src="/images/corner.gif" width="40" height="40" hspace="0"></td>
    <td></td>
  </tr>
  <tr>
    <td width="140" bgcolor="#E50187" valign="top" align="center">
      <a href="/index.php"><img src="/images/home.jpg" alt="Home Page" border="0"></a><br />
      <a href="/products.php"><img src="/images/products.jpg" alt="Products and Services" border="0"></a><br />
      <a href="/contact.php"><img src="/images/contact.jpg" alt="Contact Details" border="0"></a><br />
      <a href="/feedback.php"><img src="/images/feedback.jpg" alt="Feedback" border="0"></a><br />
      <a href="/cgi-bin/gallery.pl"><img src="/images/gallery.jpg" alt="Image Gallery" border="0"></a><br />
      <a href="/shop.php"><img src="/images/shop.jpg" alt="Online Shop" border="0"></a><br />
    </td>
    <td width="40"></td>
    <td valign="top">
HEADER


}

#------------------------------------------------------------------
# Display opening gallery html  
#------------------------------------------------------------------
sub galleryBegin
{

    print<<STATIC;

        <table cellspacing="0" cellpadding="2">
          <tr> 
            <td valign="top" nowrap><h4>Categories</h4><br>
STATIC

        open(GALLERY, "gallery.csv")|| die ("Can't open gallery file");

        while ( <GALLERY> ) 
        {
            ($catno, $cat, $fname, $desc) = split /,/, $_;
            $catlist{join("&",$catno,$cat)} += 1;
        }

        close(GALLERY);

        my @cats = keys(%catlist);
        @cats = sort(@cats);
        @cats = reverse(@cats);

        foreach(@cats) {
           ($catno,$cat) = split /&/, $_;
	   if(!$catId) { $catId=$catno; }
           if($catno eq $catId) {
             printf "<b>$cat</b><br><br>";
           } else {
             printf "<a href='gallery.pl?catId=$catno'>$cat</a><br><br>";
           }
        }

        printf "</td><td width='10'>&nbsp;</td>";
}

#------------------------------------------------------------------
# Display closing gallery html  
#------------------------------------------------------------------
sub galleryEnd
{

    print<<STATIC;



          </tr>
        </table>

<center>
    <span class="footnote"><br><img src="/images/wb.gif" border="0"><br>
    This site &copy; 2007 Wallis Brides<br>
    Questions or comments about this site should be sent to the <a href="mailto:neil\@neilwallis.com">webmaster</a>.</span>
</center>
    </td>
  </tr>
</table>


   </body>
  </html>
STATIC
}