<?php

   // Modif GC :
  $m_author = $ClsMembre->GetPrenom($guserid)." ".$ClsMembre->GetNom($guserid) ;
  //-------------------------------------

  $t_gif="<IMG SRC=\"$forum_url/images/t.gif\" WIDTH=12 HEIGHT=21 BORDER=0>";
  $l_gif="<IMG SRC=\"$forum_url/images/l.gif\" WIDTH=12 HEIGHT=21 BORDER=0>";
  $p_gif="<IMG SRC=\"$forum_url/images/p.gif\" WIDTH=9 HEIGHT=21 BORDER=0>";
  $m_gif="<IMG SRC=\"$forum_url/images/m.gif\" WIDTH=9 HEIGHT=21 BORDER=0>";
  $c_gif="<IMG SRC=\"$forum_url/images/c.gif\" WIDTH=9 HEIGHT=21 BORDER=0>";
  $i_gif="<IMG SRC=\"$forum_url/images/i.gif\" WIDTH=12 HEIGHT=21 BORDER=0>";
  $n_gif="<IMG SRC=\"$forum_url/images/n.gif\" WIDTH=9 HEIGHT=21 BORDER=0>";
  $space_gif="<IMG SRC=\"$forum_url/images/trans.gif\" WIDTH=5 HEIGHT=21 BORDER=0>";
  $trans_gif="<IMG SRC=\"$forum_url/images/trans.gif\" WIDTH=12 HEIGHT=21 BORDER=0>";
  

