<?php

/*  Alternate WebKit
(C) 2011-2014 by Alternate Tools
-----------------------------------------------------

File:         site_displaypicture.php
Description:  Frontend module to display a gallerys picture

*/

    include "modules/class_gallery.php";
    include "modules/mod_site.php";

    $bGalLoadFailed = false;
    $errorString = "";
    
    // Get folder
    $galFolder = $GALLERIES_FOLDER;
    if (isset($_GET['folder']) && $_GET['folder'] != "")
        $galFolder = $_GET['folder'];
    else if (isset($_POST['gal_Folder']) && $_POST['gal_Folder'] != "")
        $galFolder = $_POST['gal_Folder'];

    $galGallery = new cGallery;
    $galGallery->SetFolder($galFolder);

    // Get Id
    $galId = "";
    if (isset ($_GET['id']) == true)
        $galId = $_GET['id'];
    else if (isset ($_POST['gal_id']) == true)
        $galId = $_POST['gal_id'];
    // Get count
    $galCount = 0;
    if (isset ($_POST['gal_Count']) == true)
        $galCount = $_POST['gal_Count'];
    else if (isset ($_GET['count']) == true)
        $galCount = $_GET['count'];
    // Align
    $galAlign = $ALIGN_LEFT;
    if (isset ($_POST['gal_Align']) == true)
        $galAlign = $_POST['gal_Align'];
    else if (isset ($_GET['align']) == true)
        $galAlign = $_GET['align'];
    // Text align
    $galTextAlign = $ALIGN_TOP;
    if (isset ($_POST['gal_TextAlign']) == true)
        $galTextAlign = $_POST['gal_TextAlign'];
    else if (isset ($_GET['textalign']) == true)
        $galTextAlign = $_GET['textalign'];


    // Close window?
    if (isset($_POST['gal_Cancel']) == true)
    {
        echo_r('<script language="JavaScript">close();</script>');
        return;
    }

    if ($galGallery->LoadGalleryFromId($galId) == false)
    {
        $errorString = get_translation('site_displayimage_image') . $galId . '!';
        $bGalLoadFailed = true;
    }

    if ($errorString != "")
        print_error_message($errorString);

//    $lastImage = $galGallery->GetMaxImages();
    $galGallery->SortForFrontEnd();
    $totalImages = 0;
    $firstImage = -1;
    $lastImage = -1;
    $currentImage = 0;
    for ($i = 0; $i < $galGallery->GetMaxImages(); $i ++)
    {
        if (file_exists(retrieve_absolute_path($galGallery->GetImage($i))) == true)
        {
            $totalImages ++;
            if ($firstImage == -1)
                $firstImage = $i;
            if ($i <= $galCount)
                $currentImage ++;
            $lastImage = $i;
        }
    }

    // Next image
    if (isset($_POST['gal_Next']) == true)
    {
        if ($galCount < $lastImage)
        {
            $galCount ++;
            $currentImage ++;
        }
        while (file_exists(retrieve_absolute_path($galGallery->GetImage($galCount))) == false && $galCount <= $lastImage)  // empty space between?
            $galCount ++;
    }
    // Prev image
    if (isset($_POST['gal_Prev']) == true)
    {
        if ($galCount > $firstImage)
        {
            $galCount --;
            $currentImage --;
        }
        while (file_exists(retrieve_absolute_path($galGallery->GetImage($galCount))) == false && $galCount >= $firstImage)  // empty space between?
            $galCount --;
    }

    echo_r('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">');
    echo_r('<html>');
    echo_r('<head><title>' . $SITE_TITLE . '</title>');
    echo_r('<link rel="stylesheet" type="text/css" href="theme.css">');
    echo_r('<meta name="author" content="Alternate Tools">');
    echo_r('<meta http-equiv="content-type" content="text/html; charset=' . $SITE_CHARSET_FORMAT . '">');
    echo_r('</head>');
    echo_r('<body>');
    // Error occured?
    if ($errorString != "")
        print_error_message($errorString);

    echo_r('<center>');
    echo_r('<form method="post" action="">');
    echo_r('<table class="preview_tbl" width="70%" cellspacing="0">');
    echo_r('  <tr class="preview_tbltr" id="preview_tbltr1">');
    echo_r('    <td class="preview_tbltd" id="preview_tbltd1" align="center">');
    echo_r('    <table class="preview_inner_tbl" width="100%" cellspacing="0">');
    echo_r('      <tr class="preview_inner_tbltr">');
    echo_r('        <td class="preview_inner_tbltd" id="preview_inner_tbltd1" width="25%" align="center">' . get_translation('site_displayimage_image') . ' ' . ($currentImage) . ' ' . get_translation('site_displayimage_of') . ' ' . ($totalImages) . '</td>');
    echo_r('        <td class="preview_inner_tbltd" id="preview_inner_tbltd2" width="50%" align="center">');
    if ($galCount != $firstImage)
    {
        echo_r('        <input class="preview_btn" id="preview_btn_prev" type="submit" name="gal_Prev" value="' . get_translation('site_displayimage_previous') . '">');
    }
    if ($galCount != $lastImage)
    {
        echo_r('        <input class="preview_btn" id="preview_btn_next" type="submit" name="gal_Next" value="' . get_translation('site_displayimage_next') . '">');
    }
    echo_r('        </td>');
    echo_r('        <td class="preview_inner_tbltd" id="preview_inner_tbltd3" align="center"><input class="preview_btn"  id="preview_btn_close" type="submit" name="gal_Cancel" value="' . get_translation('site_displayimage_closewindow') . '"></td>');
    echo_r('      </tr>');
    echo_r('    </table>');
    echo_r('    </td>');
    echo_r('  </tr>');
    echo_r('  <tr class="preview_tbltr" id="preview_tbltr2">');
    if ($galTextAlign == $ALIGN_TOP)
    {
        echo_r('    <td class="preview_tbltd" id="preview_tbltd2" align="' . $galAlign . '" style="vertical-align: bottom">');
        if ($galGallery->ShowDescriptions() == true)
            echo_r($galGallery->GetDescription($galCount) . '<br>');
    }
    else
        echo_r('    <td class="preview_tbltd" id="preview_tbltd2" align="' . $galAlign . '" style="vertical-align: top">');

    $imageUrl = retrieve_absolute_path($galGallery->GetImage($galCount));
    if (file_exists($imageUrl) == true)
    {
        echo_r('    <img class="display_image" src="' . $imageUrl . '" alt="' . $galGallery->GetDescription($galCount) . '">');
    }
    if ($galTextAlign != $ALIGN_TOP && $galGallery->ShowDescriptions() == true)
        echo_r('<br>' . $galGallery->GetDescription($galCount));
    echo_r('    </td>');
    echo_r('  </tr>');
    echo_r('</table>');

    echo_r('  <input type="hidden" value="' .  $galId . '" name="gal_id">  ');
    echo_r('  <input type="hidden" value="' .  $galFolder . '" name="gal_Folder">  ');
    echo_r('  <input type="hidden" value="' .  $galCount . '" name="gal_Count">  ');
    echo_r('  <input type="hidden" value="' .  $galAlign . '" name="gal_Align">  ');
    echo_r('  <input type="hidden" value="' .  $galTextAlign . '" name="gal_TextAlign">  ');
    echo_r('</form>');
    echo_r('</center>');

    echo_r('</body></html>');
?>