                         new_pill = @nav.children().first()
                 @select_nav(new_pill.children(0))
 
+    show: (opts) ->
+        # the opposite of @hide, used to resurrect the dialog in its current state
+        # the @init invokes the initial @dialog.modal("show"), don't get confused!
+        old_lang = @opts.lang
+        @opts = defaults opts,
+            lang  : @opts.lang
+            cb    : @opts.cb
+        @dialog.show()
+        if not @lang? || old_lang != @opts.lang
+            @select_lang(@opts.lang)
+
+    hide: () ->
+        # this is deliberately not destroying the instance
+        @dialog.hide()
+
+    destroy: () ->
+        # this is the destructive operation, which unbinds all the event handling etc.
+        @dialog.modal("hide")
+
     submit: () ->
-        @dialog.modal('hide')
-        window.alert("INSERT CODE:\n" + @doc[0])
+        @hide()
+        if @doc?
+            @opts.cb(code: @doc[0], lang: @lang, descr: @doc[1])
 
     set_active: (list, which) ->
         list.find("li").removeClass("active")
         which.addClass("active")
 
+    select_lang: (lang) ->
+        # crude way to go from a lang-string to the <a> element
+        @select_nav(@nav.find("a[href=##{@opts.lang}]"))
+
     select_nav: (pill) ->
+        # pill is the clicked <a> in the @nav
         @set_active(@nav, pill.parent())
         @lang = pill.attr("href").substring(1)
         @lvl2.empty()


    init_nav: () ->
        # <li role="presentation"><a href="#sage">Sage</a></li>
        nav_entries = [
            ["sage", "Sage"],
            ["python", "Python"],
            ["r", "R"],
            ["gap", "GAP"],
            ["cython", "Cython"]]
        for entry, idx in nav_entries when data[entry[0]]?
            @nav.append($("<li role='presentation'><a href='##{entry[0]}'>#{entry[1]}</a></li>"))
            if @opts.lang == entry[0]
                @set_active(@nav, @nav.children(idx))

    init_lvl1: () ->
        if @opts.lang?
            @fill_list(@lvl1, data[@opts.lang])

    init_buttons: () ->
        @dialog.find(".btn-close").on "click", =>
            @dialog.modal('hide')
            return false

        @dialog.find(".btn-submit").on "click", =>


                new_doc = active[dirop]()
                if new_doc.length == 0
                    # we have to switch one step #{dirop} in the lvl2 category
                    lvl2_active = @lvl2.find(".active")
                    new_lvl2 = lvl2_active[dirop]()
                    if new_lvl2.length == 0
                        lvl1_active = @lvl1.find(".active")
                        # now, we also have to step #{dirop} in the highest lvl1 category
                        new_lvl1 = lvl1_active[dirop]()
                        if new_lvl1.length == 0
                            new_lvl1 = @lvl1.children()[carryop]()
