<html xmlns:py="http://genshi.edgewall.org/" py:strip="">

  <py:def function="sortable_th(order, direction, Class, title, href)">
    <th class="${Class} ${order == Class and direction or ''}">
      <a title="Sort by ${Class}${order == Class and direction == 'asc' and ' (descending)' or ''}" href="${href}?order=${Class}&amp;direction=${(Class == order and direction == 'desc' and 'asc' or 'desc')}">
        $title
      </a>
    </th>
  </py:def>

  <py:def function="display_discussion(discussion, href)">
    <py:with vars="is_topic_edit = not discussion.message.id and (req.args.discussion_action in ('edit', 'post-edit')) and not req.args.submit;">
      <div class="topic ${discussion.topic.new and 'new' or None}">
        <a name="-1"></a>

        <div class="header" py:choose="">
          <py:when test="is_topic_edit">
            <div class="subject">
              ${discussion.subject or '&nbsp;'}
            </div>
            <div class="body">
              ${discussion.body or '&nbsp;'}
            </div>
          </py:when>
          <py:otherwise>
            <div class="subject">
              ${discussion.topic.subject or '&nbsp;'}
            </div>
            <div class="body">
              ${discussion.topic.body or '&nbsp;'}
            </div>
          </py:otherwise>
        </div>

        <div class="controls">
          <py:if test="'DISCUSSION_APPEND' in perm">
            <a href="${href}?discussion_action=add;message=-1;#reply">Reply</a>
            <a href="${href}?discussion_action=quote;message=-1;#reply">Quote</a>
            <a py:if="('DISCUSSION_MODERATE' in perm and discussion.is_moderator) or ((discussion.authname == discussion.topic.author) and (discussion.authname != 'anonymous'))" href="${href}?discussion_action=edit;#reply">Edit</a>
          </py:if>
        </div>

        <div class="footer">
          <div class="author">
            ${discussion.topic.author}
          </div>
          <div class="time">
            ${discussion.topic.time}
          </div>
        </div>

        <py:if test="is_topic_edit">
          ${display_form(href, 'Edit:', 'edit_form', 'post-edit')}
        </py:if>
      </div>

      <div py:if="len(discussion.messages) or str(req.args.message) == '-1'" class="replies ${discussion.topic.new and 'new' or None}">

        ${display_set_display(href)}

        <ul class="reply">
          <py:if test="len(discussion.messages)">
            ${display_messages(href, discussion.messages)}
          </py:if>

          <py:if test="(str(req.args.message) == '-1') and (req.args.discussion_action in ('add', 'quote', 'post-add')) and (not req.args.submit)">
            ${display_preview()}
            <li>
              ${display_form(href, 'Reply:', 'add_form', 'post-add')}
            </li>
          </py:if>
          <li></li>
        </ul>


        ${display_set_display(href)}

      </div>
    </py:with>
  </py:def>

  <py:def function="display_form(href, title, Class, action)">
    <form class="${Class}" method="post" action="${href}#preview">
      <fieldset>
        <a name="reply"></a>
        <legend>
          ${title}
        </legend>

        <div class="field">
          <label for="author">Author:</label><br/>
          <py:choose>
            <input py:when="discussion.authname == 'anonymous'" type="text" id="author" name="author" value="${req.args.author or 'anonymous'}"/>
            <input py:otherwise="" type="text" id="author" name="author" value="${discussion.authname}" readonly="readonly"/>
          </py:choose><br/>
        </div>

        <div py:if="not req.args.message" class="field">
          <label for="subject">Subject:</label><br/>
          <py:choose>
            <input py:when="discussion.realm == 'discussion-wiki'" type="text" id="subject" name="subject" value="${req.args.subject}" readonly="readonly"/>
            <input py:otherwise="" type="text" id="subject" name="subject" value="${req.args.subject}"/>
          </py:choose><br/>
        </div>

        <div class="field">
          <label for="body">Body:</label><br/>
          <textarea id="body" class="wikitext" name="body" cols="80" rows="20">${req.args.body or 'Enter your message here...'}</textarea>
        </div>

        <div class="buttons">
          <input type="submit" name="preview" value="Preview"/>
          <input type="submit" name="submit" value="Submit changes"/>
          <input type="button" name="cancel" value="Cancel" onclick="location.replace('${href}')"/>
        </div>

        <input type="hidden" name="message" value="${req.args.message}"/>
        <input type="hidden" name="discussion_action" value="${action}"/>
      </fieldset>
    </form>
  </py:def>

  <py:def function="display_preview()">
    <li py:if="req.args.preview" class="preview">
      <a name="preview"></a>
      <div class="id">
          Message #??
      </div>
      <div class="body">
        ${discussion.body or Markup('&lt;p&gt;&nbsp;&lt;/p&gt;')}
      </div>
      <div class="footer">
        <div class="author">
          ${discussion.author}
        </div>
        <div class="time">
          ${discussion.time}
        </div>
      </div>
    </li>
  </py:def>

  <py:def function="display_set_display(href)">
    <div class="set-display">
      <a href="${href}?discussion_action=set-display;display=tree">Tree View</a>
      <a href="${href}?discussion_action=set-display;display=flat-desc">Flat View (newer first)</a>
      <a href="${href}?discussion_action=set-display;display=flat-asc">Flat View (older first)</a>
    </div>
  </py:def>

  <py:def function="display_messages(href, messages)">
    <li py:for="message in messages" class="${message.new and 'new' or None}">
      <a name="${message.id}"></a>

      <py:with vars="is_message_edit = (str(req.args.message) == str(message.id)) and (req.args.discussion_action in ('edit', 'post-edit')) and not req.args.submit;">
        <div class="id">
          Message #${message.id}
        </div>

        <div class="body" py:choose="">
          <py:when test="is_message_edit">
            ${discussion.body or Markup('&lt;p&gt;&nbsp;&lt;/p&gt;')}
          </py:when>
          <py:otherwise>
            ${message.body or Markup('&lt;p&gt;&nbsp;&lt;/p&gt;')}
          </py:otherwise>
        </div>

        <div class="controls">
          <py:if test="'DISCUSSION_APPEND' in perm">
            <a href="${href}?discussion_action=add;message=${message.id}#reply">Reply</a>
            <a href="${href}?discussion_action=quote;message=${message.id}#reply">Quote</a>
            <a py:if="('DISCUSSION_MODERATE' in perm and discussion.is_moderator) or ((discussion.authname == message.author) and (discussion.authname != 'anonymous'))" href="${href}?discussion_action=edit;message=${message.id}#reply">Edit</a>
          </py:if>
          <a py:if="'DISCUSSION_MODERATE' in perm and discussion.is_moderator" href="${href}?discussion_action=delete;message=${message.id}" onclick="return confirm('Do you realy want to delete this reply and all its descendants?')">Delete</a>
        </div>

        <div class="footer">
          <div class="author">
            ${is_message_edit and discussion.author or message.author}
          </div>
          <div class="time">
            ${is_message_edit and discussion.time or message.time}
          </div>
        </div>
      </py:with>

      <ul class="reply">
        <py:if test="(str(req.args.message) == str(message.id)) and (not req.args.submit)">
          <py:if test="req.args.discussion_action in ('add', 'quote', 'post-add')">
            ${display_preview()}
            <li>
              ${display_form(href, 'Reply:', 'add_form', 'post-add')}
            </li>
          </py:if>

          <py:if test="req.args.discussion_action in ('edit', 'post-edit')">
            <li>
              ${display_form(href, 'Edit:', 'edit_form', 'post-edit')}
            </li>
          </py:if>
        </py:if>

        <py:if test="message.replies and len(message.replies)">
          ${display_messages(href, message.replies)}
        </py:if>
        <li></li>
      </ul>

    </li>
  </py:def>

</html>
