+# Documentation
+#
+# For the Japanese / Chinese input method issue
+# http://stackoverflow.com/questions/1391278/contenteditable-change-events
+#
+# For the cursor issue
+# ;(
+
 # simple markdown parser rules
 # /[.+](.+)/g -> inline link
 # /[.+][.+]/g -> reference link

      div.css("display", "inline-block")
    area.val(div.data("content"))

  div.on "keyup paste", ->
    saveCursorLocation(div)
    div.data("content", formattedTextToDataText(div.html())).trigger("send-content").trigger("receive-content")


          location += cursorLocationFromNodeAndOffset(childNode, anchorNode, anchorOffset)
  return location

lengthOfNode = (node) ->
  if node.nodeType == Node.TEXT_NODE
    return node.nodeValue.length
  else if node.tagName == "BR"
    return 1
  else if (node.tagName == "SPAN") || (node.tagName == "DIV")
    length = 0
    for childNode in node.childNodes
      length += lengthOfNode(childNode)
    return length

nodeAndOffsetFromCursorLocation = (location, node) ->
  return [] if lengthOfNode(node) < location
  if node.nodeType == Node.TEXT_NODE
    return [node, location]
  else if node.tagName == "BR"
    return [node, location]
  else
    for childNode in node.childNodes
      if lengthOfNode(childNode) < location
        location -= lengthOfNode(childNode)
      else
        return nodeAndOffsetFromCursorLocation(location, childNode)

saveCursorLocation = (jObject) ->
  plainObject = jObject[0]
