<?
if (!defined('webid')) {
    exit();
}

$topicid = intval($_GET['id']);

$kolvo = 10;
$all = mysql_num_rows(mysql_query("SELECT id FROM tb_forum_topic where catid='$topicid'"));
$allpage = ceil($all/$kolvo);
$this_page = (isset($_GET['pg'])) ? intval($_GET['pg']) : 1 ;
if ($this_page < 1) $this_page = 1;
if ($this_page > $allpage) $this_page = $allpage;
$start = $this_page * $kolvo - $kolvo;
if ($start == -$kolvo) { $start = 1; }

include_once 'functiones.php';

$sql = mysql_query("SELECT * FROM tb_forum_topic WHERE catid = '$topicid' ORDER BY id ASC limit $start,$kolvo");
$forum = mysql_fetch_array(mysql_query("SELECT id, categoryname FROM tb_forum_category WHERE id = '$topicid'"));

if (isset($_SESSION['username']) && isset($_SESSION['password'])) {
    if ($topicid != 1 or $user_status == 'admin') {
        $creattopic = '<div style="margin:10px 0;" align="center"><a href="#" onclick="newtopic(' . $topicid . ');" class="big_btn">Создать новую тему</a><div>';
        ?>
        <script type="text/javascript">
        function newtopic (tid) {
            $.post('forum/addtopic.php',{
                id: tid
            }, function(data){
                $('#newtopic').html(data);
            });
            return false;
        }
        </script>
        <?
    }
}
?>
<div class="navname"><a href="/forum" class="link">Форум</a> | <?=$forum['categoryname']?></div>
<?
echo "<table width='100%' style='margin-bottom: 5px;'><tr><td nowrap='nowrap'>";
$nextp = $this_page + 1;
$lastp = $this_page - 1;
if ($this_page > 1) { echo "<a href='/topic/$topicid/$lastp' class=\"link\"><span class='text14'>"; } else { echo "<span class='textgray'>"; } echo "&larr;&nbsp;Предыдущая</span>";
if ($this_page == 1) { echo "</a>"; } echo "</td><td width='90%' align='center'>". navigation($allpage,'/topic/'.$topicid.'/',1,1)."</td><td nowrap='nowrap'>";
if ($this_page == $allpage) { echo "<span class='textgray'>"; } else { echo "<a href='/topic/$topicid/$nextp' class=\"link\"><span class='text14'>"; } echo "Следующая&nbsp;&rarr;</span>"; 
if ($this_page == $allpage) { echo "</a>"; } echo "</td></tr></table>";
?>
<table class="otab" width="100%" border="0" cellpadding="0" cellspacing="1">
<thead style="background: #ebebeb;">
    <th style="padding: 5px;" align="left" colspan="2">Форум</th>
    <th style="padding: 5px;" width="30" align="center">Ответов</th>
    <th style="padding: 5px;" width="140" align="center">Последнее сообщение</th>
</thead>
<?
if ($forum['id'] == '' ) {
    ?>
    <tr><td colspan="4" class="lt" style="color: #800000; background: #fababa;" align="center">Ошибка! Раздел не существует</td></tr>
    <?
}
if ($user_status == 'admin') {
    ?>
    <script type="text/javascript">
    $('.topd').tipTip();
    function deltopic (tid) {
        $.post('ajax.php?func=delpotic',{
            id: tid
        }, function(data){
            if (data == 1) {
                FLAX.Html.thread['flax'].repeat();
            }
        });
        return false;
    }
    function closetopic (tid) {
        $.post('ajax.php?func=closetopic',{
            id: tid
        }, function(data){
            if (data == 1) {
                FLAX.Html.thread['flax'].repeat();
            }
        });
        return false;
    }
    </script>
    <?
}
while ($row = mysql_fetch_array($sql)) {
    
    if ($user_status == 'admin') {
        $deltopic = '<a href="#" onclick="deltopic(' . $row['id'] . ');" class="topd" title="Удалить топик"><img src="' . ROOT . '/theme/img/ref_del.png" width="16" height="16" border="0" /></a>';
        if ($row['topic_close'] == 1) {
            $im_topic = '<a href="#" onclick="closetopic(' . $row['id'] . ');" class="topd" title="Открыть топик"><img src="' . ROOT . '/theme/img/close_topic.png" width="16" height="16" border="0" /></a>';
        } else {
            $im_topic = '<a href="#" onclick="closetopic(' . $row['id'] . ');" class="topd" title="Закрыть топик"><img src="' . ROOT . '/theme/img/open_topic.png" width="16" height="16" border="0" /></a>';
        }
    }
    
    if ($row['topic_close'] == 1) {
        $img = '<img src="' . ROOT . '/theme/img/forum/topic_close.png" width="24" height="24" />';
    } else {
        $img = '<img src="' . ROOT . '/theme/img/forum/topic.png" width="24" height="24" />';
    }
    $postcount = mysql_query("SELECT userid, date FROM tb_forum_mes WHERE tid = '" . $row['id'] . "' ORDER BY id DESC");
    if (mysql_num_rows($postcount) > 0) {
        $count = mysql_num_rows($postcount);
    } else {
        $count = 0;
    }
    $danie = mysql_fetch_array($postcount);
    $usname = mysql_fetch_array(mysql_query("SELECT username FROM tb_users WHERE id = '" . $danie['userid'] . "'"));
    ?>
    <tr>
        <td width="40" align="center" class="lt"><?=$img?></td>
        <td class="lt" align="left"><a href="/posts/<?=$row['id']?>" class="link"><?=$row['topicname']?></a></td>
        <td align="center" class="rt"><?=$count?></td>
        <td class="lt" align="left"><div style="position: relative;">
		<font color="#1c62b9"><?=$usname['username']?></font><br />
		<font color="#cccccc">
		<?
		        if (isset($_SESSION['username']) && isset($_SESSION['password'])) { 
		
		            $resxfs = mysql_fetch_array( mysql_query( "SELECT(SELECT count(*) FROM forum_visit_status WHERE iduser = '".$_SESSION['iduser']."' AND idtopic = '".$row['id']."') forumvisitfs" ) );
                    if ($resxfs["forumvisitfs"] == 0) 
					{
					   
		                ?>  
		                <font style='color: #228B22;'>Новый ответ в теме</font>
						<image class="timeimgs" title="В данной теме появились свежие ответы которых вы еще не читали." align="right" width="14" height="14" src="theme/images/control_bottom_right.png" />
		                <?
										
					}
		            else
		            {
		              echo date('d.m.Y', $danie['date']);echo ' | ';echo date('H:i', $danie['date']);
		            }
		        }
		        else
		        {
		         echo date('d.m.Y', $danie['date']);echo ' | ';echo date('H:i', $danie['date']);
		        }		
		?>
		</font>
		<div class="deltopic"><?=$im_topic?> <?=$deltopic?></div></div></td>
    </tr>
    <?
}
?>
</table>
<?=$creattopic?>
<div id="newtopic"></div>