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

Dim oneGraphicExport As GraphicExport
Dim centralTopic As Topic

Sub Main

    Dim addin As Mm5WebExportAddIn
    Dim webex As WebExport
    Dim aPage As Mm5WebExportLib.Page

    Set addin = WebExportHelper.WebExportAddIn
    Set webex = addin.ActiveWebExport

    Dim bookmark As String
    Dim template As String
    Dim topicString As String
    Dim bodyString As String
    Dim altText As String

    Dim oneImage As Image

    Dim Details_mapBlock As String
    Dim Details_imagemapName As String
    Dim Details_imagemapFilename As String
    Dim Details_fullImagemapFilename As String

    Dim Overview_mapBlock As String
    Dim Overview_imagemapName As String
    Dim Overview_imagemapFilename As String
    Dim Overview_fullImagemapFilename As String

    Dim Merged_imagemapString As String

    ' Initialize
    Set centralTopic = webex.ActiveTopic.Document.CentralTopic
    Set aPage = webex.Pages.FindPage(centralTopic)

    On Error GoTo ReportError

    webex.Trace("Entering ToolsMap.mmbas",2)

    ' DETAILS MAP
    Set oneGraphicExport = centralTopic.Document.GraphicExport
    oneGraphicExport.Reset
    oneGraphicExport.AddCustomizedTopic(centralTopic,9)

    ' Add links
    Call TraverseMapAndAddLinks (centralTopic, webex)

    ' Write out the imagemap graphic
    Details_imagemapFilename = "mapContentImagemap_Details" + ".jpg"
    Details_fullImagemapFilename = webex.FullExportFilename(Details_imagemapFilename)
    ' export with user-defined zoom factor
    If webex.ShouldWriteFile(Details_fullImagemapFilename) Then
        Dim zoomFactor As Double
        zoomFactor = webex.GetParameter("Strings.Show.OverviewMapZoomFactor") / 100
        oneGraphicExport.ExportZoomed(Details_fullImagemapFilename, mmGraphicTypeJpeg, zoomFactor, True, 5000, 5000)
    End If
    Details_imagemapName = "Details_Imagemap"
    Details_mapBlock = oneGraphicExport.GetImageMap(Details_imagemapName)

    ' Release object
    Set oneGraphicExport = Nothing

    Merged_imagemapString = webex.ReadFile("ToolsMapContMap.htm")

    ' DETAILS - replace placeholder values
    Merged_imagemapString = Replace(Merged_imagemapString, "#Details_mapBlock#", Details_mapBlock)
    Merged_imagemapString = Replace(Merged_imagemapString, "#Details_imagemapFilename#", Details_imagemapFilename)
    Merged_imagemapString = Replace(Merged_imagemapString, "#Details_imagemapName#", "#" + Details_imagemapName)

    ' Hack to get the image map links to "burst out" of the frame and open the link in parent frame
    AddParentTargetToImagemapLinks(Merged_imagemapString)

    ' Push result string back
    webex.PushString (Merged_imagemapString)

    webex.Trace("Exiting ToolsMap.mmbas",2)

    Exit Sub

ReportError:
    ProcessError(webex)
    Exit Sub

End Sub

Private Sub AddParentTargetToImagemapLinks(ByRef imagemapString As String)

    ' the old way (doesn't handle links that already have target tags!)
    ' imagemapString = Replace(imagemapString, "alt=", "target='_parent' alt=")
    ' Exit Sub
    
    Dim areaTag As String
    Dim index As Long
    index = 1
    
    ' while we can find another "<area...>" tag while advancing through the string
    While (index <= Len(imagemapString)) And FindTag("area", imagemapString, index, areaTag)
    
        ' If there is no "target=" attribute already
        If InStr(areaTag, "target=") = 0 Then
        
            Dim newAreaTag As String
            
            ' update the tag to add the "target="parent"" string
            newAreaTag = Replace(areaTag, "alt=", "target=""_parent"" alt=")
            
            ' replace the old tag with the new tag
            imagemapString = Left(imagemapString, index-1) + newAreaTag + Mid(imagemapString, index + Len(areaTag))
            
            ' advance past this new tag
            index = index + Len(newAreaTag)
        Else
            ' advance past this tag
            index = index + Len(areaTag)
        End If
        
    Wend
    
End Sub

Private Function FindTag(tagName As String, s As String, ByRef index As Long, ByRef fullTag As String) As Boolean

    ' Finds the HTML tagName tag in s starting at index.
    ' If it is found, updates index to point to the start of the tag and
    ' returns the complete tag in fullTag.

    Dim startIndex As Long
    Dim endIndex As Long

    ' assume we don't find the tag
    FindTag = False
    
    ' if we find the start of the tag
    startIndex = InStr(index, s, "<" + tagName)
    If startIndex <> 0 Then
    
        ' if we find the end of the tag
        endIndex = InStr(startIndex, s, ">")
        If endIndex <> 0 Then
            ' advance the index to the start of the tag
            index = startIndex
            
            ' extract the tag into fullTag
            fullTag = Mid(s, startIndex, endIndex - startIndex + 1)
            
            ' return true to indicate we found a tag
            FindTag = True
        End If
    End If

End Function

Sub TraverseMapAndAddLinks (ByVal oneTopic As Topic, ByRef webex As WebExport)

    Dim nextTopic As Topic
    Dim SubTopicCount As Long
    Dim i As Long

    SubTopicCount = oneTopic.SubTopics(False).Count

    For i = 1 To SubTopicCount
        Dim link As HyperlinkData
        Set nextTopic = oneTopic.SubTopics(False).Item(i)
        link = TopicHyperlink(webex, nextTopic)
        oneGraphicExport.AddLink(nextTopic, mmLinkTypeTopic, link.Url, link.Label, TargetAttribute(link))
        Call TraverseMapAndAddLinks(nextTopic, webex)
    Next i

End Sub
