this.App =
  debug: () ->
    return if arguments.length == 0
 
    if arguments.length == 1
      message = arguments[0]
    else
      message = arguments
 
    console.log message
 
  stripJid: (jid) ->
    r = new RegExp('(^.*)\/.*$')
    match = r.exec(jid)
    if match and (match.length > 0)
      return match[1]
    else
      return jid
 
  UI:
    setAutoHeight: ->
      App.debug 'resizing'
      App.UI.resizeRoster()
      App.UI.resizeMessages()
 
    resizeRoster: ->
      if (windowHeight < $('.roster').css('min-height').replace(/[^-\d\.]/g, ''))
        return
 
      windowHeight = $(window).height()
      toolboxHeight = $("#height-setter-1 .toolbox").height()
      $(".friends").css({height: windowHeight - toolboxHeight - $('#js-me').height() - 40})
      $("#height-setter-1").css({height: windowHeight - toolboxHeight - $('#js-active-friends').height() - 120})
 
    resizeMessages: ->
      windowHeight = $(window).height()
      $("#height-setter-2").css({height: windowHeight - $('#tabbar').outerHeight() - $('#msg-writer').outerHeight() - 62})
      messagesContainer = $('.messages')
      messagesContainer.animate(scrollTop: messagesContainer.height(), 'fast')
 
    filterContacts: (searchTerm) ->
      App.Collections.contacts.filter(searchTerm)
 
  Com:
    connect: (callback) ->
      App._dispatcher = new WebSocketRails('www.xmpp.dev:3000/websocket')
      App._dispatcher.on_open = =>
        @_setupBackboneComponents()
        @_bindListeners(callback)
 
    trigger: (options) ->
      _.defaults(options, {data: {} })
      App._dispatcher.trigger(options.event, options.data, options.success, options.error)
 
    initRoster: (callback) ->
      App.Com.trigger(
        event: 'app.roster.initRoster'
        success: (data) ->
          _.each(data.contacts, (contact) ->
            newContact = new Xmpp.Models.Contact(
              id: contact.jid
              jid: contact.jid
              belongsTo: [contact.belongsTo]
            )
            App.Collections.contacts.add(newContact, merge: true)
          )
 
          callback?()
      )
 
    startFetchingVcards: ->
      App.Com.trigger(event: 'app.roster.startFetchingVcards')
 
    startPollingRoster: ->
      App.Com.trigger(event: 'app.roster.startPolling')
 
    startPollingMessages: ->
      App.Com.trigger(event: 'app.chat.startPollingMessages')
 
    setPresence: ->
      App.Com.trigger(event: 'app.roster.setPresence')
 
    getMe: ->
      App.Com.trigger(event: 'app.roster.myself', success: (response) ->
        App.Models.me = new Xmpp.Models.Me(
          jid: response.jid
          name: response.vcard.name
          status: response.status
          avatar: response.vcard.avatar
        )
      )
 
    sendMessage: (message, to, from, callbackOk, callbackFail) ->
      App.Com.trigger(event: 'app.chat.sendMessage', data: {message: message, to: to, from: from}, success: callbackOk, error: callbackFail)
 
    sendMultiMessage: (message, chatId, from, callbackOk, callbackFail) ->
      App.Com.trigger(event: 'app.chat.sendMessage', data: {message: message, chatId: chatId, from: from}, success: callbackOk, error: callbackFail)
 
    openNewMultiChat: (chatOwner, attendant, chat) ->
      @trigger(event: 'app.chat.newMultiChat', data: {chatOwner: chatOwner.get('jid')}, success: (response) =>
        chat.setChatId(response.id)
        chat.appendWithWhom(attendant)
        @trigger(event: 'app.chat.addToMultiChat', data: {chatOwner: chatOwner.get('jid'), chatId: response.id, jid: attendant.get('jid')}, success: ->