
<%
/**
 *	$RCSfile: thread.jsp,v $
 *	$Revision: 1.1.1.1 $
 *	$Date: 2002/09/09 13:50:24 $
 */
%>

<%@ page import="java.util.*,
                 com.jivesoftware.forum.*,
                 com.jivesoftware.forum.util.*"
    errorPage="error.jsp"
%>

<%@ include file="include/branding/style.jsp" %>

<%@ include file="include/forumSetup.jsp" %>

<%  // Get parameters
    long forumID = ParamUtils.getLongParameter(request,"forum",-1L);
    long threadID = ParamUtils.getLongParameter(request,"thread",-1L);
    long messageID = ParamUtils.getLongParameter(request,"message",-1L);
    int start = ParamUtils.getIntParameter(request,"start",0);
    int range = myEnv.du.getMessageRange(request,response,pageUser);
    boolean hilite = ParamUtils.getBooleanParameter(request,"hilite");
    boolean redirect = ParamUtils.getBooleanParameter(request,"redirect");
    String searchWords = ParamUtils.getParameter(request,"q");
    
    // Load the forum
    Forum forum = forumFactory.getForum(forumID);
    // The requested thread object
    ForumThread thread = forum.getThread(threadID);
    
    // Compute number of topics, messages in this forum, number of pages
    int numMessages = thread.getMessageCount();
	int numReplies = numMessages-1; // subtract 1 because the root message is counted
    int numPages = (int)Math.ceil((double)numMessages/(double)range);
    int numMessagesToday = getNewMessageCount(thread);
    
    // Determine if we need to adjust the start index and redirect
    Iterator messages = null;
    if (redirect && messageID != -1) {
        // get an iterator of all messages
        messages = thread.messages();
        int index = 0;
        int messageRange = myEnv.du.getMessageRange(request,response,pageUser);
        StringBuffer redirectTo = new StringBuffer("thread.jsp");
        redirectTo.append("?forum=").append(forumID);
        redirectTo.append("&thread=").append(threadID);
        redirectTo.append("&hilite=true");
        redirectTo.append("&q=").append(java.net.URLEncoder.encode(searchWords));
        while (messages.hasNext()) {
            ForumMessage message = (ForumMessage)messages.next();
            if (message.getID() != messageID) {
                index++;
            }
            else {
                redirectTo.append("&start=");
                redirectTo.append(((index/messageRange))*messageRange);
                redirectTo.append("#").append(messageID);
                break;
            }
        }
        response.sendRedirect(redirectTo.toString());
        return;
    }
    
    // Get all iterators
	ResultFilter filter = ResultFilter.createDefaultMessageFilter();
	filter.setStartIndex(start);
	filter.setNumResults(range);
    // reinitialize the message iterator to show the appropriate range
	messages = thread.messages(filter);
	Iterator popularThreads = forum.popularThreads();
	ForumMessage rootMessage = thread.getRootMessage();
    
    // A user manager lets us get information about users
    UserManager userManager = forumFactory.getUserManager();
    
    // indicate whether or not this user is the author of the root message
    boolean pageUserIsThreadAuthor = 
        ((pageUser != null) && (!rootMessage.isAnonymous())
        && (pageUser.getID() == rootMessage.getUser().getID()));
    
    // Get a thread iterator (for the next/previous thread feature)
    ForumThreadIterator threadIterator = forum.threads();
    threadIterator.setIndex(thread);
%>

<%  String title = titlePrefix + ": " + thread.getName(); %>
<%@ include file="include/header.jsp" %>

<%@ include file="include/branding/header.jsp" %>

