this.App =
  debug: (msg) ->
    console.log msg

  UI:
    setAutoHeight: ->
      height = $(window).height()
      $("#height-setter-1").css({height: height - 50})
      $("#height-setter-2").css({height: height - $('#tabbar').outerHeight() - $('#msg-writer').outerHeight() - 57})

  Com:
    connect: (callback) ->
      App._dispatcher = new WebSocketRails('www.xmpp.dev:3000/websocket')
      App._dispatcher.on_open = =>
        @_setupBackboneComponents()
        @_bindEvents()
        callback?()

    fetch: (options) ->
      _.defaults(options, {data: {} })
      App._dispatcher.trigger(options.event, options.data, options.success, options.error)

    initRoster: ->
      App.Com.fetch(
        event: 'app.roster.initRoster'
        success: (data) ->
          _.each(data.contacts, (jid) ->
            newContact = new Xmpp.Models.Contact(id: jid, jid: jid)
            App.Collections.Contacts.add newContact
          )
      )

    startFetchingVcards: ->
      App.Com.fetch(event: 'app.roster.startFetchingVcards')

    startPollingRoster: ->
      App.Com.fetch(event: 'app.roster.startPolling')

    setPresence: ->
      App.Com.fetch(event: 'app.roster.setPresence')

    _setupBackboneComponents: ->
      App.Collections.Contacts = new Xmpp.Collections.ContactsCollection()

    _bindEvents: ->
      App._dispatcher.bind('app.roster.statusChanged', (result) ->
        App.debug 'change contact state'
        App.Collections.Contacts.updateStatus(result)
      )

      App._dispatcher.bind('app.roster.vcard', (result) ->
        App.debug 'got vcard'
        App.Collections.Contacts.udpateVcard(result)
      )

  Collections:
    Contacts: null