<?php
    include "misc.inc";
    start_page();
?>
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=koi8-r">
<?php
    include "db_connect.inc";


    if(!isset($comment_id)) $comment_id=NULL;
    if(!isset($thread_id))   $thread_id=NULL;
    if(!isset($pos))         $pos=NULL;
    if(!isset($offset))      $offset=NULL;

    if(!$comment_id && $thread_id)
    {
        if(!$pos)
            $pos = 0;
        $result = mysql_db_query($db_database,
                            "SELECT Id  " .
                            " FROM Comments " .
                            " WHERE ThreadId=" . addslashes($thread_id) .
                            " AND ThreadPosition=". $pos,
                            $link);
        $row = mysql_fetch_array($result);
        $comment_id = $row[0];
    }

    if($comment_id)
    {
        include "comment.php";
        print("<link rel=\"alternate\" type=\"application/rss+xml\" title=\"RSS\" href=\"" . $PROGRAM_URL . "commentsrss.php?Id=" . $comment_id . "\">");
        print("<TITLE>" . $row[0] . "&nbsp;OBEC 3BOH</TITLE>");
    }
    else
    {
        if($thread_id)
        {
            $result = mysql_db_query($db_database,
                                "SELECT Title  " .
                                " FROM Threads " .
                                " WHERE Id=" . addslashes($thread_id) ,
                                $link);
            $row = mysql_fetch_array($result);

            print("<TITLE>" . $row[0] . "&nbsp;OBEC 3BOH</TITLE>");
        }
        else 
            print("<TITLE>OBEC 3BOH</TITLE>");
    }
?>
</HEAD>
<BODY link="#000033" vlink="#333333" alink="#CC0000">

<?php
    include "common.inc";
    include "table.inc";

    class ThreadTable extends Table
    {
        var $thread_id;
        var $comment_id;

        function ThreadTable($thread_id, $comment_id, $offset)
        {
            $this->thread_id  = $thread_id;
            $this->comment_id = $comment_id;

            $this->Table(array("Title",    "Name",  "PublishedAt"),
                         array("Название", "Автор", "Дата"),

                         "SELECT Comments.Id, Comments.Title, UNIX_TIMESTAMP(Comments.PublishedAt), Name, AuthorId, ThreadOffset " .
                         "FROM Comments, Authors " .
                         "WHERE Authors.Id=AuthorId AND ThreadId = " . addslashes($thread_id) .
                         " ORDER BY ThreadPosition ASC",  

                         "SELECT COUNT(*) FROM Comments WHERE ThreadId = " . addslashes($thread_id),
       
                         $offset,

                         array(0 => 70, 1 => 20)
                        );
        }

        function processCell($row, $i)
        {
            include "config.inc"; //??
            switch($i)
            {
                case 0:
                    $level = $row[5];
                    $title = htmlspecialchars(trim($row[1]));

                    if($this->comment_id==$row[0])
                    {
                        $b_start = "<B>";
                        $b_end = "</B>";
                    }

                    print("<TD NOWRAP><nobr>" . $b_start ."<IMG SRC=\"images/blank.gif\" align=top height=20 WIDTH=" . (10+30*$level) . "><A HREF = \"thread.php?comment_id=" . htmlspecialchars($row[0]) . "&thread_id=" . $this->thread_id . "&pos=" . $this->offset . "\">" . $title . "</A>" . $b_end . "</nobr></TD>\n");
                    break;

                case 1:
                    print("<TD NOWRAP><A HREF = \"author.php?Id=" . htmlspecialchars($row[4]) . "\">" . htmlspecialchars($row[3]) . "</A></TD>\n");
                    break;

                case 2:
                    print("<TD ALIGN=CENTER NOWRAP>" . htmlspecialchars(gmstrftime($DATE_FORMAT, $row[2])) . "</TD>\n");
                    break;
            }
        }

        function urlParameters($i)
        {
            return "&thread_id=" . $this->thread_id;
        }
    }

    show_header();

    if($thread_id)
        $table = new ThreadTable($thread_id, $comment_id, $pos);

    if($comment_id)
    {
        showComment($row, $comment_id, $thread_id, ($thread_id>=0)?$table->row_count:1);
    }

    if($thread_id)
    {
        $table->showTable("thread.php");
    }

    show_footer();
?>

</BODY>
</HTML>
