{{define "title"}}{{printf "Inbucket for %v" .name}}{{end}}
{{$name := .name}}

{{define "script"}}
<script>
  var selected = "{{.selected}}"
  function messageLoaded(responseText, textStatus, XMLHttpRequest) {
    if (textStatus == "error") {
      alert("Failed to load message, server said:\n" + responseText)
    } else {
      window.scrollTo(0,0)
    }
  }

  function listLoaded() {
    $('.listEntry').hover(
    function() {
      $(this).addClass("listEntryHover")
    },
    function() {
      $(this).removeClass("listEntryHover")
    }
    ).click(
    function() {
      $('.listEntry').removeClass("listEntrySelected")
      $(this).addClass("listEntrySelected")
      $('#emailContent').load('/mailbox/{{.name}}/' + this.id, messageLoaded)
    }
    )
    $("#messageList").slideDown()
    if (selected != "") {
      $("#" + selected).click()
      selected = ""
    }
  }

  function loadList() {
    $('#messageList').load("/mailbox/{{.name}}", listLoaded)
  }

  function reloadList() {
    $('#messageList').hide()
    loadList()
  }

  function listInit() {
    $("#messageList").hide()
    loadList()
  }

  function deleteMessage(id) {
    $('#emailContent').empty()
    $.ajax({
      type: 'DELETE',
      url: '/mailbox/{{.name}}/' + id,
      success: reloadList
    })
  }

  function htmlView(id) {
    window.open('/mailbox/{{.name}}/' + id + "/html", '_blank',
    'width=800,height=600,' +
    'menubar=yes,resizable=yes,scrollbars=yes,status=yes,toolbar=yes')
  }

  function messageSource(id) {
    window.open('/mailbox/{{.name}}/' + id + "/source", '_blank',
    'width=800,height=600,' +
    'menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no')
  }

  $(document).ready(listInit)
</script>
{{end}}

{{define "menu"}}
<div id="logo">
  <h1><a href="#">inbucket</a></h1>
  <h2>mail for {{.name}}</h2>
</div>
<div class="box" style="text-align:center; padding-bottom:10px;">
  <a href="javascript:reloadList()">Refresh List</a>
</div>
<div id="messageList"></div>
{{end}}

{{define "content"}}
<div id="emailContent">
  <p>Select a message at left, or enter a different username into the box on upper right.</p>
</div>
{{end}}

