 Scribe = require('../scribe')
 
 
+getValue = (input) ->
+  if input.tagName == 'SELECT'
+    return input.options[input.selectedIndex].value
+  else if Scribe.DOM.hasClass(input, 'link')
+    if !Scribe.DOM.hasClass(input, 'active')
+      range = @editor.selection.getRange()
+      return range.getText()
+    else
+      return false
+  else
+    return Scribe.DOM.hasClass(input, 'active')
+
 initActiveFormatListener = ->
   @editor.on(Scribe.Editor.events.SELECTION_CHANGE, (selection) =>
-    formats = selection.getFormats()
     _.each(@container.querySelectorAll('.active'), (button) =>
       Scribe.DOM.removeClass(button, 'active')
     )
-    _.each(formats, (value, key) =>
+    _.each(@container.querySelectorAll('select'), (select) =>
+      select.querySelector('option[selected]').selected = true
+    )
+    return unless selection?
+    _.each(selection.getFormats(), (value, key) =>
       if value?
         elem = @container.querySelector(".#{key}")
         return unless elem?

    )
  )

initLinkFormat = ->
  linkButton = @container.querySelector('.link')
  return unless linkButton?
  linkButton.addEventListener('click', =>
    value = false
    if !Scribe.DOM.hasClass(linkButton, 'active')
      range = @editor.selection.getRange()
      value = range.getText()
    @editor.selection.format('link', value, false)
    this.emit(Scribe.Toolbar.events.FORMAT, 'link', value)
  )

initFormats = ->
  _.each(Scribe.Toolbar.formats, (formats, formatGroup) =>
    _.each(formats, (format) =>

  constructor: (@container, @editor) ->
    @container = document.getElementById(@container) if _.isString(@container)
    initFormats.call(this)
    initActiveFormatListener.call(this)
    this.on(Scribe.Toolbar.events.FORMAT, =>
      @editor.root.focus()
