       $document.off 'keydown', keyPressed
 
     # ===== GETTERS =======================================
-        
+
     $scope.shouldShowNewButton = () ->
       return false if $scope.panelStack.length >= wizardMaxDepth
       true
-    
+
     # ===== SETTERS =======================================
 
     $scope.panelHover = (hoveredIndex) ->

    link: link
  }
]


  # ===== LINK ================================================================

  link = ($scope, element, attrs, controller) ->

    $scope.loading = true
    $scope.errors = []

    $scope.setOptions = (resource, item, formMode, wizardMaxDepth, parent) ->

      panelDescriptor = {
        resource : resource
        item : item
        formMode : formMode
        wizardMaxDepth : wizardMaxDepth
        parent : parent
        directive : directive
      }
      $scope.panelStack = [panelDescriptor]
      $scope.finish()


    $scope.finish = (error)->

      $scope.loading = false
      $scope.errors.push(error) if error
      # Interesting
      # Without this $scope.$apply the form is loaded really slowly
      # or not at all.
      $scope.$apply()
      throw new Error(error) if error

    if $scope.csWizardOptions
      resource_type = $scope.csWizardOptions['resource-type']
      #If the csWizardOptions arrives then descriptors are surely loaded
      #so there is no need for the csDescriptorService.getPromises().then
      resource = angular.copy(ResourceService.get(resource_type))
      item = $scope.csWizardOptions['form-item']
      formMode = $scope.formMode || $scope.csWizardOptions['form-mode']
      parent =  null
      wizardMaxDepth = $scope.csWizardOptions['max-depth'] || 5

      if $scope.csWizardOptions['resource-overrides'] && (override = $scope.csWizardOptions['resource-overrides'][resource_type])
        if override.directive?
          directive = override.directive

      $scope.setOptions(resource, item, formMode, wizardMaxDepth, parent, directive)

    else

      $scope.csWizardOptions = {}
      # TODO Check if all the parameters arrived
      # { resourceType, id, pageType}
      resource_type = $scope.resourceType
      formMode = $scope.pageType 
      $scope.csWizardOptions['form-mode'] = formMode
      wizardMaxDepth = 5
      csDescriptorService.getPromises()
      .then(()->
        return ResourceService.get($scope.resourceType)
      ).catch((error) ->
        errorMsg = "\"" + $scope.resourceType + "\" is not a registered resource"
        $scope.finish(errorMsg)
      ).then((_resource_) ->
          resource = angular.copy(_resource_)
          # Getting the resourc data
          resource.$get($scope.itemId, {include: '*'})
      ).catch((reason) ->
          # TODO process the reason
          errorMsg = "There is no " + resource.descriptor.name + " with the id: " + $scope.itemId
          $scope.finish(errorMsg)
      ).then((_item_)->
          item = _item_
      ).then( ()->
        #Setting events
        wizardCanceled = (() ->
          csRoute.go("index", {resourceType : resource.descriptor.type})
        ).bind(resource)

        wizardSubmitted =  (()->
          switch(formMode)
            when "create" then csAlertService.success('new_resource_created')
            when "edit" then csAlertService.success("changes_saved")
          csRoute.go("index", {resourceType : resource.descriptor.type})
        ).bind(formMode, resource)

        $scope.csWizardOptions.events = {
          'wizard-canceled' : wizardCanceled
          'wizard-submitted' : wizardSubmitted
        }
        $scope.setOptions(resource, item, formMode, wizardMaxDepth)
    )

    if $scope.panelNumberCallback
      $scope.panelNumberCallback($scope.panelStack.length)

