<div class="mail_thread" xmlns:py="http://genshi.edgewall.org/">
  <script type="text/javascript">
    jQuery(document).ready(function() {
      initMailThread('mail_thread_${id}');
    });
  </script>
  <py:def function="show_thread(mail, cached_mails, highlight_ids=[], flatmode=False)">
    <ul class="thread_ul">
      <li class="thread_li">
        <py:choose>
          <py:when test="not mail.has_permission(req)">
            (表示権限がありません)
          </py:when>
          <py:otherwise test="">
            <py:choose>
              <?python
              	highlight_css = mail.id in highlight_ids and 'related_mail' or ''
              ?>
              <a py:when="mail.has_attachments(req)" href="${req.href.mailarchive(mail.id)}" class="mail_clickable mail_subject thread_subject_clip ${highlight_css}">
              [$mail.id] ${mail.get_subject()}</a>
              <a py:otherwise="" href="${req.href.mailarchive(mail.id)}" class="mail_clickable mail_subject ${highlight_css}">[$mail.id] ${mail.get_subject()}</a>
              <py:with vars="related_tickets=mail.get_related_tickets(req);">
                <py:if test="len(related_tickets) > 0">
                  <span class="related_tickets">
                    --- (関連チケット: 
                    <py:for each="ticket in related_tickets">
                      <a href="${req.href.ticket(ticket.id)}">#${ticket.id}</a>&nbsp;
                    </py:for>
                    )
                  </span>
                </py:if>
              </py:with>
              <py:if test="'MAILARCHIVE_REPLYMAIL' in req.perm('mailarchive')">
                &nbsp;&nbsp;<a href="" class="reply_mail mail_clickable" title="返信する">&nbsp;</a>
              </py:if>
              &nbsp;&nbsp;<a class="open_mail" target="_brank"
                href="${req.href.mailarchive(mail.id)}" title="別ウィンドウで開く">&nbsp;</a>
            </py:choose>
            <br />
            <span class="thread_from">${mail.get_fromtext()}</span>
            <span class="thread_senddate">${mail.get_senddate()}</span>
            <div class="mail_id" style="display:none;">${mail.id}</div>
            <div class="mailreplyplaceholder" style="display:none"></div>
            <div class="mail_loading" style="display:none;">&nbsp;</div>
            <div class="mailplaceholder"></div>
          </py:otherwise>
        </py:choose>
    
        <py:if test="flatmode is False">
          <py:for each="child in mail.get_children(cached_mails=cached_mails)">
            ${show_thread(child, cached_mails, flatmode=flatmode, highlight_ids=highlight_ids)}
          </py:for>
        </py:if>
      </li>
    </ul>
  </py:def>
  
  <py:def function="show_onethread(mail, highlight_ids=highlight_ids)">
    <div id="mail_thread_${id}" class="mail_thread">
      ${show_thread(mail.get_thread_root(), mail.get_thread_mails(), highlight_ids=highlight_ids)}
    </div>
  </py:def>
  
  <py:def function="show_threads(mail)">
    <?python
      wrote_ids=set()
	?>
    <div id="mail_thread_${id}" class="mail_threads">
      <py:for each="mail in mails">
        <fieldset py:if="mail.id not in wrote_ids" class="options collapsed">
          <?python
            mail_ids = [x.id for x in mails]
            thread_mails = mail.get_thread_mails()
            thread_mails.append(mail.get_thread_root())
            wrote_ids.update([x.id for x in thread_mails])
            related_ids = [x for x in mail_ids if x in [y.id for y in thread_mails]]
		  ?>
          <legend class="foldable" style="! important">${str(related_ids)} ${mail.subject} </legend>
          <span class="thread_from">${mail.get_fromtext()}</span>
          <span class="thread_senddate">${mail.get_senddate()}</span>
          <div class="mail_thread">
            ${show_thread(mail.get_thread_root(), thread_mails, highlight_ids=mail_ids)}
          </div>
        </fieldset>
      </py:for>
    </div>
  </py:def>
</div>