<?
if (isset($_GET['category_id'])) {
    $category_id = (int) $_GET['category_id'];
    
    $set['p_str'] = 24;
    $k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `gallery_foto` WHERE `id_user` = '$user[id]' AND `id_gallery` = '$category_id'"),0);
    $k_page = k_page($k_post,$set['p_str']);
    $page = page($k_page);
    $start = $set['p_str'] * $page - $set['p_str'];

    if ($k_post == 0)
    {
      	?>
        <div class="mess">
      	 У вас нет фотоальбомов
      	</div>
        <?
    }

    $q = mysql_query("SELECT * FROM `gallery_foto` WHERE `id_user` = '$user[id]' AND `id_gallery` = '$category_id' ORDER BY `time` DESC LIMIT $start, $set[p_str]");

    ?><div class="email-list-photo"><?
    
    while ($post = mysql_fetch_assoc($q))
    {
    	?>
      <a href="?location=<?= $location?>&amp;type=<?= $type?>&amp;attachments_id=<?= $post['id']?>">
        <img src="/foto/foto50/<?= $post['id']?>.<?= $post['ras']?>" />
      </a>
      <?
    }
    ?></div><?

    if ($k_page > 1) {
      str('?location=' . $location . '&amp;type=' . $type . '&amp;category_id=' . $category_id . '&amp;', $k_page, $page); 
    }
    
    $urlBack = '?location=' . $location . '&amp;type=' . $type . '&amp;category';
} elseif (isset($_GET['category'])) {
    $k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `gallery` WHERE `id_user` = '$user[id]'"),0);
    $k_page = k_page($k_post,$set['p_str']);
    $page = page($k_page);
    $start = $set['p_str'] * $page-$set['p_str'];

    if ($k_post == 0)
    {
      	?>
        <div class="mess">
      	 У вас нет фотоальбомов
      	</div>
        <?
    }

    $q = mysql_query("SELECT * FROM `gallery` WHERE `id_user` = '$user[id]' ORDER BY `time` DESC LIMIT $start, $set[p_str]");

    while ($post = mysql_fetch_assoc($q))
    {
    	$num++;
    	$count = mysql_result(mysql_query("SELECT COUNT(*) FROM `gallery_foto` WHERE `id_gallery` = '$post[id]'"), 0);
    	?>
      <div class="email <?= ($num % 2 ? "nav1" : "nav2")?>">
        <a href="?location=<?= $location?>&amp;type=<?= $type?>&amp;category_id=<?= $post['id']?>">
          <img src="/style/icons/images-stack.png" /> <?= text($post['name'])?> (<?= $count?> фото)
        </a>
      </div>
      <?
    }

    if ($k_page > 1) {
      str('?location=' . $location . '&amp;type=' . $type . '&amp;category&amp;', $k_page, $page); 
    }
    
    $urlBack = '?location=' . $location . '&amp;type=' . $type;
} else {
    $q = mysql_query("SELECT * FROM `gallery_foto` WHERE `id_user` = '$user[id]' ORDER BY `id` DESC LIMIT 5");
    
    if (mysql_num_rows($q) > 0) {
      ?>
      <div class="email-list-photo">
        <?
        while($post = mysql_fetch_assoc($q)) {
          ?>
          <a href="?location=<?= $location?>&amp;type=<?= $type?>&amp;attachments_id=<?= $post['id']?>"><img src="/foto/foto50/<?= $post['id']?>.jpg" /></a>
          <?
        }
        ?>
      </div>
      
      <div class="email email-panel"> 
        <a href="?location=<?= $location?>&amp;type=<?= $type?>&amp;category"><img src="/style/icons/images.png" /> Показать все фото</a> &raquo;
      </div>
      <?    
    } else {
      ?>
      <div class="mess">
        У вас еще нет на сайте загруженных фотографий, но вы можете воспользоваться формой ниже.
      </div>
      <?
    }
    ?>

    <form action="?location=<?= $location?>&amp;type=<?= $type?>" method="POST" enctype="multipart/form-data">
      <div>
        <b>Добавить файл</b><br />
        <input type="file" name="file" accept="image/*,image/jpeg" />
      </div>
      
      <div>
        <b>Описание</b><br />
        <textarea name="msg" placeholder=""></textarea><br />
      </div>
      
      <div class="adult">
        <label><input type="checkbox" name="adult" value="1" /> Файл для взрослых <span>(18+)</span></label>
      </div>
      
      <input type="submit" name="submit" value="Загрузить" />
    </form>
    <? 
    $urlBack = $_SESSION[$location]['return'];
} 
?>
<div class="email email-panel">
  <a href="<?= $urlBack?>"><img src="/style/icons/in.png" /> Назад</a>
</div>
<?