ControllerExtensions = Ember.Mixin.create
  ajaxError: (callback)->
    handler = (emberError)=>
      console.log "Error: status #{emberError.status}"
      if emberError.status == 401
        App.__container__.lookup('route:application').unauthorized()
      else
        callback.call(@, emberError)
    handler
  showModal: (options={})->
    $(document).foundation('reflow') # needed (stupid!!!)
    @confirm_cancel = options.cancel
    @set 'controllers.application.modal.title', options.title if options.title
    @set 'controllers.application.modal.content', options.content if options.content
Ember.ArrayController.reopen ControllerExtensions
Ember.Controller.reopen ControllerExtensions
Ember.Controller.reopen
  needs: ['application']
  secured: (callback)->
    unless Qstorage.getItem('auth_token') && typeof(Qstorage.getItem('auth_token')) == 'string' && Qstorage.getItem('auth_token').length > 0
      return App.obtain_token()
    @authentication_string = 'auth_token='+Qstorage.getItem('auth_token')
    @authentication_object = {auth_token: Qstorage.getItem('auth_token')}
    callback.call(@) if callback
  redirect_to: (route, args...)->
    route = 'index' if route == 'user_root'
    route_object= App.Router.router.recognizer.names[route]
    throw "Route #{route} cannot exist" unless route_object
    dynamic_segments = route_object.segments.reduce (sum, segment) ->
      if segment.name then sum + 1 else sum
    , 0
    route_args = [route]

    if dynamic_segments # needed because javascript for loop always fires, even when end is less than start :'(
      for isegment in [1..dynamic_segments]
        route_args.push args.shift()

    options = args.pop() || {}

    #route_args.push args if a
    @transitionToRoute.apply(@,route_args).then =>
      if options.message
        tkey = if options.message.indexOf('.') > -1 then options.message else "messages.#{options.message}"
        @set 'controllers.application.notice', t(tkey)
  #confirm: (options = {})->
    ##@showModal options
    ##$(document).foundation('reflow') # needed (stupid!!!)
    #r = @container.lookup('route:application')
    #r.send('confirm', options)
    ##window.confirm_cancel = options.cancel
    ##window.confirm_ok = options.ok
    ##$('#confirm-modal .confirm-title').html options.title if options.title
    ##$('#confirm-modal .confirm-content').html options.content if options.content
    ##@set 'controllers.application.confirm.content', options.content if options.content
    ##$('#confirm-modal').foundation('reveal', 'open') # this kills the ember actions
    ##$('#confirm-modal').css('visibility', 'visible').show()
    #$('#confirm-modal').foundation('reveal', 'open') #this kills the ember actions
    #$('#confirm-modal').css('visibility', 'visible').show()

Ember.ArrayController.reopen
  needs: ['application']
  secured: (callback)->
    unless Qstorage.getItem('auth_token') && typeof(Qstorage.getItem('auth_token')) == 'string' && Qstorage.getItem('auth_token').length > 0
      return App.obtain_token()
    @authentication_string = 'auth_token='+Qstorage.getItem('auth_token')
    @authentication_object = {auth_token: Qstorage.getItem('auth_token')}
    callback.call(@) if callback
  redirect_to: (route, options={})->
    route = 'index' if route == 'user_root'
    @transitionToRoute(route).then =>
      if options.message
        tkey = if options.message.indexOf('.') > -1 then options.message else "messages.#{options.message}"
        @set 'controllers.application.notice', t(tkey)

Ember.ObjectController.reopen
  needs: ['application']
