 
   # Returns true if the chat is scrolled to the bottom of the screen.
   # This also returns true if the messages are hidden.
-  isAtBottom: =>
-    $el = @$('.popup_messages')
+  isAtBottom: ($popup) =>
+    $el = $popup.find('.popup_messages')
     ($el[0].scrollHeight - $el.scrollTop() <= $el.outerHeight())
 
-  scrollToBottom: =>
-    messages = @$('.popup_messages')[0]
+  scrollToBottom: ($popup) =>
+    messages = $popup.find('.popup_messages')[0]
     messages.scrollTop = messages.scrollHeight
     false
 

        messageHtml += "<p class='log-message'><strong>#{username}:</strong> #{message}</p>"

    $messages = $popup.find('.popup_messages').append($(messageHtml))
    @scrollToBottom()

  # todo: make this and addLogMessages construct messages from a common source
  receiveMessage: (messageId, username, message, options) =>
    $popup = @$findOrCreatePopup(messageId)
    $messages = $popup.find('.popup_messages')
    wasAtBottom = @isAtBottom()
    if options.type == 'error'
      $messages.append("<p class='privmsg-error'>#{message}</p>")
    else if options.type == 'alert'

        $popup.addClass('new_message')
        PokeBattle.notifyUser(PokeBattle.NotificationTypes.PRIVATE_MESSAGE, username)
      $messages.append("<p><strong>#{username}:</strong> #{message}</p>")
    if wasAtBottom then @scrollToBottom($popup)

  openChallenge: (messageId, generation, conditions) =>
    $popup = @$findOrCreatePopup(messageId)
