 _ = require 'underscore'
+
+# Function that translates an index from start (absolute position) into a
+# relative position in word index and offset
+#
+# @param [Integer] position the position
+# @param [Rte] rte an rte instance
+relativeFromAbsolute = (position, rte)->
+    index = 0
+
+    while position > 0
+      if index >= rte._rte.words.length
+        return {word: index, pos: 0}
+      if rte._rte.words[index].word.length > position
+        return {word: index, pos: position}
+      else
+        position -= rte._rte.words[index].word.length
+        index += 1
+    return {word: index, pos: position}
+
+# Function that translates an index from start (absolute position) into a
+# relative position in word index and offset
+#
+# @param [Option] relative the position
+# @param [Rte] rte an rte instance
+absoluteFromRelative = (index, offset, rte) ->
+  absolute = offset
+  if index > 0
+    for i in [0..(index-1)]
+      absolute += rte.getWord(i).word.length
+
+  absolute
 
 # XRegExp = require('xregexp').XRegExp
 WordRegExp = /\S+\s*/g

        @deleteSel selection

      else if delta.insert?
        end = position + delta.insert.length
        selection = new Selection position, end, @
        @insert selection, delta.insert
        position += delta.insert.length

      else

  window.Word = Word

if module?
  module.exports = [Rte, Selection, Word,
    relativeFromAbsolute, absoluteFromRelative]