<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
    <td width="99%" valign="top">
    
    <span class="header">
    <%= rootMessage.getSubject() %>
    </span>
    <br>
    
    <font size="-1">
    <span class="header">
    <%  if (rootBreadcrumbText != null && rootBreadcrumbLink != null) { %>
    <a href="<%= rootBreadcrumbLink %>" class="header"
    ><%= rootBreadcrumbText %></a>
    &raquo;
    <%  } %>
    <a href="index.jsp" class="header" title="Go back to the forum listing"
    >Forums</a>
    &raquo;
    <a href="forum.jsp?forum=<%= forumID %>" class="header" title="Go back to the topic list"
    ><%= forum.getName() %></a>
    </span>
    </font>
    
    <p>
    
    <font size="-1" color="<%= deckTextColor %>">
    <b><%= numReplies %></b> <%= ((numReplies==1)?"reply":"replies") %> on
    <b><%= numPages %></b> <%= ((numPages==1)?"page":"pages") %>.
    <b><%= numMessagesToday %></b> <%= ((numMessagesToday == 1)?"reply":"replies") %> posted today.
<%  if (numReplies > 0) {
        String recentUsername = null;
        ForumMessage recentMessage = getMostRecentMessage(thread);
        if (recentMessage != null) {
            User recentUser = recentMessage.getUser();
            long recentMessageID = recentMessage.getID();
            if (!recentMessage.isAnonymous()) {
                recentUsername = recentUser.getUsername();
            }
            // starting page of the recent message:
            int recentMessageStart = (numPages-1) * range;
%>
    <br>
    <a href="thread.jsp?forum=<%= forumID %>&thread=<%= threadID %>&start=<%= recentMessageStart %>#<%= recentMessageID %>"
     style="color:<%= deckTextColor %>;" title="Go to the most recent message in this topic">Most recent message</a> posted in this topic by:
        <%  if (recentMessage.isAnonymous()) { %>
    <i>Guest</i>
        <%  } else { %>
    <a href="profile.jsp?user=<%= recentUser.getID() %>" style="color:<%= deckTextColor %>;" title="<%= recentUsername %>'s profile"><%= recentUsername %></a>
<%          }
        }
    }
%>
    </font>
    <p>
    
    </td>
    <td valign="top" width="1%" align="center">
    <%@ include file="include/loginbox.jsp" %>
    </td>
</tr>
</table>


<table cellpadding="0" cellspacing="2" border="0" width="100%" rows="1" cols="5">
<tr><td width="1%">
    <%= printBox("<img src=images/reply_12_12.gif border=0 width=12 height=12 alt=\"Reply to this topic\"> Reply to this topic","post.jsp?forum="+forumID+"&thread="+threadID+"&reply=true",buttonBorderColor,buttonBgColor,buttonBorderColorHover,buttonBgColorHover,"100%") %>
    </td>
    <td width="1%">
    <%= printBox("<img src=images/post_new_12_12.gif border=0 width=12 height=12 alt=\"Post a new topic\"> Post a new topic","post.jsp?forum="+forumID,buttonBorderColor,buttonBgColor,buttonBorderColorHover,buttonBgColorHover,"100%") %>
    </td>
    <td width="1%">
    <%= printBox("<img src=images/search_12_12.gif border=0 width=12 height=12 alt=\"Search this forum\"> Search this forum","search.jsp?forum="+forumID+"&thread="+threadID,buttonBorderColor,buttonBgColor,buttonBorderColorHover,buttonBgColorHover,"100%") %>
    </td>
    <td width="97%">&nbsp;</td>
</tr>
</table>

<p>

<table cellpadding="0" cellspacing="2" border="0" width="100%" rows="1" cols="5">
<tr>
    <td width="1%">
<%  if (threadIterator.hasPrevious()) {
        ForumThread prevThread = (ForumThread)threadIterator.previous();
        // advance the iterator pointer back to the original index
        threadIterator.next();
        String subj = prevThread.getRootMessage().getSubject();
%>
    <%= printBox("<span title=\""+subj+"\">&laquo; Previous Thread</span>","thread.jsp?forum="+forumID+"&thread="+prevThread.getID(),buttonBorderColor,buttonBgColor,buttonBorderColorHover,buttonBgColorHover,"100%") %>
<%  } else { %>
    &nbsp;
<%  } %>
    </td>
    <td width="98%">&nbsp;</td>
    <td width="1%">
