#FB.init appId: '168928633304849'

#Ember.Application.initializer
  #name: 'authentication',
  #initialize: (container, application)->
    ## register the Facebook authenticator so the session can find it
    #container.register 'authenticators:facebook', App.FacebookAuthenticator
    #Ember.SimpleAuth.setup(container, application)

@App = Ember.Application.create
  LOG_TRANSITIONS: true
  rootElement: '#ember-app-container'
  obtain_token: (message)->
    message ||= ''
    window.location = "#{$root_url}/obtain_token?message=#{message}"

Ember.$.ajaxPrefilter (options) ->
  if options.type.toUpperCase() == 'GET'
    if auth_token = Qstorage.getItem('auth_token')
      if options.data
        options.data += "&auth_token=#{auth_token}"
      else
        options.data = "auth_token=#{auth_token}"
  if options.type.toUpperCase() == 'POST'
    if auth_token = Qstorage.getItem('auth_token')
      if options.data
        if typeof(options.data) == 'string'
          if options.data[0] == '{' || options.data[0] == '[' # json
            object = JSON.parse(options.data)
            object.auth_token = auth_token
            options.data = JSON.stringify(object)
          else
            options.data += "&auth_token=#{auth_token}"
        else
          options.data.auth_token = auth_token
      else
        options.data = "auth_token=#{auth_token}"
  true

Ember.$.ajaxSetup
  error: (jqXHR, textStatus, errorThrown)->
    console.log "Error: #{textStatus}: #{errorThrown}"
    if jqXHR.status == 401
      App.__container__.lookup('route:application').unauthorized()
