   render: (content) ->
     if content
       @el.html(content)
-      @bind()
+      @executeProblemScripts () =>
+        @setupInputTypes()
+        @bind()
     else
       $.postWithPrefix "#{@url}/problem_get", (response) =>
         @el.html(response.html)
-        @executeProblemScripts()
-        @bind()
-
-  executeProblemScripts: ->
-    @el.find(".script_placeholder").each (index, placeholder) ->
-      s = $("<script>")
-      s.attr("type", "text/javascript")
-      s.attr("src", $(placeholder).attr("data-src"))
+        @executeProblemScripts () =>
+          @setupInputTypes()
+          @bind()
+
+  # TODO add hooks for problem types here by inspecting response.html and doing
+  # stuff if a div w a class is found
+
+  setupInputTypes: =>
+    @el.find(".capa_inputtype").each (index, inputtype) =>
+      classes = $(inputtype).attr('class').split(' ')
+      for cls in classes
+        setupMethod = @inputtypeSetupMethods[cls]
+        setupMethod(inputtype) if setupMethod?
+
+  executeProblemScripts: (callback=null) ->
+
+    placeholders = @el.find(".script_placeholder")
+
+    if placeholders.length == 0
+      callback()
+      return
+
+    completed      = (false for i in [1..placeholders.length])
+    callbackCalled = false
+
+    # This is required for IE8 support.
+    completionHandlerGeneratorIE = (index) =>
+      return () ->
+        if (this.readyState == 'complete' || this.readyState == 'loaded')
+          #completionHandlerGenerator.call(self, index)()
+          completionHandlerGenerator(index)()
+
+    completionHandlerGenerator = (index) =>
+      return () =>
+        allComplete = true
+        completed[index] = true
+        for flag in completed
+          if not flag
+            allComplete = false
+            break
+        if allComplete and not callbackCalled
+          callbackCalled = true
+          callback() if callback?
+
+    placeholders.each (index, placeholder) ->
+      s = document.createElement('script')
+      s.setAttribute('src', $(placeholder).attr("data-src"))
+      s.setAttribute('type', "text/javascript")
+
+      s.onload             = completionHandlerGenerator(index)
+
+      # s.onload does not fire in IE8; this does.
+      s.onreadystatechange = completionHandlerGeneratorIE(index)
 
       # Need to use the DOM elements directly or the scripts won't execute
       # properly.
-      $('head')[0].appendChild(s[0])
+      $('head')[0].appendChild(s)
       $(placeholder).remove()
 
   ###

    @$(".CodeMirror").each (index, element) ->
      element.CodeMirror.save() if element.CodeMirror.save
    @answers = @$("[id^=input_#{@element_id.replace(/problem_/, '')}_]").serialize()

class @Problem

  constructor: (element) ->
    @el = $(element).find('.problems-wrapper')
    @id = @el.data('problem-id')
