     date &&= date.toString("dddd, d MMM yyyy h:mm:ss tt")
 
   messagesCount: ->
-    $('#messages tr').length - 1
+    $("#messages tr").length - 1
 
   tabs: ->
-    $('#message ul').children('.tab')
+    $("#message ul").children(".tab")
 
   getTab: (i) =>
     $(@tabs()[i])
 
   selectedTab: =>
-    @tabs().index($('#message li.tab.selected'))
+    @tabs().index($("#message li.tab.selected"))
 
   openTab: (i) =>
-    @getTab(i).children('a').click()
+    @getTab(i).children("a").click()
 
   previousTab: (tab)=>
     i = if tab || tab is 0 then tab else @selectedTab() - 1

#= require xslt

# Add a new jQuery selector expression which does a case-insensitive :contains
jQuery.expr[':'].icontains = (a, i, m) ->
  (a.textContent ? a.innerText ? "").toUpperCase().indexOf(m[3].toUpperCase()) >= 0

class MailCatcher
  constructor: ->
    $('#messages tr').live 'click', (e) =>
      e.preventDefault()
      @loadMessage $(e.currentTarget).attr 'data-message-id'

    $('input[name=search]').keyup (e) =>
      query = $.trim $(e.currentTarget).val()
      if query
        @searchMessages query
      else
        @clearSearch()

    $('#message .views .format.tab a').live 'click', (e) =>
      e.preventDefault()
      @loadMessageBody @selectedMessage(), $($(e.currentTarget).parent('li')).data 'message-format'

    $('#message .views .analysis.tab a').live 'click', (e) =>
      e.preventDefault()
      @loadMessageAnalysis @selectedMessage()

    $('#message iframe').load =>
      @decorateMessageBody()

    $('#resizer').live
      mousedown: (e) =>
        e.preventDefault()
        $(window).bind events =


    @resizeToSaved()

    $("nav.app .clear a").live "click", (e) =>
      e.preventDefault()
      if confirm "You will lose all your received messages.\n\nAre you sure you want to clear all messages?"
        $.ajax
          url: "/messages"
          type: "DELETE"
          success: =>
            @unselectMessage()
          error: ->
            alert "Error while clearing all messages."

    $("nav.app .quit a").live "click", (e) =>
      e.preventDefault()
      if confirm "You will lose all your received messages.\n\nAre you sure you want to quit?"
        $.ajax
          type: "DELETE"
          success: ->
            location.replace $("body > header h1 a").attr("href")
          error: ->
            alert "Error while quitting."

    key "up", =>
      if @selectedMessage()
        @loadMessage $("#messages tr.selected").prev().data("message-id")
      else
        @loadMessage $("#messages tbody tr[data-message-id]:first").data("message-id")
      false

    key "down", =>
      if @selectedMessage()
        @loadMessage $("#messages tr.selected").next().data("message-id")
      else
        @loadMessage $("#messages tbody tr[data-message-id]:first").data("message-id")
      false

    key "⌘+up, ctrl+up", =>
      @loadMessage $("#messages tbody tr[data-message-id]:first").data("message-id")
      false

    key "⌘+down, ctrl+down", =>
      @loadMessage $("#messages tbody tr[data-message-id]:last").data("message-id")
      false

    key "left", =>
      @openTab @previousTab()
      false

    key "right", =>
      @openTab @nextTab()
      false

    key "backspace, delete", =>
      id = @selectedMessage()
      if id?
        $.ajax
          url: "/messages/" + id
          type: "DELETE"
          success: =>
            messageRow = $("""#messages tbody tr[data-message-id="#{id}"]""")
            switchTo = messageRow.next().data("message-id") || messageRow.prev().data("message-id")
            messageRow.remove()
            if switchTo
              @loadMessage switchTo
