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

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

$kolvo = 10;
$all = mysql_num_rows(mysql_query("SELECT id FROM tb_forum_mes where tid='$topmesid'"));
$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_mes WHERE tid = '$topmesid' ORDER BY id ASC limit $start,$kolvo");
$topic = mysql_fetch_array(mysql_query("SELECT id, catid, topicname, topic_close FROM tb_forum_topic WHERE id = '$topmesid'"));
$cat = mysql_fetch_array(mysql_query("SELECT id, categoryname FROM tb_forum_category WHERE id = '" . $topic['catid'] . "'"));
/// Пишем в таблицу о том что тема прочитана
    if (isset($_SESSION['username']) && isset($_SESSION['password'])) { 
	$resx = mysql_fetch_array( mysql_query( "SELECT(SELECT count(*) FROM forum_visit_status WHERE iduser = '".$_SESSION['iduser']."' AND idtopic = '".$topic['id']."') forumvisit" ) );
    $forumvisit=$resx["forumvisit"];
	    if($forumvisit == 0){
	        mysql_query("INSERT INTO forum_visit_status (status, iduser, idtopic) VALUES (1, '".$_SESSION['iduser']."', '".$topic['id']."')");
		}
    }
///	
if (isset($_SESSION['username']) && isset($_SESSION['password']) and $topic['topic_close'] != 1) {
    $creatposts = '<div align="center"><a href="#" onclick="newmess(' . $topmesid . ');" class="big_btn">Ответить</a><div>';
    $pcolor = 'background:#ebebeb';
    ?>
    <script type="text/javascript">
    function newmess (tid) {
        $.post('forum/addmess.php',{
            id: tid
        }, function(data){
            $('#idmes').fadeIn(500);
            $('#idmes td').html(data);
        });
        return false;
    }
    </script>
    <?
} else
if (isset($_SESSION['username']) && isset($_SESSION['password']) and $topic['topic_close'] == 1) {
    $creatposts = '<div align="center" style="color:#800000;">Тема закрыта для комментирования<div>';
    $pcolor = 'background:#FABABA';
} else {
    $nous = 'style="display:none"';
}
?>
<div class="navname"><a href="/forum" class="link">Форум</a> | <a href="/topic/<?=$cat['id']?>" class="link"><?=$cat['categoryname']?></a> | <?=$topic['topicname'] ?></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='/posts/$topmesid/$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,'/posts/'.$topmesid.'/',1,1)."</td><td nowrap='nowrap'>";
if ($this_page == $allpage) { echo "<span class='textgray'>"; } else { echo "<a href='/posts/$topmesid/$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; font-weight: normal;" align="left" colspan="2"><b>Тема:</b>&nbsp;&nbsp;&nbsp;<font color="#1c62b9"><?=$topic['topicname']?></font></th>
</thead>
<?
if ($cat['id'] == '' ) {
    ?>
    <tr><td colspan="2" class="lt" style="color: #800000; background: #fababa;" align="center">Ошибка! Раздел не существует</td></tr>
    <?
}
if (isset($_SESSION['username']) && isset($_SESSION['password'])) {
    ?>
    <script type="text/javascript">
    function qoute (tid, qid) {
        $.post('forum/addmess.php',{
            id: tid,
            quote: qid
        }, function(data){
            $('#idmes').fadeIn(500);
            $('#idmes td').html(data);
        });
        return false;
    }
    function delposts (tid, mid) {
        $.post('ajax.php?func=delposts',{
            id: tid,
            posts: mid
        }, function(data){
            FLAX.Html.thread['flax'].repeat();
        });
        return false;
    }
    function banforum (tid) {
        $.post('ajax.php?func=banforum',{
            id: tid
        }, function(data){
            FLAX.Html.thread['flax'].repeat();
        });
        return false;
    }
    $('#quote, #delposts, #banposts').tipTip();
    </script>
    <?
}
while ($row = mysql_fetch_array($sql)) {
    $usinfo = mysql_fetch_array(mysql_query("SELECT id, avatar, username, user_status, blockforum, reyting FROM tb_users WHERE id = '" . $row['userid'] . "'"));
    if ($usinfo['user_status'] == 'admin') {
        $stat = '<div style="color:#800000">Администратор</div>';
    } else
    if ($usinfo['user_status'] == 'moder') {
        $stat = '<div style="color:#1c62b9">Модератор</div>';
    } else
	if ($usinfo['user_status'] == 'arbitr') {
        $stat = '<div style="color:#DFA101">Арбитр</div>';
    } else {
        $stat = '';//'<div style="color:#212121">Пользователь</div>';
    }
    if ($usinfo['blockforum'] == 1) {
        $usban = ' - <font color="#800000">БАН</font>';
    } else {
        $usban = '';
    }
    if ($usinfo['reyting'] >= 250 and $usinfo['reyting'] < 600) {
        $img_o = '<img class="usstatuspost" width="26" height="40" border="0" src="' . ROOT . '/theme/img/status/master.png">';
    } else
    if ($usinfo['reyting'] >= 600 and $usinfo['reyting'] < 1000) {
        $img_o = '<img class="usstatuspost" width="26" height="40" border="0" src="' . ROOT . '/theme/img/status/s_master.png">';
    } else
    if ($usinfo['reyting'] >= 1000) {
        $img_o = '<img class="usstatuspost" width="26" height="40" border="0" src="' . ROOT . '/theme/img/status/oligarh.png">';
    } else {
        $img_o = '';
    }
    
    if (isset($_SESSION['username']) && isset($_SESSION['password'])) {
        $qoute = '<a href="#" id="quote" title="Цитировать" onclick="qoute('.$topmesid.', '.$row['id'].')"><img src="' . ROOT . '/theme/img/forum/qoute.png" width="16" height="16" border="0" /></a>';
        if (/*$row['userid'] == $_SESSION['iduser'] or */$user_status == 'admin' or $user_status == 'moder' and $usinfo['user_status'] != 'admin') {
            $del = '<a href="#" id="delposts" title="Удалить" onclick="delposts('.$row['userid'].', '.$row['id'].')"><img src="' . ROOT . '/theme/img/forum/del.png" width="16" height="16" border="0" /></a>';
        } else {
            $del = '';
        }
        if ($user_status == 'admin' or $user_status == 'moder' and $usinfo['user_status'] != 'admin' and $usinfo['user_status'] != 'moder') {
            $ban = '<a href="#" id="banposts" title="Бан" onclick="banforum('.$row['userid'].')"><img src="' . ROOT . '/theme/img/forum/ban.png" width="16" height="16" border="0" /></a>';
        } else {
            $ban = '';
        }
    }
    
    $us_online = mysql_query("SELECT id FROM online WHERE iduser = '$usinfo[id]'");
    if (mysql_num_rows($us_online) > 0) {
        $img_online = '<font color="#84A91E">онлайн</font>';
    } else {
        $img_online = '<font color="#800000">оффлайн</font>';
    }
	?>
    <tr>
        <td class="lt" style="background: #FAF7BA; padding: 5px;" colspan="2"><div style="position: relative;">ID: <?=$usinfo['id']?> - <font color="#1c62b9"><?=$usinfo['username']?></font> [<?=$img_online?>]<?=$usban?><div style="position: absolute; right: 5px; top: 0;"><?=$qoute?> <?=$del?> <?=$ban?></div></div></td>
    </tr>
    <tr>
        <td class="lt" valign="top" width="80" align="center"><div style="position: relative;">
		<a href="/id<?=$usinfo['id']?>" >
		<img src="<?=ROOT?>/avp/<?=$usinfo['avatar']?>" class="avatar" width="60" height="60" />
		</a> 
		<?=$img_o?></div><?=$stat?><br /></td>
        <td class="lt" valign="top" align="left"><div class="pdate"><?=date('d.m.Y', $row['date'])?> | <?=date('H:i', $row['date'])?></div><div id="vcom">
        <?
        $array = explode(" ", $row['mes']);
        for ($i = 0, $array_num = count($array); $i < $array_num; $i++) {
            $word_split = wordwrap($array[$i], 70, " ", true);
            echo $word_split . ' ';
        }
        ?>
        </div></td>
    </tr>
    <?
}
?>
<tr <?=$nous?>><td class="rt" colspan="2" style="<?=$pcolor?>"><?=$creatposts?></td></tr>
<tr id="idmes" style="display: none;"><td colspan="2"></td></tr>
</table>
