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

require_once("pagemaker.php");

$Page = new CBodyMaker();
$Page->AddPathPart("Фотографии", "photos.php");

$index = 0;
if (isset($_GET["ix"]))
    $index = intval($_GET["ix"]);
    
$rows = 3;
$cols = 4;

class AlbumIndex extends CPageMaker
{
    var $background = null;
    var $images = array();
    var $rows = 3;
    var $cols = 4;
    var $total = 0;
    var $album = 0;
    var $index = 0;
    
    function AlbumIndex($rows, $cols, $total, $album, $index)
    {
        CPageMaker::CPageMaker("templ/albumindex.tpl");
        $this->rows = $rows;
        $this->cols = $cols;
        $this->total = $total;
        $this->album = $album;
        $this->index = $index;
    }
    
    function SetBackground($img)
    {
        $this->background = $img;
    }
    
    function GetPage()
    {
        if ($this->background != null)
            $this->Replace("%BACKGROUND%", "background-image: url('" . ROOT . "storage/img" . $this->background . "'); background-repeat: no-repeat; background-position: top left;");
        else
            $this->Replace("%BACKGROUND%", "");
        
        $pages = intval(round(($this->total / ($this->rows * $this->cols) + 0.49)));
        if ($this->index >= $pages || $this->index < 0)
        {
            require_once("news.php");
            exit();
        }

        $indeces = "";        
        for ($i = 1; $i <= $pages; $i++)
        {
            if ($this->index == $i - 1)
                $indeces .= "<b>&nbsp;" . $i . "</b>";
            else
                $indeces .= "<a href='album.php?num=" . $this->album . "&ix=" . ($i - 1) . "'>" . "&nbsp;" . $i . "</a>";       
        }
        $this->Replace("%INDECES_NUMS%", $indeces);
        
        $bNext = false;
        $urlNext = "";
        
        if ($pages == 0)
        {
            $this->Replace("%PREV_BTN%", "");
            $this->Replace("%NEXT_BTN%", "");
        }
        else
        {
            if ($this->index == 0)
                $this->Replace("%PREV_BTN%", "<img src='img/previous_disabled.gif' border='0'>");
            else
                $this->Replace("%PREV_BTN%", "<a href='album.php?num=" . $this->album . "&ix=" . ($this->index - 1) . "'><img src='img/previous.gif' alt='Previous page' border='0'></a>");
                
            if ($this->index == $pages - 1)            
                $this->Replace("%NEXT_BTN%", "<img src='img/next_disabled.gif' border='0'>");
            else
            {
                $bNext = true;
                $urlNext = "album.php?num=" . $this->album . "&ix=" . ($this->index + 1);
                $this->Replace("%NEXT_BTN%", "<a href='" . $urlNext . "'><img src='img/next.gif' alt='Next page' border='0'></a>");
            }
        }
        
        for ($r = 0; $r < $this->rows; $r++)
        {
            if ($r * $this->cols >= count($this->images))
                break;
            $content .= "<tr>";
            for ($c = 0; $c < $this->cols; $c++)
            {
                $content .= "<td class = 'imageback'>";
                if (($r * $this->cols + $c) >= count($this->images))
                    $content .= "&nbsp;";
                else
                {
                    $content .= "<a href='view.php?num=" . $this->album . "&id=" . $this->images[$r * $this->cols + $c]["id"] . "'>";
                    $content .= "<img class = 'image' src='" . ROOT . "storage/thumb/" . $this->images[$r * $this->cols + $c]["thumb"] . "' border='0'><br/>";
                    $content .= "</a>";
                }
                $content .= "</td>";
            }
            $content .= "</tr>";
            
            $content .= "<tr>";
            for ($c = 0; $c < $this->cols; $c++)
            {
                $content .= "<td class = 'imagename'>";
                if (($r * $this->cols + $c) >= count($this->images))
                    $content .= "&nbsp;";
                else
                {
                    $content .= "<a href='view.php?num=" . $this->album . "&id=" . $this->images[$r * $this->cols + $c]["id"] . "'>";
                    $content .= $this->images[$r * $this->cols + $c]["name"];
                    $content .= "</a>";
                }
                $content .= "</td>";
            }
            $content .= "</tr>";
        }
        
        $this->Replace("%INDEX%", $content);
            
        return CPageMaker::GetPage();
    }
    
    function AddImage($id, $thumb, $name)
    {    
        $this->images[] = array("id" => $id, "thumb" => $thumb, "name" => ($name == null ? "&nbsp;" : $name));
    }
}
    
require_once("be/db.php");
$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.name, i.thumbfilename, i.id FROM album_img_map AS ai LEFT JOIN images AS i ON ai.image_id = i.id WHERE ai.album_id = " . $num . " ORDER BY ai.number limit " . ($rows * $cols * $index) . ", " . ($rows * $cols * ($index + 1)));
$Album = new AlbumIndex($rows, $cols, $total, $num, $index);
while ($row = mysql_fetch_row($res))
{
    $Album->AddImage($row[2], $row[1], $row[0]);
}
$res = Query("SELECT a.name, a.back FROM albums AS a WHERE a.id = " . $num);
if ($row = mysql_fetch_row($res))
{
    $Album->SetBackground($row[1]);
    $Page->SetTitle($row[0]);
}

$Page->Replace("%CONTENT%", $Album->GetPage());
echo $Page->GetPage();
?>
