                         return "presentation"
 
                     when "pdf" then return sub[1]
+
                     when "gzip", "zip" then return 'archive'
 
 
+    # Format date to a conventional format for reply headers.
     formatReplyDate: (date) ->
         date = moment() unless date?
         date = moment date
         date.format 'lll'
 
 
+    # Display date as a readable string.
+    # Make it shorter if compact is set to true.
     formatDate: (date, compact) ->
+
         unless date?
-            return
-        today = moment()
-        date  = moment date
-        if date.isBefore today, 'year'
-            formatter = 'DD/MM/YYYY'
-        else if date.isBefore today, 'day'
-            if compact? and compact
-                formatter = 'L'
+            return null
+
+        else
+            today = moment()
+            date  = moment date
+
+            if date.isBefore today, 'year'
+                formatter = 'DD/MM/YYYY'
+
+            else if date.isBefore today, 'day'
+
+                if compact? and compact
+                    formatter = 'L'
+                else
+                    formatter = 'MMM DD'
+
             else
-                formatter = 'MMM DD'
-        else
-            formatter = 'HH:mm'
-        return date.format formatter
-
+                formatter = 'HH:mm'
+
+            return date.format formatter
+
+
+    # Return avatar corresponding to sender by matching his email address with
+    # addresses from existing contacts.
     getAvatar: (message) ->
         if message.get('from')[0]?
             return ContactStore.getAvatar message.get('from')[0].address
         else
             return null
-
-    # Delete message(s) or conversations
-    #
-    # @params {Mixed}    ids          messageID or Message or array of
-    #                                 messageIDs or Messages
-    # @params {Boolean}  conversation true to delete whole conversation
-    # @params {Function} cb           callback
-    delete: (ids, conversation, cb) ->
-        @action 'delete', ids, conversation, null, cb
-
-    move: (ids, conversation, from, to, cb) ->
-        options =
-            from: from
-            to: to
-        @action 'move', ids, conversation, options, cb
-
-    action: (action, ids, conversation, options, cb) ->
-
-        alertError   = LayoutActionCreator.alertError
-        if Array.isArray ids
-            mass = ids.length
-            selected = ids
-        else
-            mass = 1
-            selected = [ids]
-
-        # If we deleted only one message, we'll navigate to the next one,
-        # otherwise close preview panel and select first message
-        if selected.length > 1
-            window.cozyMails.messageClose()
-        else
-            next = MessageStore.getNextMessage conversation
-
-        # Called once every message has been deleted
-        onDone = _.after selected.length, ->
-            if typeof cb is 'function'
-                cb()
-
-        # Handle one message
-        handleMessage = (messageID) ->
-            switch action
-                when 'move'
-                    MessageActionCreator.move messageID, options.from, options.to, (error) ->
-                        if error?
-                            alertError "#{t("message action move ko")} #{error}"
-                        onDone()
-                when 'delete'
-                    MessageActionCreator.delete messageID, (error) ->
-                        if error?
-                            alertError "#{t("message action delete ko")} : #{error}"
-                        onDone()
-
-        # Handle one conversation
-        handleConversation = (message) ->
-            messageID = message.get 'id'
-            # sometime, draft messages don't have a conversationID
-            conversationID = message.get 'conversationID'
-            if conversationID?
-                switch action
-                    when 'move'
-                        ConversationActionCreator.move message, options.from, options.to, (error) ->
-                            if error?
-                                alertError "#{t("message action move ko", subject: message.get('subject'))} #{error}"
-                            onDone()
-                    when 'delete'
-                        ConversationActionCreator.delete conversationID, (error) ->
-                            if error?
-                                alertError "#{t("conversation delete ko", subject: message.get('subject'))} : #{error}"
-                            onDone()
-            else
-                handleMessage(messageID)
-
-        selected.forEach (messageID) ->
-            if conversation
-                if typeof messageID is 'string'
-                    message = MessageStore.getByID messageID
-                else
-                    message   = messageID
-                handleConversation message
-            else
-                if typeof messageID isnt 'string'
-                    messageID = messageID.get 'id'
-                handleMessage messageID
-        if next?
-            MessageActionCreator.setCurrent next.get('id'), true
-            # open next message if the deleted one was open
-            window.cozyMails.messageDisplay next, false
 
 
     # Remove from given string:

                return address.address.split('@')[0]


    displayAddresses: (addresses, full = false) ->
        if not addresses?
            return ""

        res = []
        for item in addresses
            if not item?
                break
            res.push(MessageUtils.displayAddress item, full)
        return res.join ", "


    # From a text, build an address object (name and address).
    # Add a isValid field to
    parseAddress: (text) ->
        text = text.trim()
        if match = text.match /"{0,1}(.*)"{0,1} <(.*)>/

        if subject.indexOf(replyPrefix) isnt 0
            subject = "#{replyPrefix}#{subject}"
        subject

    # TODO put functions below in an other module.

    # Delete message(s) or conversations.
    #
    # @params {Mixed}    ids          messageID or Message or array of
    #                                 messageIDs or Messages
    # @params {Boolean}  conversation true to delete whole conversation
    # @params {Function} cb           callback
    delete: (ids, conversation, cb) ->
        @action 'delete', ids, conversation, null, cb


    # Perform a move operation on given conversation.
    move: (ids, conversation, from, to, cb) ->
        options =
            from: from
            to: to
        @action 'move', ids, conversation, options, cb


    action: (action, ids, conversation, options, cb) ->

        if Array.isArray ids
            mass = ids.length
            selected = ids
        else
            mass = 1
            selected = [ids]

        # If we deleted only one message, we'll navigate to the next one,
        # otherwise close preview panel and select first message
        if selected.length > 1
            window.cozyMails.messageClose()
        else
            next = MessageStore.getNextMessage conversation


        # Called once every message has been deleted
        onDone = _.after selected.length, ->
            if typeof cb is 'function'
                cb()


        # Handle one message
        handleMessage = (id) ->
            switch action

                when 'move'

                    {from, to} = options
                    MessageActionCreator.move id, from, to, (error) ->
                        if error?
                            msg = "#{t("message action move ko")} #{error}"
                            LayoutActionCreator.alertError msg
                        onDone()

                when 'delete'
                    MessageActionCreator.delete id, (error) ->
                        if error?
                            msg = "#{t("message action delete ko")} : #{error}"
                            LayoutActionCreator.alertError msg
                        onDone()


        # Handle one conversation
        handleConversation = (message) ->

            messageID = message.get 'id'

            # sometime, draft messages don't have a conversationID
            conversationID = message.get 'conversationID'

            if conversationID?
                params = subject: message.get('subject')

                switch action

                    when 'move'
                        {from, to} = options
                        ConversationActionCreator.move message, from, to, (error) ->
                            if error?
                                msg = "#{t("message action move ko", params)} #{error}"
                                LayoutActionCreator.alertError msg
                            onDone()

                    when 'delete'
                        ConversationActionCreator.delete conversationID, (error) ->
                            if error?
                                msg = "#{t("conversation delete ko", params)} : #{error}"
                                LayoutActionCreator.alertError msg
                            onDone()
            else
                handleMessage(messageID)


        selected.forEach (id) ->

            if conversation
                if typeof id is 'string'
                    message = MessageStore.getByID id
                else
                    message = id
                handleConversation message

            else
                if typeof id isnt 'string'
                    id = id.get 'id'
                handleMessage id


        if next?
            MessageActionCreator.setCurrent next.get('id'), true

            # open next message if the deleted one was open
            window.cozyMails.messageDisplay next, false


