             'django-cradmin-floating-fullsize-iframe-bodycontentwrapper')
           djangoCradminModelChoiceFieldCoordinator.addBodyContentWrapperClass(
             'django-cradmin-floating-fullsize-iframe-bodycontentwrapper-push')
+          djangoCradminWindowDimensions.register $scope
 
         $scope.hide = ->
           $scope.iframeWrapperElement.removeClass('django-cradmin-floating-fullsize-iframe-wrapper-show')

            return
          $scope.fieldScope.setValue(data.value)
          $scope.previewElementScope.setPreviewHtml(data.preview)
          djangoCradminWindowDimensions.unregister $scope
          $scope.hide()
          $scope.iframeScope.afterFieldValueChange()


      return
  }

.directive('djangoCradminModelChoiceFieldIframe', [
  '$interval'
  ($interval) ->
    return {
      require: '^djangoCradminModelChoiceFieldIframeWrapper'
      restrict: 'A'
      scope: {}

      controller: ($scope) ->
        scrollHeightInterval = null
        currentScrollHeight = 0

        getIframeWindow = ->
          return $scope.element.contents()

        getIframeDocument = ->
          return getIframeWindow()[0]

        getIframeScrollHeight = ->
          iframeDocument = getIframeDocument()
          if iframeDocument?.body?
            return iframeDocument.body.scrollHeight
          else
            return 0

        resizeIfScrollHeightChanges = ->
          newScrollHeight = getIframeScrollHeight()
          console.log 'newScrollHeight tick', newScrollHeight
          if newScrollHeight != currentScrollHeight
            currentScrollHeight = newScrollHeight
            $scope.setIframeSize()

        startScrollHeightInterval = ->
          if not scrollHeightInterval?
            scrollHeightInterval = $interval(resizeIfScrollHeightChanges, 500)

        stopScrollHeightInterval = ->
          if scrollHeightInterval?
            $interval.cancel(scrollHeightInterval)
            scrollHeightInterval = null

        $scope.onHide = ->
          stopScrollHeightInterval()

        $scope.afterFieldValueChange = ->
          # NOTE: We may want to add an option that clears the view
          #       after selecting a value.
          # $scope.element.attr('src', '')
          stopScrollHeightInterval()

        $scope.beforeShowingIframe = (iframeSrc) ->
          currentSrc = $scope.element.attr('src')
          if not currentSrc? or currentSrc == '' or currentSrc != iframeSrc
            $scope.loadedSrc = currentSrc
            $scope.wrapperCtrl.onIframeLoadBegin()
            $scope.resetIframeSize()
            $scope.element.attr('src', iframeSrc)
          startScrollHeightInterval()

        $scope.setIframeSize = ->
          iframeDocument = getIframeDocument()
          if iframeDocument?.body?
            iframeBodyHeight = iframeDocument.body.offsetHeight
            $scope.element.height(iframeBodyHeight)

        $scope.resetIframeSize = ->
          $scope.element.height('40px')

        return

      link: (scope, element, attrs, wrapperCtrl) ->
        scope.element = element
        scope.wrapperCtrl = wrapperCtrl
        wrapperCtrl.setIframe(scope)
        scope.element.on 'load', ->
          wrapperCtrl.onIframeLoaded()
          scope.setIframeSize()
        return
    }
])


.directive('djangoCradminModelChoiceFieldWrapper', [
