 
     unless this.toolbar
       ta = new Sugar.RichTextArea(this)
+      decorator = markdownDecorator
 
       # Setup the buttons
       ta.toolbar
 
         # Bold
-        .addButton "Bold", "bold", ->
-          this.textArea.wrapSelection('**', '**')
+        .addButton "Bold", "bold", (selection) ->
+          decorator.bold(selection)
 
         # Italic
-        .addButton "Italics", "italic", ->
-          this.textArea.wrapSelection('_', '_')
+        .addButton "Italics", "italic", (selection) ->
+          decorator.emphasis(selection)
 
         # Link
-        .addButton "Link", "link", ->
-          selection = this.textArea.selectedText()
-          response = prompt('Enter link URL', '')
-          this.textArea.replaceSelection(
-            "[" + (selection || "Link text") + "](" + (response || 'http://link_url/').replace(/^(?!(f|ht)tps?:\/\/)/, 'http://') + ")"
-          )
+        .addButton "Link", "link", (selection) ->
+          url = prompt("Enter link URL", "")
+          name = if selection.length > 0 then selection else "Link text"
+          url = if url.length > 0 then url else "http://example.com/"
+          url = url.replace(/^(?!(f|ht)tps?:\/\/)/, 'http://')
+          decorator.link(url, name)
 
         # Image tag
-        .addButton "Image", "picture", ->
-          selection = this.textArea.selectedText()
-          if selection == ''
-            response = prompt('Enter image URL', '')
-            unless response
-              return
-            this.textArea.replaceSelection("![](" + response + ")")
-          else
-            this.textArea.replaceSelection("![](" + selection + ")")
+        .addButton "Image", "picture", (selection) ->
+          url = if selection.length > 0 then selection else prompt("Enter image URL", "")
+          decorator.image(url)
 
         # MP3 Player
-        .addButton "MP3", "music", ->
-          selection = this.textArea.selectedText()
-          response = prompt('Enter MP3 URL', '')
-          unless selection
-            selection = prompt('Enter track title', '')
-          this.textArea.replaceSelection(
-            '<a href="' + (response || 'http://link_url/').replace(/^(?!(f|ht)tps?:\/\/)/, 'http://') + '" class="mp3player">' +
-            (selection || "Link text") + '</a>')
+        .addButton "MP3", "music", (selection) ->
+          url = prompt("Enter MP3 URL", "")
+          name = if selection.length > 0 then selection else prompt("Enter track title", "")
+          url = if url.length > 0 then url else "http://example.com/example.mp3"
+          url = url.replace(/^(?!(f|ht)tps?:\/\/)/, 'http://')
+          decorator.mp3(url, name)
 
         # Block Quote
-        .addButton "Block Quote", "quote-left", ->
-          selection = this.textArea.selectedText()
-          quotedText = ("> " + line for line in selection.split("\n")).join("\n")
-          this.textArea.replaceSelection(quotedText)
+        .addButton "Block Quote", "quote-left", (selection) ->
+          decorator.blockquote(selection)
 
         # Escape HTML
-        .addButton "Code", "code", ->
-          selection = this.textArea.selectedText()
-          response = prompt('Enter language (leave blank for no syntax highlighting)', '')
-          if response
-            this.textArea.replaceSelection("```" + response + "\n" + selection + "\n```")
-          else
-            this.textArea.replaceSelection("```\n" + selection + "\n```")
+        .addButton "Code", "code", (selection) ->
+          lang = prompt("Enter language (leave blank for no syntax highlighting)", "")
+          decorator.code(selection, lang)
 
         # Spoiler
-        .addButton "Spoiler", "warning-sign", ->
-          this.textArea.wrapSelection('<div class="spoiler">', '</div>')
+        .addButton "Spoiler", "warning-sign", (selection) ->
+          decorator.spoiler(selection)

  @toolbar.create()
  this


$(Sugar).bind 'ready modified', ->


      i = document.createElement("i")
      a.title = name
      a.textArea = @textArea
      jQuery(a).click ->
        selection = textArea.selectedText()
        [prefix, replacement, postfix] = callback(selection)
        textArea.replaceSelection(prefix + replacement + postfix)
      jQuery(a).addClass className
      jQuery(i).addClass "icon-#{className}"
      jQuery(a).append(i)