<%  if (threadIterator.hasNext()) {
        ForumThread nextThread = (ForumThread)threadIterator.next();
        String subj = nextThread.getRootMessage().getSubject();
%>
    <%= printBox("<span title=\""+subj+"\">Next Thread &raquo;</span>","thread.jsp?forum="+forumID+"&thread="+nextThread.getID(),buttonBorderColor,buttonBgColor,buttonBorderColorHover,buttonBgColorHover,"100%") %>
<%  } else { %>
    &nbsp;
<%  } %>
    </td>
</tr>
</table>

<p>

<%-- end the special wrapping table --%>
</td></tr>
</table>

<table bgcolor="<%= tableHeaderBgColor %>" cellpadding="3" cellspacing="0" border="0" width="98%" align="center">
<tr><td width="99%">
    <font size="-1" color="<%= tableHeaderTextColor %>">
    This topic has <b><%= numReplies %></b> <%= ((numReplies==1)?"reply":"replies") %>
    on <b><%= numPages %></b> <%= ((numPages==1)?"page":"pages") %>
    
<%	if (numPages > 1) { %>
    [
<%	    if (start > 0) { %>
    <a href="thread.jsp?forum=<%=forumID%>&thread=<%=threadID%>&start=<%= (start-range) %>&msRange=<%= range %>"
     title="Previous page" style="color:#ffffff;">&laquo;</a>
    |
<%	    }
        int currentPage = (start/range)+1;
    	int lo = currentPage - 3;
    	if (lo <= 0) {
	    	lo = 1;
    	}
	    int hi = currentPage + 5;
    	while (lo < currentPage) {
%>
    <a href="thread.jsp?forum=<%=forumID%>&thread=<%=threadID%>&start=<%= ((lo-1)*range) %>&msRange=<%= range %>"
      style="color:#ffffff;"><%= lo %></a>&nbsp;
<%          lo++;
	    }
%>
    <b><span style="background-color:#ffffff;color:#000000;"><%= currentPage %></span></b>
<%	    while ((currentPage < hi) && (currentPage<numPages)) { %>
        &nbsp;<a href="thread.jsp?forum=<%=forumID%>&thread=<%=threadID%>&start=<%= ((currentPage)*range) %>&msRange=<%= range %>"
          style="color:#ffffff;"><%= currentPage+1 %></a>
<%          currentPage++;
	    }
        if (numMessages > (start+range)) {
            int numRemaining = (int)(numMessages-(start+range));
%>
	    | 
        <a href="thread.jsp?forum=<%=forumID%>&thread=<%=threadID%>&start=<%= (start+range) %>&msRange=<%= range %>"
         title="Next page" style="color:#ffffff;">&raquo;</a>
<%      } %>
    ]
<%	} %>
    </font>
    </td>
    <td width="1%" nowrap>
        <font size="-1">
        <a href="forum.jsp?forum=<%= forumID %>" style="color:#ffffff;"
         >Back to topic list</a>
        </font>
    </td>
</tr>
</table>


