 NODE_RADIUS = 2
 MAX_NODE_DISTANCE = 100
 CLICK_TRESHOLD = 4
+
+MODE_NORMAL = 0     # draw every node and the complete tree
+MODE_NOCHAIN = 1    # draw only decision points
 
 class AbsractTreeViewer
   constructor : (width, height) ->

      result += child.width
    return result

  getMaxTreeDepth : (tree, count) ->
    unless tree
      return 0
    unless count
      count = 1

    while tree.children.length == 1
      tree = tree.children[0]
      count++

    if tree.children.length == 0
      return count
    return Math.max(@getMaxTreeDepth(tree.children[0], count),
              @getMaxTreeDepth(tree.children[1], count)) + 1

  onClick : (evt) =>
    id = @getIdFromPos(evt.offsetX, evt.offsetY)


    # if the decision point has (2) children, draw them and remember their prosition
    if decisionPoint.children.length > 0
      # Calculate the top of the children
      if mode == MODE_NORMAL or chainCount < 3
        topC = top + (chainCount + 1) * @nodeDistance
      else if mode == MODE_NOCHAIN
        topC = top + 3 * @nodeDistance

      c1 = @drawTreeWithWidths(decisionPoint.children[0], left,  m, topC, mode)
      c2 = @drawTreeWithWidths(decisionPoint.children[1], m, right, topC, mode)
      # set the root's x coordinate to be in between the decisionPoint's childs
      xr = (c1[0] + c2[0]) / 2
      # draw edges from last node in 'chain' (or root, if chain empty)
      # and the decisionPoint's children
      @drawEdge(xr, topC - @nodeDistance, c1[0], c1[1])
      @drawEdge(xr, topC - @nodeDistance, c2[0], c2[1])
    else
      # if decisionPoint is leaf, there's not much to do
      xr = m
    
    if mode == MODE_NORMAL or chainCount < 3
      # Draw the chain and the root, connect them.
      node = tree
      for i in [0..chainCount]
        @drawNode(xr, top + i * @nodeDistance, node.id)
        node = node.children[0]
        if i != 0
          @drawEdge(xr, top + (i - 1) * @nodeDistance, xr, top + i * @nodeDistance)
    else if mode == MODE_NOCHAIN
      # Draw root, chain indicator and decision point
      @drawNode(xr, top, tree.id)
      @drawEdge(xr, top, xr, top + 0.5 * @nodeDistance)
      @drawChainIndicator(xr, top + 0.5 * @nodeDistance, top + 1.5 * @nodeDistance)
      @drawEdge(xr, top + 1.5 * @nodeDistance, xr, top + 2 * @nodeDistance)
      @drawNode(xr, top + 2 * @nodeDistance, decisionPoint.id)

    return [xr, top]

