# This is based on the scriptbasic style

$DO_CONTENTS = 1;

$INLINE_CONTENTS = 1;

@SECTION_BUTTONS =
    (
     'Back', 'Forward', ' ', 'Contents', 'Index', 'About'
    );

@CHAPTER_BUTTONS =
    (
     'Back', 'Forward', ' ', 'Contents', 'Index', 'About'
    );

@MISC_BUTTONS = ('Contents', 'Index', 'About');

@SECTION_FOOTER_BUTTONS = ('Contents', 'Index', 'About');

@NODE_FOOTER_BUTTONS = ('Back', 'Forward');

$WORDS_IN_PAGE = undef;
$SHOW_MENU = 0;

$BIG_RULE = '<hr size="2">';

push @command_handler_init, \&book_initialize_variables;

sub book_print_sub_toc($$);

sub book_print_sub_toc($$)
{
    my $parent_element = shift;
    my $element = shift;
    my $result = "<li> ".&$anchor('', main::href($element, $parent_element->{'file'}), $element->{'text'}) . " </li>\n";
#print STDERR "SUB_TOC $element->{'text'}\n"; #sleep 1;
    if (exists($element->{'child'}))
    {
#print STDERR "SUB_TOC child $element->{'child'}->{'text'}\n"; 
         $result .= "<li><ul${NO_BULLET_LIST_ATTRIBUTE}>\n". book_print_sub_toc($parent_element, $element->{'child'}) ."</ul></li>\n";
    }
    if (exists($element->{'sectionnext'}))
    {
#print STDERR "SUB_TOC next($element->{'text'}) $element->{'next'}->{'text'}\n"; 
         $result .= book_print_sub_toc($parent_element, $element->{'sectionnext'});
    }
    return $result;
}

sub book_print_up_toc($)
{
    my $element = shift;

    my $result = '';
    my $current_element = $element;
    my @elements;
    while (defined($current_element->{'sectionup'}) and ($current_element->{'sectionup'} ne $current_element))
    {
        unshift (@elements, $current_element->{'sectionup'});
        $current_element = $current_element->{'sectionup'};
    }
    #print $fh "<ul>" . &$anchor('', $Texi2HTML::HREF{Contents}, '[' . $Texi2HTML::NAME{Contents} . ']') . " <br>\n";
    my $elem = shift @elements;
    $result .= "<ul${NO_BULLET_LIST_ATTRIBUTE}><li>" . &$anchor('', main::href($elem, $element->{'file'}), $elem->{'text'}) . " </li>\n";
    foreach my $elem (@elements)
    {
        $result .= "<li><ul${NO_BULLET_LIST_ATTRIBUTE}><li>" . &$anchor('', main::href($elem, $element->{'file'}), $elem->{'text'}) . " </li>\n";
    }
    foreach my $elem (@elements)
    {
        $result .= "</ul></li>\n";
    }
    $result .= "</ul>\n";
}

#$print_section = \&book_print_section;
$print_element_header = \&book_print_element_header;
$print_Top = \&book_print_Top;
$print_Toc = \&book_print_Toc;
$element_file_name = \&book_element_file_name;

sub book_print_element_header($$)
{
    my $first_in_page = shift;
    my $previous_is_top = shift;
    my $buttons = \@SECTION_BUTTONS;
    my $result;

    if ($first_in_page and $SECTION_NAVIGATION)
    {
        #print_up_toc($fh, $Texi2HTML::THIS_ELEMENT) if ($SPLIT eq 'node');
        $result = book_print_up_toc($Texi2HTML::THIS_ELEMENT);
        $result .= &$print_head_navigation(undef, $buttons);
    }
    else
    { # got to do this here, as it isn't done in print_head_navigation
        $result = &$print_navigation($buttons) if ($SECTION_NAVIGATION);
    }
    return $result;
}

