'#Reference {A3B5C45B-F842-44fc-A5E5-50CED69BA86E}#2.0#0#C:\Program Files\Mindjet\MindManager 6\Mm5WebExport.dll#Mm5webexport 2.0 Type Library
'#Uses "GlobalParameterNames.mmbas"
'#Uses "GlobalUtilities.mmbas"
'#Uses "ParameterNames.mmbas"

Option Explicit

Sub Main

    Dim addin As Mm5WebExportAddIn
    Dim webex As WebExport

    Set addin = WebExportHelper.WebExportAddIn
    Set webex = addin.ActiveWebExport
    Dim pageCount As Long
    Dim currentPageIndex As Long
    Dim s As String
    Dim homeFilename As String
    Dim previousFilename As String
    Dim nextFilename As String
    Dim PageToLinkTo As Long
    Dim LastTopicPage As Long

    On Error GoTo ReportError
    
    webex.Trace("Entering PreSeq.mmbas", 2)

    If webex.ActivePage.Type = wePageTypeCallout Then

        ' no next/prev navigation
        s = ""

      Else

        ' If we have more than one page
        pageCount = webex.Pages.Count
        currentPageIndex = webex.ActivePageIndex

        If pageCount > 0 Then

          s = webex.ReadFile("PreSeq.html")
          homeFilename = webex.GetParameter(WeRootExportFilenameResolved)

        If currentPageIndex <= 1 Then
            previousFilename = webex.ActivePage.Filename
        Else
            PageToLinkTo = currentPageIndex - 1
            ' If previous page is a callout page, link to next available topic page
            While webex.Pages.Item(PageToLinkTo).Type = wePageTypeCallout And (PageToLinkTo > 0)
                PageToLinkTo = PageToLinkTo - 1
            Wend
            ' Link to root page if no topic page to link to was found
            If PageToLinkTo = 0 Then
                previousFilename = homeFilename
            Else
                previousFilename = webex.Pages.Item(PageToLinkTo).Filename
            End If
        End If

        If currentPageIndex = pageCount Then
            '
            nextFilename = webex.ActivePage.Filename
        Else
            PageToLinkTo = currentPageIndex + 1
            LastTopicPage = PageToLinkTo
            ' If next page is a callout page, link to next available topic page
            While webex.Pages.Item(PageToLinkTo).Type = wePageTypeCallout And (PageToLinkTo < webex.Pages.Count)
                PageToLinkTo = PageToLinkTo + 1
            Wend

            ' If the page we link to is still a callout, link to the current page
            If webex.Pages.Item(PageToLinkTo).Type = wePageTypeCallout Then
                nextFilename = webex.ActivePage.Filename
            Else
                nextFilename = webex.Pages.Item(PageToLinkTo).Filename
            End If

        End If

        s = Replace(s, "#PreviousFilename#", webex.PathToUrl(previousFilename))
        s = Replace(s, "#NextFilename#", webex.PathToUrl(nextFilename))
      
        End If

    End If

   ' Return our string (even if it is empty)
    webex.PushString(s)

    webex.Trace("Exiting PreSeq.mmbas", 2)
    
    Exit Sub
   
ReportError:
    ProcessError(webex)
    Exit Sub
   
End Sub
