<?php
if (isset($_GET["num"]))
    $num = intval($_GET["num"]);
else
{
    require_once("news.php");
    exit();
}

require_once("pagemaker.php");

$Page = new CPageMaker("templ/slide.tpl");
$Page->AddPathPart("Фотографии", "photos.php");

$index = 0;
if (isset($_GET["id"]))
    $id = intval($_GET["id"]);
    
class View extends CPageMaker
{   
    function View()
    {
        CPageMaker::CPageMaker("templ/view.tpl");
        $this->AddPathPart("Фотографии", "photos.php");
    }
    
    function GetPage($total, $album, $id, $name, $file, $descr, $created, $width, $height, $background, $prevId, $nextId, $num, $index)
    {
        if ($background != null)
            $this->Replace("%BACKGROUND%", "background-image: url('" . ROOT . "storage/img" . $background . "'); background-repeat: no-repeat; background-position: top left;");
        else
            $this->Replace("%BACKGROUND%", "");
            
        if ($prevId < 0)
            $this->Replace("%PREV_BTN%", "<img src='img/previous_disabled.gif' border='0'>");
        else
            $this->Replace("%PREV_BTN%", "<a href='view.php?num=" . intval($_GET["num"]) . "&id=" . $prevId ."'><img src='img/previous.gif' alt='Previous page' border='0'></a>");
            
        if ($nextId < 0)
            $this->Replace("%NEXT_BTN%", "<img src='img/next_disabled.gif' border='0'>");
        else
            $this->Replace("%NEXT_BTN%", "<a href='view.php?num=" . intval($_GET["num"]) . "&id=" . $nextId ."'><img src='img/next.gif' alt='Next page' border='0'></a>");
            
        $this->Replace("%TOTAL%", $total);
        $this->Replace("%URL_INDEX%", "album.php?num=" . intval($_GET["num"]) . "&ix=" . $index);
        $this->Replace("%IMG%", ROOT . "storage/img" . $file);
        if ($nextId >= 0)
            $this->Replace("%IMG_NEXT%", "view.php?num=" . intval($_GET["num"]) . "&id=" . $nextId);
        else
            $this->Replace("%IMG_NEXT%", "");
            
        $this->Replace("%IMG_NUM%", $num + 1);
        $this->Replace("%IMG_NAME%", $name);
        $this->Replace("%IMG_CREATED%", $created);
        $this->Replace("%IMG_COMMENT%", $descr);
        
        $this->Replace("%ALBUM_ID%", intval($_GET["num"]));
        $this->Replace("%IMAGE_ID%", $id);
                    
        return CPageMaker::GetPage();
    }   
}
    
require_once("be/db.php");
$rows = 3;
$cols = 4;

$total = GetVal("SELECT count(i.id) FROM album_img_map AS ai LEFT JOIN images AS i ON ai.image_id = i.id WHERE ai.album_id = " . $num, 0);

$res = Query("SELECT i.id from album_img_map ai LEFT JOIN images AS i ON ai.image_id = i.id WHERE ai.album_id = " . $num . " ORDER BY ai.number");
$imgNum = -1;
$i = 0;
$temp = -1;
$idPrev = -1;
$idNext = -1;
while ($row = mysql_fetch_row($res))
{
    if ($imgNum >= 0 && $idNext < 0)
        $idNext = $row[0];
        
    if ($row[0] == $id)
    {
        $imgNum = $i;
        $idPrev = $temp;
    }
    $i++;
    $temp = $row[0];
}
if ($imgNum < 0)
{
    require_once("news.php");
    exit();
}

$res = Query("SELECT i.name, i.filename, i.descr, DATE_FORMAT(i.created, '%d.%m.%Y %H:%i'), i.width, i.height, ia.filename, a.name FROM album_img_map AS ai " .
             "LEFT JOIN images AS i ON ai.image_id = i.id " .
              "LEFT JOIN albums AS a ON ai.album_id = a.id " .
              "LEFT JOIN images AS ia ON a.back = ia.id " .
               "WHERE ai.album_id = " . $num . " AND i.id = " . $id . " ORDER BY ai.number");

$Album = new View();
if ($row = mysql_fetch_row($res))
{
    $Page->Replace("%CONTENT%", $Album->GetPage($total, $num, $id, $row[0], $row[1], $row[2], $row[3], $row[4], $row[5], $row[6], $idPrev, $idNext, $imgNum, intval($imgNum / ($rows * $cols))));
    $Page->SetTitle($row[7]);
}
echo $Page->GetPage();
?>