sub book_print_Top($$)
{
    my $fh = shift;
    my $has_top_heading = shift;

    my $buttons = \@MISC_BUTTONS;
    # head navigation is printed with heading, and is in THIS_SECTION
#    &$print_head_navigation($fh, $buttons) if ($SPLIT or $SECTION_NAVIGATION);
    my ($titlepage_text, $titlepage_no_texi, $titlepage_simple_text) = main::do_special_region_lines('titlepage',$Texi2HTML::THISDOC{'state'});
    &$titlepage([],$titlepage_text, $titlepage_no_texi, $titlepage_simple_text); 
    print $fh $Texi2HTML::TITLEPAGE;
    print $fh "<h1 class=\"settitle\">$Texi2HTML::NAME{Top}</h1>\n"
        unless ($has_top_heading);
    main::print_lines($fh, $Texi2HTML::THIS_SECTION);
    if (!$INLINE_CONTENTS and !$Texi2HTML::THISDOC{'setcontentsaftertitlepage'})
    {
        print $fh '<h2> ' . $Texi2HTML::NAME{'Contents'} . "</h2>\n" ;
        main::print_lines($fh, $Texi2HTML::TOC_LINES);
    }
}

sub book_print_Toc
{
    my $fh = shift;
    &$print_page_head($fh) if $SPLIT;
    #print $fh $Texi2HTML::TITLEPAGE;
    print $fh '<h2> ' . $Texi2HTML::NAME{'Contents'} . "</h2>\n" ;
    main::print_lines($fh);
    &$print_misc_footer($fh, \@MISC_BUTTONS);
}

my $unumbered_nr;
my $previous_nr;
my $previous_file_name; 

sub book_initialize_variables()
{
   $unumbered_nr = -1;
   $previous_nr = undef;
   $previous_file_name = undef;   
}

sub book_element_file_name($$$)
{
    my $element = shift;
    my $type = shift;
    my $prefix = shift;

    return undef if ($NODE_FILES or !$SPLIT);
    return undef if (!defined($element));
   
    if (defined($previous_nr) and ($element->{'doc_nr'} == $previous_nr))
    {
        return $previous_file_name;
    }
    if ($type eq 'top')
    {
        $previous_file_name = "${prefix}_top.html";
    }
    elsif (defined($element->{'number'}) and ($element->{'number'} ne ''))
    {
         my $number = $element->{'number'};
         $number .= '.' unless ($number =~ /\.$/);
         $previous_file_name = "${prefix}_$number" . 'html';
    }
    else
    {
         $unumbered_nr++;
         $previous_file_name = "${prefix}_U." . $unumbered_nr . '.html'; 
    }
    $previous_nr = $element->{'doc_nr'};
    return $previous_file_name;
}

$heading = \&book_heading;

sub book_heading($$$$$$;$$)
{
    my $element = shift;
    my $command = shift;
    my $texi_line = shift;
    my $line = shift;
    my $in_preformatted = shift;
    my $one_section = shift;
    my $element_heading = shift;

    my $level = 3;

    return '' if ($element->{'top'});
    if (!$element->{'node'})
    {
        $level = $element->{'level'};
    }
    $level = 1 if ($level == 0);
    my $text = $element->{'text'};
    if (!$element->{'node'} and (!$NUMBER_SECTIONS))
    {
        $text = $element->{'text_nonumber'};
    }
    return '' if ($text !~ /\S/);
    my $class = $element->{'tag_level'};
    $class = 'unnumbered' if ($class eq 'top');
    if (defined($element->{'tocid'}) and $TOC_LINKS)
    {
         $text = &$anchor ('', "$Texi2HTML::THISDOC{'toc_file'}#$element->{'tocid'}", $text);
    }
    $text = "<h$level class=\"$class\"> $text </h$level>\n";
    #if (defined($SPLIT)  and ($SPLIT eq 'node') 
    if (exists($element->{'child'}) 
        and ($element->{'tag_level'} !~ /heading/))
    {
        $text .= "<ul${NO_BULLET_LIST_ATTRIBUTE}>\n";
        $text .= "" . book_print_sub_toc($element, $element->{'child'});
        $text .= "</ul>\n";
    }
    return $text;
}

1;
