#line 4014 "/home/travis/build/felix-lang/felix/src/packages/flx_web.fdoc"
  include "./paragraph-interface";
  include "./heading-interface";
  
  include "./button-interface";
  var button-factory : unit -> button-factory_t;
  
  
  val js1 =
  """
  <script type="text/javascript">
  
  function expand(but,id)
  {
    var n = document.getElementById(id).style;
    var button = document.getElementById(but);
    button.src = "/share/src/web/images/minus.gif";
    button.alt = "-";
    n.display = "block";
  }
  function collapse(but,id)
  {
    var n = document.getElementById(id).style;
    var button = document.getElementById(but);
    button.src = "/share/src/web/images/plus.gif";
    button.alt = "+";
    n.display = "none";
  }
  function toggle(button,id)
  {
    var n = document.getElementById(id).style;
    if (n.display == "none")
    {
      button.src = "/share/src/web/images/minus.gif";
      button.alt = "-";
      n.display = "block";
    }
    else
    {
      button.src = "/share/src/web/images/plus.gif";
      button.alt = "+";
      n.display = "none";
    }
  }
  var allbuttons = [
  """;
  val js2 =
  """
  ];
  function expand_all(dummy)
  {
    for (i in allbuttons)
    {
      expand(allbuttons[i], allbuttons[i]+"_d");
    }
  }
  function collapse_all(dummy)
  {
    for (i in allbuttons)
    {
      collapse(allbuttons[i], allbuttons[i]+"_d");
    }
  }
  </script>
  """;
  
  fun escape_sp(h: string) => map (fun (c: char) => if c == ' ' then '_'.char else c) h;
  
  fun setup(config_data:string) = {
    button-factory = Dynlink::load-plugin-func0 [button-factory_t] (dll-name="fdoc_button");
    eprintln$ "Setup fdoc_heading " + config_data;
    return 0;
  }
  
  
  object fdoc_heading (paragraph: paragraph-control_t, write_string: string -> 0) implements heading-control_t =
  {
    var button = #button-factory;
  
    method fun whatami () => "Heading object";
    var hstack = 0; // number of open <div>s
    var hnums = varray[int] (size 5,1);
    var all_buttons = "";
    var htree = Empty[int * string];
  
    method fun get_headings () => rev htree;
  
    method fun emit-buttons() =>
     button.make-button(id="expand", text="Expand", onclick="expand_all") +
     button.make-button(id="collapse", text="Collapse", onclick="collapse_all")
    ;
  
    method fun emit-js() => js1 + all_buttons + js2;
  
    // bid is the button id, cid is the stuff which is controlled by it
    method fun tree_button(bid:string, cid:string)=>
      "<img src='/share/src/web/images/minus.gif' id='"+bid+"' onclick='toggle(this,\""+cid+"\")' alt='+'/>"
    ;
  
    method proc add_button (fname: string) {
      all_buttons = if all_buttons != "" then all_buttons + ',\n' else '' endif + '"' + fname + '"';
    }
  
    proc edivs(n:int) {
      while hstack > n do
        write_string("</div>");
        --hstack;
        set (hnums,hstack,1);
       done
       if hstack == n do
         --hstack;
         write_string("</div>");
         set(hnums,hstack,hnums.hstack+1);
       done
    }
  
    fun hnum() = {
      var s = ""; var i:int;
      for i in 0 upto hstack - 2 do
        s+=str(hnums.i) + ".";
      done
      return s + str(hnums.(hstack - 1));
    }
  
    method proc head(docnum: string, n:int, txt:string) {
      #(paragraph.ep);
      edivs(n);
      add_button txt;
      tb:=tree_button(txt,txt+"_d");
      ++hstack;
      htree = Cons ( (n,txt), htree);
      write_string("<h"+str n+" id='"+escape_sp(txt)+"_h'>"+tb+" "+docnum+hnum()+" "+ txt+"</h"+str n+">" +
       "<div id='"+escape_sp(txt)+"_d' style='display:block'>\n");
    }
  
    method proc finalise () {
     edivs(1);
    }
  }
  
  export fun setup of (string) as "fdoc_heading_setup";
  export fun fdoc_heading of (paragraph-control_t * (string->0)) as "fdoc_heading";