         animation: 0
       },
       plugins: ['json_data', 'ui', 'themes']
-      }).on('select_node.jstree', (event, data) ->
-        if ignoreNavigation
-          return
-        console.log "select_node", event, data
-        node = data.rslt.obj;
-        id = node.attr('name');
-        if (id)
-          console.log "clicked", id
-
-          node = editor.dom.select('#'+id);
-          #node.append('<span class="empty_tag_remove_me"></span>');
-          
-          nodeEl = node[0]
-          editor.getWin().scrollTo(0, editor.dom.getPos(nodeEl).y);
-          editor.selection.select(nodeEl)
-          editor.nodeChanged()
-
-          editor.focus()
-          
-      )
-
-
-
-
+      }).on('select_node.jstree', _selectNodeHandlerGenerator(editor))
     
   navigateTree = (editor, controlmanager, node) ->
     ignoreNavigation = true

    ignoreNavigation = false
    return null



  {
    create: createTree
    update: updateTree

      plugins: ['json_data', 'ui', 'themeroller', 'contextmenu']
      })

  _selectNodeHandlerGenerator = (editor) ->
    selectNodeHandler = (event, data) ->
      if ignoreNavigation
        return
      console.log "select_node", event, data
      node = data.rslt.obj;
      id = node.attr('name');
      if (id)
        console.log "clicked", id

        node = editor.dom.select('#'+id);
        #node.append('<span class="empty_tag_remove_me"></span>');

        nodeEl = node[0]
        editor.getWin().scrollTo(0, editor.dom.getPos(nodeEl).y);
        editor.selection.select(nodeEl)
        editor.nodeChanged()

        editor.focus()

  _depthWalkCallbackGenerator = (holder) ->
    depthWalkCallback = (depth) ->
      node = this
      if not node.getAttribute?
        return false
      id = node.getAttribute('id')
      if not id
        node.setAttribute('id', tinymce.DOM.uniqueId('t_'))
        id = node.getAttribute('id')
      while depth < (holder.length - 1)
        holder.shift()
      title = node.getAttribute('data-xmlel') || ("[" + node.localName + "]")
      holder[0]['children'] ||= []
      holder[0]['state'] ||= 'closed'
      holder[0]['children'].push {
        'data': title,
        'attr': {
          name: id,
          class: node.getAttribute('class')
        }
      }
      if (depth) <= holder.length - 1
        holder.unshift holder[0]['children'][holder[0]['children'].length - 1]
      true


  updateTree = (selector, editor) ->
    body = editor.dom.getRoot()
    $(selector).jstree('delete_node', '#root');