//***************************************************************************************
  function echo_data($image, $topic, $row_color){
    GLOBAL $ForumTableWidth,$ForumTableHeaderColor,$ForumTableHeaderFontColor;
    GLOBAL $ForumTableBodyColor1,$ForumTableBodyFontColor1,$ForumTableBodyColor2,$ForumTableBodyFontColor2;
    GLOBAL $read_page,$ext,$collapse,$id,$UseCookies;
    GLOBAL $space_gif,$num,$old_message,$haveread,$use_haveread;
    GLOBAL $lNew, $GetVars;
    GLOBAL $myconnexion, $color1, $m_author ;

    $thread_total="";
    
    if(($row_color%2)==0){
      $bgcolor="#FFFFFF";//$ForumTableBodyColor1;
      $font_color=$ForumTableBodyFontColor1;
    }
    else{
      $bgcolor=$color1;//$ForumTableBodyColor2;
      $font_color=$ForumTableBodyFontColor2;
    }  

    $subject ="<TABLE CELLSPACING=0 CELLPADDING=0 BORDER=0";
    if($bgcolor!=""){
        $subject.=" BGCOLOR=\"".$bgcolor."\"";
    }
    $subject.=">\n";
    $subject.="<TR>\n<TD>";
    $subject.=$space_gif;
//    $subject.=$image."</TD>\n<TD><FONT COLOR=\"$font_color\">&nbsp;";
    $subject.=$image."</TD>\n<TD>&nbsp;";

    if($id==$topic["id"] && $read=true){
        $subject .= "<b>".$topic["subject"]."</b>";
        $author = "<b>".$topic["author"]."</b>";
        $datestamp = "<b>".date_format($topic["datestamp"])."</b>";
    }
    else{
        $subject.='<a class="student" href="'.$read_page.$ext.'?f='.$num.'&i='.$topic["id"];
        $subject.='&t='.$topic["thread"].$myconnexion.'" '.Status().'>'.$topic["subject"].'</a>';
        $author = $topic["author"];
        $datestamp = date_format($topic["datestamp"]);
    }

//    $subject.="&nbsp;&nbsp;</font>";
    $subject.="&nbsp;&nbsp;";

    if($UseCookies){
      $isnew=false;
      if($use_haveread==true){
        if ($old_message<$topic["id"]) {
          if(!IsSet($haveread[$topic["id"]])) $isnew=true;
        }
      }
      elseif($old_message<$topic["id"]){
        $isnew=true;
      }
      if($isnew){
        $subject.="<font face=\"MS Sans Serif,Geneva\" size=\"-2\" color=\"#FF0000\">".$lNew."</font>";
      }
    }

    $subject.="</TD>\n</TR>\n</TABLE>";
	
	print ('<TR VALIGN=middle>
<TD '.bgcolor($bgcolor).'>'.$subject.'</TD>');
	if ( $m_author != $author ) {
		print ('<TD '.bgcolor($bgcolor).' nowrap><FONT COLOR="'.$font_color.'"><b>'.$author.'</b></FONT></TD>');
	} 
	else { 
		print ('<TD '.bgcolor($bgcolor).' nowrap><FONT COLOR="'.$font_color.'">'.$author.'</FONT></TD>');
	} 
	print ('<TD '.bgcolor($bgcolor).' nowrap><FONT SIZE="-1" COLOR="'.$font_color.'">'.$datestamp.'&nbsp;</FONT></TD>
	</TR>');
  }


//***************************************************************************************
  function thread($seed=0){

    GLOBAL $row_color_cnt;
    GLOBAL $messages,$threadtotal;
    GLOBAL $font_color, $bgcolor;
    GLOBAL $t_gif,$l_gif,$p_gif,$m_gif,$c_gif,$i_gif,$n_gif,$trans_gif;    
    
    $image="";
    $images="";
      
    if(!IsSet($row_color_cnt)){
      $row_color_cnt=0;
    }
    $row_color_cnt++;

    if($seed!="0"){
      $parent=$messages[$seed]["parent"];
      if($parent!=0){
        if(!IsSet($messages[$parent]["images"])){  
          $messages[$parent]["images"]="";
        }
        $image=$messages[$parent]["images"];
        if($messages[$parent]["max"]==$messages[$seed]["id"]){
          $image.=$l_gif;
        }
        else{
          $image.=$t_gif;
        }
      }

      if(@is_array($messages[$seed]["replies"])){
        if(IsSet($messages[$parent]["images"])){
          $messages[$seed]["images"]=$messages[$parent]["images"];
          if($seed==$messages["$parent"]["max"]){
            $messages[$seed]["images"].=$trans_gif;
          }
          else{
            $messages[$seed]["images"].=$i_gif;
          }  
        }
        $image.=$m_gif;
      }
      else{
        if($messages[$seed]["parent"]!=0){
          $image.=$c_gif;
        }
        else{
          if($threadtotal[$messages[$seed]["thread"]]>1){
            $image.=$p_gif;
          }
          else{
            $image.=$n_gif;
          }
        }
      }        

      echo_data($image, $messages[$seed], $row_color_cnt);
    }//end of: if($seed!="0")
    
    if(@is_array($messages[$seed]["replies"])){
      $count=count($messages[$seed]["replies"]);
      for($x=1;$x<=$count;$x++){
        $key=key($messages[$seed]["replies"]);
        thread($key);
        next($messages[$seed]["replies"]);
      } 
    }
  }  


//***************************************************************************************

  @reset($headers);
  $row=@current($headers);

  if(is_array($row)){
    if(!$read){
      reset($threads);
      $rec=current($threads);
      while(is_array($rec)){
        $thd=$rec["thread"];
        if(!isset($rec["tcount"])) $rec["tcount"]=0;
        $tcount=$rec["tcount"];
        $threadtotal[$thd]=$tcount;
        $rec=next($threads);
      }
    }
    else{
      $threadtotal[$thread]=count($headers);
    }
    $topics["max"]="0";
    $topics["min"]="0";
    While(is_array($row)){
      $x="".$row["id"]."";
      $p="".$row["parent"]."";
      $messages["$x"]=$row;
      $messages["$p"]["replies"]["$x"]="$x";
      $messages["$p"]["max"]=$row["id"];
      if(!isset($messages["max"])) $messages["max"]=0;
      if(!isset($messages["min"])) $messages["min"]=0;
      if($messages["max"]<$row["thread"]) $messages["max"]=$row["thread"];
      if($messages["min"]>$row["thread"]) $messages["min"]=$row["thread"];
      $row=next($headers);
    }
  }
  

print ('<TABLE width="100%" border="0" cellspacing="0" cellpadding="2" bgcolor="'.$color2.'" >
<TR><TD>
<TABLE WIDTH="100%" CELLSPACING=0 CELLPADDING=0 BORDER=0>
<TR>
    <TD HEIGHT=21 '.bgcolor($color1).' WIDTH="100%"><FONT class="HEADER2">&nbsp;<b>'. $lTopics.'</b></FONT></TD>
    <TD HEIGHT=21 '.bgcolor($color1).' NOWRAP WIDTH=150><FONT class="HEADER2"><b>'.$lAuthor.'&nbsp;</b></FONT></TD>
    <TD HEIGHT=21 '.bgcolor($color1).' NOWRAP WIDTH=100><FONT class="HEADER2"><b>'.$lDate.'</b></FONT></TD>
</TR>');

thread();

print ('</TABLE>
</TR></TD></TABLE>');