<?
require 'admin/parsequery.php';

function DisplaySingleImage( $FolderName, $Index )
{
  require 'admin/admin.php';	// Gets the header and footer info.
  $TextFont = GetTextFont();
  $CaptionFileName = GetCaptionFileName();

  // Include any html header data that is required.
  if ( file_exists( GetImageHeaderName() ) )
    if ( !readfile( GetImageHeaderName() ) )
    {
      print( "<html>\n<body>\n" );
    }
  
  // get a list of eligible files in the folder...
  $Dir = opendir( $FolderName );
  $Extension = "";
  while( $FileName = readdir( $Dir ) )
  {
     if ( strstr( $FileName, "." ) )
	 {
	     $Extension = explode( ".", $FileName );
    	 if ( ( $Extension[1] == "jpg" or 
  	    	    $Extension[1] == "gif" or
		    	$Extension[1] == "png") and
			( !strncmp( $Extension[ 0 ], "th-", 3 ) ) )
    		 { 
			   // Only add on graphics files that start with th- (the thumbnails))
		       $FileList[] = $FileName;
		     }
	 }
  }
  closedir( $Dir );
  sort( $FileList );

  // There may also be individual file descriptions.	
  // Load up an array of these
  if ( file_exists( $FolderName."/". $CaptionFileName ) )
  {
    $CaptionFile = fopen( $FolderName."/". $CaptionFileName, "r" );
    if ( $CaptionFile )
    {
	  while ($buf = fgetcsv ($CaptionFile, 1000, ",") )
      {
	    $Captions[ $buf[ 0 ] ] = $buf[ 1 ];
      }
    }
  }
  
  $i = array_search( "th-".$Index, $FileList );
  $File = $FileList[ $i ];
  $BigFile = substr ($File, 3 );
	
  if ( isset( $Captions ) && $Captions[ $Index ] !== NULL )
	$Caption = $Captions[ $Index ];
  else
	$Caption = $BigFile;

  
  $IndexStr = sprintf( " Picture %d of %d: ", $i+1, count( $FileList ) );
  if ( $i == 0 ) 
    $IndexStr .= sprintf( " &lt&lt First - &ltPrevious - " );
  else
  {
    $IndexStr .= sprintf( "<a href=\"showsingleimage.php?FolderName=%s&Index=%s\"> &lt&ltFirst </a> - ",
	        $FolderName, substr( $FileList[0], 3 ) );
    $IndexStr .= sprintf( "<a href=\"showsingleimage.php?FolderName=%s&Index=%s\"> &ltPrevious</a> - ",
	        $FolderName, substr( $FileList[$i-1], 3 ) );
  }
  
  $IndexStr.= "<a href=\"showimages.php?folder=$FolderName\">Index</a> - ";
  
  if ( $i+1 == count( $FileList ) )
    $IndexStr .= sprintf( " Next&gt> - Last&gt&gt" );
  else
  {
    $IndexStr .= sprintf( " <a href=\"showsingleimage.php?FolderName=%s&Index=%s\">Next&gt</a> - ",
	        $FolderName, substr( $FileList[$i+1], 3 ) );
    $IndexStr .= sprintf( " <a href=\"showsingleimage.php?FolderName=%s&Index=%s\">Last&gt&gt</a> ",
	        $FolderName, substr( $FileList[count( $FileList )-1], 3 ) );
  }


  printf( "<p align=\"center\">$TextFont". $IndexStr."</p>\n" );
  printf( "<p align=\"center\">%s%s</p>\n", $TextFont, $Caption );
  printf( "<p align=\"center\"><img src=\"%s%s%s\"></a></p>\n",
		  $FolderName, "/", $BigFile );
//  printf( "<p align=\"center\"><a href=\"%s%s%s\"><img src=\"%s%s%s\" width=\"50%%\"></a></p>\n",
//		  $FolderName, "/", $BigFile, $FolderName, "/", $BigFile );

  // Include any html header data that is required.
  if ( file_exists( GetImageFooterName() ) )
    if ( !readfile( GetImageFooterName() ) )
    {
      print( "</body>\n</html>\n" );
    }
}

//$folder = "ausreports/19810000hville";
if ( isset( $Index ) && isset( $FolderName ) )
  DisplaySingleImage( $FolderName, $Index )

?>