<%	// check to see if there are any forums
	if (!messages.hasNext()) {
%>
    <font size="-1">
    <i>No threads to display in this forum (or you don't have permission to see any).</i>
    </font>

<%	} else { %>

<%	// There are messages, so iterate and print them out
    String bgcolor = "";
    int row = 1;
	while (messages.hasNext()) {
		ForumMessage message = (ForumMessage)messages.next();
		boolean isRootMessage = (message.getID() == rootMessage.getID());
		User author = message.getUser();
        bgcolor = ((row++%2)==0)?tableRowColor1:tableRowColor2;
%>
    
<a name="<%= message.getID() %>"></a>
    
<table bgcolor="<%= tableBorderColor %>" cellpadding="0" cellspacing="0" border="0" width="98%" align="center">
<tr><td>
    <table bgcolor="<%= tableBorderColor %>" cellpadding="4" cellspacing="1" border="0" width="100%">
    <tr bgcolor="<%= bgcolor %>">
        <td width="1%" rowspan="2" valign="top">
        <table cellpadding="0" cellspacing="0" border="0" width="140">
        <tr><td>
            <font size="-1">
<%	    if (author == null) {
            // Check to see if there is an extended property for name & email
            // since the gateway feature imports names and emails as extended
            // props.
            String name = message.getProperty("name");
            String email = message.getProperty("email");
            String nameAndEmail = myEnv.du.getNameAndEmailDisplay(name, email);
            if (nameAndEmail != null) {
%>          
            <i>Guest: <%= nameAndEmail %></i>
<%          } else { %>
			<i>Guest</i>
<%          }
        } else { 
            String userStatus = author.getProperty("status");
%>
			<a href="profile.jsp?user=<%= author.getID() %>"
             ><b><%= author.getUsername() %></b></a>
            <%  if (userStatus != null) { %>
            <br><i><%= userStatus %></i>
            <%  } %>
            <br><br>
            Posts: <%= userManager.userMessageCount(author) %>
            <br>
            From:
            <br>
            Since: <%= shortDateFormatter.format(author.getCreationDate()) %>
            <br>
<%      } %>
            </font>
            </td>
        </tr>
        </table>
        </td>
        <td width="98%">
        <font size="-1">
        <b><%= message.getSubject() %></b>
        <br>
        Date Posted: <%= SkinUtils.formatDate(request,response,pageUser,message.getCreationDate()) %>
        </font>
		</td>
        <td width="1%" nowrap align="center">
        <font size="-1" color="#666666">
        <a href="post.jsp?forum=<%= forumID %>&thread=<%= threadID %>&message=<%= message.getID() %>&reply=true"
         title="Reply to this message">&raquo; Reply</a>
        </font>
        </td>
    </tr>
    <tr bgcolor="<%= bgcolor %>">
        <td width="99%" colspan="3" valign="top">
        <font size="-1">
<%  if (hilite) {
        String body = message.getBody();
        String[] queryWords = StringUtils.toLowerCaseWordArray(searchWords);
        body = StringUtils.highlightWords(body, queryWords, "<font style='background-color:#ffff00'><b>", "</b></font>");
%>
    <%= body %>
<%  } else { %>
    <%= message.getBody() %>
<%  } %>
        </font>
		<p>
        </td>
    </tr>
    </table>
</td></tr>
</table>
<%  if (isRootMessage) { %>
<br>
<%  } %>
	
<%		} // end of while
	} // end of else
%>

<table bgcolor="<%= tableBorderColor %>" cellpadding="1" cellspacing="0" border="0" width="98%" align="center">
<tr>
    <td>
<table bgcolor="<%= paginatorTableBgColor %>" cellpadding="3" cellspacing="0" border="0" width="100%" align="center">
<tr><td width="99%">
    <font size="-1" color="<%= paginatorTextColor %>">
    This topic has <b><%= numReplies %></b> <%= ((numReplies==1)?"reply":"replies") %>
    on <b><%= numPages %></b> <%= ((numPages==1)?"page":"pages") %>
    
<%	if (numPages > 1) { %>
    [
<%	    if (start > 0) { %>
    <a href="thread.jsp?forum=<%=forumID%>&thread=<%=threadID%>&start=<%= (start-range) %>&msRange=<%= range %>"
     title="Previous page" style="color:<%= paginatorTextColor %>;">&laquo;</a>
    |
<%	    }
        int currentPage = (start/range)+1;
    	int lo = currentPage - 3;
    	if (lo <= 0) {
	    	lo = 1;
    	}
	    int hi = currentPage + 5;
    	while (lo < currentPage) {
%>
    <a href="thread.jsp?forum=<%=forumID%>&thread=<%=threadID%>&start=<%= ((lo-1)*range) %>&msRange=<%= range %>"
      style="color:<%= paginatorTextColor %>;"><%= lo %></a>&nbsp;
<%          lo++;
	    }
%>
    <b><span style="background-color:#ffffff;color:#000000;"><%= currentPage %></span></b>
<%	    while ((currentPage < hi) && (currentPage<numPages)) { %>
        &nbsp;<a href="thread.jsp?forum=<%=forumID%>&thread=<%=threadID%>&start=<%= ((currentPage)*range) %>&msRange=<%= range %>"
          style="color:<%= paginatorTextColor %>;"><%= currentPage+1 %></a>
<%          currentPage++;
	    }
        if (numMessages > (start+range)) {
            int numRemaining = (int)(numMessages-(start+range));
%>
	    | 
        <a href="thread.jsp?forum=<%=forumID%>&thread=<%=threadID%>&start=<%= (start+range) %>&msRange=<%= range %>"
         title="Next page" style="color:<%= paginatorTextColor %>;">&raquo;</a>
<%      } %>
    ]
<%	} %>
    </font>
    </td>
    <td width="1%" nowrap>
        <font size="-1">
        <a href="forum.jsp?forum=<%= forumID %>" style="color:<%= paginatorTextColor %>;"
         >Back to topic list</a>
        </font>
    </td>
</tr>
</table>
    </td>
</tr>
</table>


<%-- begin the special wrapping table again --%>
<table cellpadding="8" cellspacing="0" border="0" width="100%">
<tr>
<td width="100%" valign="top">

<br>

<table cellpadding="0" cellspacing="2" border="0" width="100%">
<tr>
    <td width="1%">
    <%= printBox("<img src=images/back_12_12.gif border=0 width=12 height=12 alt=\"Back to topic list\"> Back to topic list","forum.jsp?forum="+forumID,buttonBorderColor,buttonBgColor,buttonBorderColorHover,buttonBgColorHover,"100%") %>
    </td>
    <td width="1%">
    <%= printBox("<img src=images/up_12_12.gif border=0 width=12 height=12 alt=\"Top of the page\"> Top of the page","#top",buttonBorderColor,buttonBgColor,buttonBorderColorHover,buttonBgColorHover,"100%") %>
    </td>
    <td width="98%">&nbsp;</td>
</tr>
</table>

<br>

<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
    <td align="right">
    <a href="http://www.jivesoftware.com/poweredby/" target="_blank"
    ><img src="images/powered_by_jive.gif" width="100" height="30" alt="Powered by Jive" border="0"></a>
    </td>
</tr>
</table>

<%@ include file="include/branding/footer.jsp" %>

<%@ include file="include/footer.jsp" %>

<%! // Global vars, methods, etc
    
    // Returns a property formatted name and email address string,
    // like <a href="mailto:foo">Foo Bar</a>
    private String myEnv.du.getNameAndEmailDisplay(String name, String email) {
        if (name == null && email == null) {
            return null;
        }
        StringBuffer buf = new StringBuffer(40);
        // name is not null, so try to make a like like:
        // <a href="mailto:foo@bar.com">Foo Bar</a>
        if (name != null) {
            if (email != null) {
                buf.append("<a href=\"mailto:").append(email).append("\">");
                buf.append(name);
                buf.append("</a>");
            }
            else {
                buf.append(name);
            }
        }
        // Else, name is null so just try to use the email:
        // <a href="mailto:foo@bar.com">foo@bar.com</a>
        else if (email != null) {
            String display = email;
            if (email.length() > 15) {
                display = email.substring(0,15) + "..";
            }
            buf.append("<a href=\"mailto:").append(email).append("\">");
            buf.append(display);
            buf.append("</a>");
        }
        
        if (buf.length() == 0) {
            return null;
        }
        else {
            return buf.toString();
        }
    }
%>
