 # Service: enmlRenderer
 # ----------------------------------------------------------------------------------------------------------------------
 markever.factory 'enmlRenderer', ['imageManager', (imageManager) ->
-  getEnmlFromElement = (jq_html_div, html_render_func, markdown) ->
-    # init markdown render
-    # html post process
-    html_render_func(jq_html_div)
-
-    # further post process
-    # remove all script tags
-    jq_html_div.find('script').remove()
-
-    # add inline style
-    # refer: https://github.com/Karl33to/jquery.inlineStyler
-    # TODO still too much redundant styles
-    inline_styler_option = {
-      'propertyGroups' : {
-        'font-matters' : ['font-size', 'font-family', 'font-style', 'font-weight'],
-        'text-matters' : ['text-indent', 'text-align', 'text-transform', 'letter-spacing', 'word-spacing',
-                  'word-wrap', 'white-space', 'line-height', 'direction'],
-        'display-matters' : ['display'],
-        'size-matters' : ['width', 'height'],
-        'color-matters' : ['color', 'background-color'],
-        'position-matters' : ['margin', 'margin-left', 'margin-right', 'margin-top', 'margin-bottom',
-                    'padding', 'padding-left', 'padding-right', 'padding-top', 'padding-bottom',
-                    'float'],
-        'border-matters' : ['border', 'border-left', 'border-right', 'border-radius',
-                  'border-top', 'border-right', 'border-color'],
-      },
-      'elementGroups' : {
-        # N.B. UPPERCASE tags
-        'font-matters' : ['DIV', 'BLOCKQUOTE', 'SPAN', 'STRONG', 'EM', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6'],
-        'text-matters' : ['SPAN', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6'],
-        'display-matters' : ['HR', 'PRE', 'SPAN', 'UL', 'OL', 'LI', 'PRE', 'CODE'],
-        'size-matters' : ['SPAN'],
-        'color-matters' : ['DIV', 'SPAN', 'PRE', 'CODE', 'BLOCKQUOTE', 'HR'],
-        'position-matters' : ['DIV', 'PRE', 'BLOCKQUOTE', 'SPAN', 'HR', 'UL', 'OL', 'LI', 'P',
-                    'H1', 'H2', 'H3', 'H4', 'H5', 'H6'],
-        'border-matters' : ['HR', 'BLOCKQUOTE', 'SPAN', 'PRE', 'CODE'],
+  get_enml_and_title_promise = (jq_html_div, html_render_func, markdown) ->
+    # html_render_func has to be a promise
+    return html_render_func(jq_html_div).then () =>
+      # further post process
+      # remove all script tags
+      jq_html_div.find('script').remove()
+
+      # add inline style
+      # refer: https://github.com/Karl33to/jquery.inlineStyler
+      # TODO still too much redundant styles
+      inline_styler_option = {
+        'propertyGroups' : {
+          'font-matters' : ['font-size', 'font-family', 'font-style', 'font-weight'],
+          'text-matters' : ['text-indent', 'text-align', 'text-transform', 'letter-spacing', 'word-spacing',
+                    'word-wrap', 'white-space', 'line-height', 'direction'],
+          'display-matters' : ['display'],
+          'size-matters' : ['width', 'height'],
+          'color-matters' : ['color', 'background-color'],
+          'position-matters' : ['margin', 'margin-left', 'margin-right', 'margin-top', 'margin-bottom',
+                      'padding', 'padding-left', 'padding-right', 'padding-top', 'padding-bottom',
+                      'float'],
+          'border-matters' : ['border', 'border-left', 'border-right', 'border-radius',
+                    'border-top', 'border-right', 'border-color'],
+        },
+        'elementGroups' : {
+          # N.B. UPPERCASE tags
+          'font-matters' : ['DIV', 'BLOCKQUOTE', 'SPAN', 'STRONG', 'EM', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6'],
+          'text-matters' : ['SPAN', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6'],
+          'display-matters' : ['HR', 'PRE', 'SPAN', 'UL', 'OL', 'LI', 'PRE', 'CODE'],
+          'size-matters' : ['SPAN'],
+          'color-matters' : ['DIV', 'SPAN', 'PRE', 'CODE', 'BLOCKQUOTE', 'HR'],
+          'position-matters' : ['DIV', 'PRE', 'BLOCKQUOTE', 'SPAN', 'HR', 'UL', 'OL', 'LI', 'P',
+                      'H1', 'H2', 'H3', 'H4', 'H5', 'H6'],
+          'border-matters' : ['HR', 'BLOCKQUOTE', 'SPAN', 'PRE', 'CODE'],
+        }
       }
-    }
-    jq_html_div.inlineStyler(inline_styler_option)
-
-    # set href to start with 'http' is no protocol assigned
-    jq_html_div.find('a').attr 'href', (i, href) ->
-      if not href.toLowerCase().match('(^http)|(^https)|(^file)')
-        return 'http://' + href
-      else
-        return href
-
-    # clean the html tags/attributes
-    html_clean_option = {
-      format: true,
-      allowedTags: ['a', 'abbr', 'acronym', 'address', 'area', 'b', 'bdo', 'big', 'blockquote',
-              'br', 'caption', 'center', 'cite', 'code', 'col', 'colgroup', 'dd', 'del',
-              'dfn', 'div', 'dl', 'dt', 'em', 'font', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
-              'hr', 'i', 'img', 'ins', 'kbd', 'li', 'map', 'ol', 'p', 'pre', 'q', 's',
-              'samp', 'small', 'span', 'strike', 'strong', 'sub', 'sup', 'table', 'tbody',
-              'td', 'tfoot', 'th', 'thead', 'title', 'tr', 'tt', 'u', 'ul', 'var', 'xmp',],
-      allowedAttributes: [['href', ['a']], ['longdesc'], ['style']],
-      removeAttrs: ['id', 'class', 'onclick', 'ondblclick', 'accesskey', 'data', 'dynsrc', 'tabindex',],
-    }
-    cleaned_html = $.htmlClean(jq_html_div.html(), html_clean_option);
-    # FIXME hack for strange class attr not removed
-    cleaned_html = cleaned_html.replace(/class='[^']*'/g, '')
-    cleaned_html = cleaned_html.replace(/class="[^"]*"/g, '')
-
-    # embbed raw markdown content into the html
-    cleaned_html = cleaned_html +
-      '<center style="display:none">' +
-      $('<div />').text(markdown).html() +
-      '</center>'
-
-    # add XML header & wrap with <en-note></en-note>
-    final_note_xml = '<?xml version="1.0" encoding="utf-8"?>' +
-             '<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">' +
-             '<en-note>' +
-             cleaned_html +
-             '</en-note>'
-    # FIXME debugging info
-    console.log(final_note_xml)
-    return final_note_xml
+      jq_html_div.inlineStyler(inline_styler_option)
+
+      # set href to start with 'http' is no protocol assigned
+      jq_html_div.find('a').attr 'href', (i, href) ->
+        if not href.toLowerCase().match('(^http)|(^https)|(^file)')
+          return 'http://' + href
+        else
+          return href
+
+      # clean the html tags/attributes
+      html_clean_option = {
+        format: true,
+        allowedTags: ['a', 'abbr', 'acronym', 'address', 'area', 'b', 'bdo', 'big', 'blockquote',
+                'br', 'caption', 'center', 'cite', 'code', 'col', 'colgroup', 'dd', 'del',
+                'dfn', 'div', 'dl', 'dt', 'em', 'font', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
+                'hr', 'i', 'img', 'ins', 'kbd', 'li', 'map', 'ol', 'p', 'pre', 'q', 's',
+                'samp', 'small', 'span', 'strike', 'strong', 'sub', 'sup', 'table', 'tbody',
+                'td', 'tfoot', 'th', 'thead', 'title', 'tr', 'tt', 'u', 'ul', 'var', 'xmp',],
+        allowedAttributes: [['href', ['a']], ['longdesc'], ['style']],
+        removeAttrs: ['id', 'class', 'onclick', 'ondblclick', 'accesskey', 'data', 'dynsrc', 'tabindex',],
+      }
+      cleaned_html = $.htmlClean(jq_html_div.html(), html_clean_option);
+      # FIXME hack for strange class attr not removed
+      cleaned_html = cleaned_html.replace(/class='[^']*'/g, '')
+      cleaned_html = cleaned_html.replace(/class="[^"]*"/g, '')
+
+      # embbed raw markdown content into the html
+      cleaned_html = cleaned_html +
+        '<center style="display:none">' +
+        $('<div />').text(markdown).html() +
+        '</center>'
+
+      # add XML header & wrap with <en-note></en-note>
+      final_note_xml = '<?xml version="1.0" encoding="utf-8"?>' +
+               '<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">' +
+               '<en-note>' +
+               cleaned_html +
+               '</en-note>'
+
+      # make title
+      title = 'New Note - Markever'
+      if jq_html_div.find('h1').size() > 0
+        text = jq_html_div.find('h1').text()
+        title = text if text.trim().length > 0
+      else if jq_html_div.find('h2').size() > 0
+        text = jq_html_div.find('h2').text()
+        title = text if text.trim().length > 0
+      else if jq_html_div.find('h3').size() > 0
+        text = jq_html_div.find('h3').text()
+        title = text if text.trim().length > 0
+      else if jq_html_div.find('p').size() > 0
+        text = jq_html_div.find('p').text()
+        title = text if text.trim().length > 0
+
+      # the return value of result promise
+      return {enml: final_note_xml, title: title}
+
+
+  get_title_promise = (jq_html_div, html_render_func) ->
+    return html_render_func(jq_html_div).then () =>
+      title = 'New Note - Markever'
+      if jq_html_div.find('h1').size() > 0
+        text = jq_html_div.find('h1').text()
+        title = text if text.trim().length > 0
+      else if jq_html_div.find('h2').size() > 0
+        text = jq_html_div.find('h2').text()
+        title = text if text.trim().length > 0
+      else if jq_html_div.find('h3').size() > 0
+        text = jq_html_div.find('h3').text()
+        title = text if text.trim().length > 0
+      else if jq_html_div.find('p').size() > 0
+        text = jq_html_div.find('p').text()
+        title = text if text.trim().length > 0
+      return title
+
 
   return {
-    getEnmlFromElement : getEnmlFromElement
+    get_enml_and_title_promise : get_enml_and_title_promise
+    get_title_promise: get_title_promise
   }
 ]
 

    if vm.saving_note == false
      # set status
      vm.saving_note = true
      # get enml and title
      _enml_and_title = vm.get_enml_and_title_from_md(vm.get_md())
      enml = _enml_and_title['enml']
      title = _enml_and_title['title']


  vm.get_enml_and_title_from_md = (md) ->
    html_div_hidden = $('#md_html_div_hidden')
    # return a promise
    return enmlRenderer.get_enml_and_title_promise(
      html_div_hidden,
      vm.render_html,
      md
    )

  # ------------------------------------------------------------------------------------------------------------------
  # Debugging methods
