 
 # @todo obviously encapsulate
-insertNode = (node, dom, trailer) ->
-  if trailer
-    dom.insertBefore(node, trailer)
-  else
-    dom.appendChild(node)
-
+createState = (dom, trailer) ->
+  # the state is a closure that normally returns the current context DOM, or inserts a child node if one is given
+  (node) ->
+    if node
+      if trailer then dom.insertBefore(node, trailer) else dom.appendChild(node)
+    else
+      dom
 
 window.tubularHtml =
+  # @todo get the container from somewhere
+  $tubularHtmlCursor: createState document.getElementById('container')
+
   element: (options...) ->
-    # @todo get the container from somewhere (have an "origin" function that seeds view-model state)
-    # @todo store HTML-specific view-model state as an object in a single '$tubularHtml' property or something
-    dom = if @currentDom then @currentDom else document.getElementById('container')
-    trailer = @currentTrailer
-
     subTemplate = null
 
     elementName = null

      for n, v of o
        childDom.setAttribute n, v

    insertNode childDom, dom, trailer

    if subTemplate
      @fork { currentDom: childDom, currentTrailer: null }, subTemplate

  attr: (setting) ->
    for n, path of setting

      if currentCondition isnt condition
        if condition
          # forking the original view-model, since this one is based around the condition model value
          self.fork { $tubularHtmlCursor: createState(currentDom, endNode) }, subTemplate
        else
          while startNode.nextSibling isnt endNode
            startNode.parentNode.removeChild startNode.nextSibling # @todo optimize using local vars
