'#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"
Option Explicit
Dim theURL As String
Dim theText As String
Dim theTarget As String
Dim MergedCalloutSections As String
Dim bodyString As String

Sub Main

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

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

    Dim oneTopic As Topic
    Dim oneCallout As Topic
    Dim oneCalloutCollection As Topics

    On Error GoTo ReportError

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

    Set oneTopic = webex.ActiveTopic

    If DoExportRelatedSection_Callouts(oneTopic, webex) Then

        ' Process callout collection
        Set oneCalloutCollection = oneTopic.CalloutTopics(False)

        For Each oneCallout In oneCalloutCollection

            If Not webex.IsSuccessful Then
                Exit For
            End If

			BoxRecCosSectHelper oneCallout, webex

        Next

		' Process summary topics
        If webex.IsSuccessful And oneTopic.HasBoundary Then
        	If oneTopic.Boundary.HasSummaryTopic Then
				BoxRecCosSectHelper oneTopic.Boundary.SummaryTopic, webex
			End If
        End If

        ' Push back the two "related sections" to the web export
        webex.PushString(MergedCalloutSections)

    Else

        ' Nothing to export
        webex.PushString("")

    End If

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

    Exit Sub
 
ReportError:
    ProcessError(webex)
    Exit Sub
 
End Sub

Sub BoxRecCosSectHelper (oneCallout As Topic, webex As WebExport)
    Dim template As String
    Dim customIconFileName As String
    Dim topicString As String
    Dim theHeader As String
    Dim CalloutsString As String
    Dim MMCalloutType As Variant
    Dim theURL As String
    Dim theText As String
    Dim theTarget As String
    Dim relatedSectionFrame As String

    Dim oneTopic As Topic
    Dim oneHyperlink As Hyperlink
    Dim oneIconCollection As Icons
    Dim oneIcon As Icon
    Dim IconCounter As Variant
    Dim oneTopicCollection As Topics

    ' Read related section template
    relatedSectionFrame = webex.ReadFile("BoxOneReSect.htm")

    ' Determine if this is a link callout, if it is not we won't process it
    MMCalloutType = webex.CalloutType(oneCallout)

    If MMCalloutType = weLinkCallout Then

        ' Check if there is a link to include in the header section
        If oneCallout.HasHyperlink = True Then

            ' Header has a hyperlink
            theHeader = webex.ReadFile("SnipHypReHdHyp.htm")

            ' Could be a hyperlink or a bookmark
            Set oneHyperlink = oneCallout.Hyperlink

            ' If there's no hyperlink text for our callout hyperlink,
            ' use the topic text for the hyperlink text
            If Len(oneHyperlink.TextToDisplay) = 0 Then
	            theText = oneCallout.Text
	        Else
	            ' Reset value before call to UrlFromHyperlink
	            theText = ""
            End If

            ' Reset value before call to UrlFromHyperlink
            theTarget = ""

		    webex.UrlFromHyperlink(oneHyperlink, theURL, theText, theTarget)

            ' Process related section header
            webex.SetParameter("CustomParameter.HyperlinkURL", theURL)
            webex.SetParameter("CustomParameter.HyperlinkTarget", theTarget)
            webex.SetParameter("CustomParameter.HyperlinkName", theText)

            ' Process all commands and parameters
            ProcessString webex, theHeader

            ' Our new hyperlink to merge into the related section body item template
            relatedSectionFrame = Replace(relatedSectionFrame,"#RelatedSectionHeader#", theHeader)

        Else

            ' No hyperlink for header, just assign the section template
            ' to header, then replace placeholder for header
            relatedSectionFrame = Replace(relatedSectionFrame,"#RelatedSectionHeader#", VerifyTopicText(webex,oneCallout))

        End If

        ' We created the header section, now iterate over the callout's sub
        ' topics (only one level of sub topics) to get hyperlinks & bookmarks
        Set oneTopicCollection = oneCallout.SubTopics(False)

        For Each oneTopic In oneTopicCollection

            If Not webex.IsSuccessful Then
                Exit For
            End If

            If oneTopic.HasHyperlink = True Then

                ' Hyperlink can be a hyperlink URL or local, or a bookmark
                Set oneHyperlink = oneTopic.Hyperlink

                ' Determine hyperlink file name (if any)
                If oneTopic.Icons(True).Count > 0 Then
                Set oneIconCollection = oneTopic.Icons(True)
                For Each oneIcon In oneIconCollection
                    If oneIcon.Type = mmIconTypeAutomaticHyperlink Then
                        ' Get image counter parameter to ensure unique icon file name
                        IconCounter = webex.GetParameter("CustomParameter.ImageCounter")
                        customIconFileName = RelativeMapMarkerPath(webex, oneIcon.Name + CStr(IconCounter))
                        ' Increment image counter parameter
                        webex.SetParameter("CustomParameter.ImageCounter",IconCounter + 1)
                        ' Save icon
                        oneIcon.Save(webex.FullExportFilename(customIconFileName),mmGraphicTypeGif)
                        webex.SetParameter("CustomParameter.CodeFileName", webex.PathToUrl(customIconFileName))
                        Exit For
                    End If
                Next
                Else
                    webex.SetParameter("CustomParameter.CodeFileName", "")
                End If

                If oneHyperlink.Address = "" Then

                    ' It is a bookmark
                    topicString = webex.ReadFile("SnipBookmarkLink.htm")

                    ' Process related section header
                    webex.SetParameter("CustomParameter.HyperlinkURL", oneHyperlink.TopicBookmarkGuid)
                    webex.SetParameter("CustomParameter.HyperlinkTarget", "")
                    webex.SetParameter("CustomParameter.HyperlinkName", VerifyTopicText(webex,oneTopic))

                Else

                    ' It is a hyperlink
                    topicString = webex.ReadFile("SnipHypRe.htm")

                    ' If there's no hyperlink text for our callout hyperlink,
                    ' use the topic text for the hyperlink text
                    If Len(oneHyperlink.TextToDisplay) = 0 Then
	                    theText = oneTopic.Text
        	        Else
        	            ' Reset value before call to UrlFromHyperlink
        	            theText = ""
                    End If

                    ' Reset value before call to UrlFromHyperlink
                    theTarget = ""

	                webex.UrlFromHyperlink(oneHyperlink, theURL, theText, theTarget)

                    ' Process related section header
                    webex.SetParameter("CustomParameter.HyperlinkURL", theURL)
                    webex.SetParameter("CustomParameter.HyperlinkTarget", theTarget)
                    webex.SetParameter("CustomParameter.HyperlinkName", theText)

                End If

                ' Process all commands and parameters
                ProcessString webex, topicString

                ' Store the results for this topic
                bodyString = bodyString + topicString

            Else

                ' Topic has no hyperlink
                topicString = webex.ReadFile("SnipNoHypRe.htm")
                webex.SetParameter("CustomParameter.HyperlinkName", VerifyTopicText(webex,oneTopic))

                ' Process all commands and parameters
                ProcessString webex, topicString

                ' Store the results for this topic
                bodyString = bodyString + topicString

            End If

        Next

        ' Merge together the header and body of the newly processed callout (with sub topics)
        bodyString = Replace(relatedSectionFrame,"#RelatedItem#", bodyString)

        ' Store the callout sections
        MergedCalloutSections = MergedCalloutSections + bodyString

        bodyString = ""

    End If
End Sub
