 
       channel.bind 'wants', (trans, {what, data}) -> scope.wants {what, data}
 
-      for link in $window.document.getElementsByTagName('link') then do (link) ->
+      for link in win.document.getElementsByTagName('link') then do (link) ->
         channel.call
           method: 'style'
           params:


  toolCssLoaded = {}

  Directives.directive 'nativeTool', ($window, $compile, $injector) ->
    restrict: 'E'
    scope:
      tool: '=tool'
      actions: '=actions'
      state: '=state'
    link: (scope, element, attrs) ->

      scope.$watch 'tool.controllerURI', ->
        if scope.tool

          if scope.tool.style and not toolCssLoaded[scope.tool.style]
            link = $window.document.createElement('link')
            link.rel = 'stylesheet'
            link.href = scope.tool.style
            document.getElementsByTagName('head')[0].appendChild(link)
            toolCssLoaded[scope.tool.style] = true

          ctrl = $window.location.origin + scope.tool.controllerURI
          tmpl = $window.location.origin + scope.tool.templateURI

          require [ctrl, "text!#{ tmpl }"], (controller, template) ->

            $injector.invoke controller, this, {'$scope': scope}

            element.html(template)
            $compile(element.contents())(scope)

            scope.$apply()

  Directives.directive 'nextStep', ($window, $compile, $injector) ->
    restrict: 'E'

          else
            init()

  Directives.directive 'currentStep', Array '$compile', ($compile) ->
    restrict: 'C'
    scope:
      tool:     '='
      step:     '='
      fullSize: '='
      onToggle: '&'
      hasItems: '&'
      has:      '&'
      wants:    '&'
      nextStep: '&'
    template: """
      <div class="panel-heading">
        <i class="fa pull-right"
           ng-click="onToggle()"
           ng-class="{'fa-compress': fullSize, 'fa-expand': !fullSize}"></i>
        {{tool.heading}}
      </div>
    """
    link: (scope, element, attrs) ->
      element.addClass('panel panel-default')
      scope.$watch 'tool.type', (toolType) ->
        console.log toolType

        toAppend = if toolType is 'IFrame'
          "<iframe-tool/>"
        else if toolType is 'native'
          "<angular-tool/>"

        if toAppend?
          $compile(toAppend) scope, (cloned) -> element.append cloned

  Directives.directive 'filterTerm', ->
    restrict: 'AE'
    replace: true,
