<?php

// Penguin Gallery http://www.penguintutor.com/penguingallery 
// Copyright 2009 Stewart Watkiss
/*  
	This file is a part of Penguin Gallery

	Penguin Gallery is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Penguin Gallery is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Penguin Gallery.  If not, see <http://www.gnu.org/licenses/>.
*/

// Creates navigation buttons for gallery

$isrunning = false;

// Is this running standalone or as an include - if standalone
// - does $galleryconfigfile exist - if not then we are standalone (eg. AJAX) - if is we are embedded  
// standalone variable used later to determine if we need to close the <p> we create
if (!isset($galleryconfigfile)) // || ($galleryconfigfile eq ""))
{
	$standalone = 1;
	// If standalone get configfile from url
	$galleryconfigfile = trim($_GET[gallery]);
	$runningstatus = trim($_GET[running]);
	// Make
	if ($runningstatus == "yes") {$isrunning = true;}
	else {$isrunning = false;}
	// Call the setup code (this is included in gallery.php for embedded)
	// Note security checking i
	include ($galleryphpdir."gallerysetup.php");
}
else
{
	$standalone = 0;
	// We save galleryconfigfile as a js variable - so it can be used by 
	echo "<div id=\"gallerynavigationdiv\">\n\n";
}


// Get image number from the url
$imgnum = trim($_GET[img]);
// If not in the url then we show the first image 
// Put it so that image starts with 1 rather than 0 - as this makes more sense to the user
// Array is still held as 0 - so we need to subtract one from the user value to get the array position.
if (empty($imgnum) || !is_numeric($imgnum) || $imgnum < 1) {$imgnum = 1;}
// Already checked for imgnum being too small - now check it's not too large - if so make it the last image
if ($imgnum > sizeof($filenames)) {$imgnum = sizeof($filenames);}


// Create buttons
if ($imgnum > 1) 
	{
		$rewindbutton = "<a href=\"$thisurl&img=1\" class=\"gallerybuttonlink\" onclick=\"changeimg(1); return false;\"><img src=\"".$galleryimgdir."left-rewind.png\" class=\"gallerybuttonimgactive\" title=\"First image\" alt=\"Go to first image\" /></a>";
		$backbutton = "<a href=\"$thisurl&img=".($imgnum-1)."\" class=\"gallerybuttonlink\" onclick=\"changeimg($imgnum-1); return false;\"><img src=\"".$galleryimgdir."left.png\" class=\"gallerybuttonimgactive\" title=\"Previous image\" alt=\"Go to previous image\" /></a>";
	}
else 
	{
		$rewindbutton = "<img src=\"".$galleryimgdir."left-rewind.png\" class=\"gallerybuttonimgdisabled\"  title=\"No previous image\" alt=\"No previous image\" />";
		$backbutton = "<img src=\"".$galleryimgdir."left.png\" class=\"gallerybuttonimgdisabled\"  title=\"No previous image\" alt=\"No previous image\" />";
	}
if ($imgnum < sizeof($filenames)) 
	{
		$forwardbutton = "<a href=\"$thisurl&img=".($imgnum+1)."\" class=\"gallerybuttonlink\" onclick=\"changeimg($imgnum+1); return false;\"><img src=\"".$galleryimgdir."right.png\" class=\"gallerybuttonimgactive\" title=\"Next image\" alt=\"Go to next image\" /></a>";
		$fforwardbutton = "<a href=\"$thisurl&img=".sizeof($filenames)."\" class=\"gallerybuttonlink\" onclick=\"changeimg(".sizeof($filenames)."); return false;\"><img src=\"".$galleryimgdir."right-forward.png\" class=\"gallerybuttonimgactive\" title=\"Last image\" alt=\"Go to last image\" /></a>";
	}
else 
	{
		$forwardbutton = "<img src=\"".$galleryimgdir."right.png\" class=\"gallerybuttonimgdisabled\" title=\"No more images\" alt=\"No more images\" />";
		$fforwardbutton = "<img src=\"".$galleryimgdir."right-forward.png\" class=\"gallerybuttonimgdisabled\" title=\"No more images\" alt=\"No more images\" />";
	}

	
// If Create slideshowbutton disabled - then use javascript to replace it with an enabled button 
// This means we have tested to ensure that Javascript works before we enable it
if ($standalone == 0)
{
	$slideshowbutton = "<span id=\"slideshowbutton\"><img src=\"".$galleryimgdir."play.png\" class=\"galleryplaybuttonimgdisabled\" title=\"Javascript must be enabled for screenshow\" alt=\"Slideshow disabled\" /></span>";
}
else
{
	// play/pause button
	if ($isrunning) {$slideshowbutton = "<span id=\"slideshowbutton\"><a href=\"#\" class=\"gallerybuttonlink\" onclick=\"slideshowrunning=false;updateNavigation();return false;\"><img src=\"".$galleryimgdir."pause.png\" class=\"galleryplaybuttonimgactive\" title=\"Slideshow\" alt=\"Slideshow\" /></a></span>";}
	else {$slideshowbutton = "<span id=\"slideshowbutton\"><a href=\"#\" class=\"gallerybuttonlink\" onclick=\"slideshowrunning=true;slideshowRefresh();return false;\"><img src=\"".$galleryimgdir."play.png\" class=\"galleryplaybuttonimgactive\" title=\"Slideshow\" alt=\"Slideshow\" /></a></span>";}
}

// display buttons 
echo "<span id=\"gallerynavigationblock\">\n";
echo "$rewindbutton$backbutton$slideshowbutton$forwardbutton$fforwardbutton\n";
echo "</span>\n\n";

if ($standalone == 0) 
	{
	// If standalone (ie. not AJAX) end div section and enable javascript button
	echo "</div>\n\n";

	// Embed javascript to replace slideshow button with an active one
	// If code above changes for slideshow button then gallery.js will need to be changed as well
	echo "<script type=\"text/javascript\">updateNavigation();</script>\n";
	}
?>
