'#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
    Dim aPage As Mm5WebExportLib.Page

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

    Dim oneImage As Image
    Dim oneTopic As Topic

    Dim imageFileName As String
    Dim fullImagePath As String
    Dim i As Long
    Dim template As String
    Dim topicString As String
    Dim bodyString As String

    Dim MaxImageSize As Single
    Dim ImageWidth As Single
    Dim ImageHeight As Single

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

    template = webex.ReadFile("SnipImg.htm")

    Set oneTopic = webex.ActiveTopic

    ' Export the topic image if one exists
    If webex.ActiveTopic.HasImage Then
    
        Set oneTopic = webex.ActiveTopic
        Set oneImage = oneTopic.Image

        ' build file name from export page the topic is on plus a counter
        imageFileName =   webex.Pages.FindPage(oneTopic).BaseFilename _
                        + "_img" _
                        + CStr(webex.GetParameter("CustomParameter.ImageCounter")) _
                        + ".gif"
        fullImagePath = webex.FullExportFilename(imageFileName)

        ' Save image
        oneImage.Save(fullImagePath, mmGraphicTypeGif)

        webex.SetParameter("CustomParameter.TopicImage", imageFileName)
        webex.SetParameter("CustomParameter.TopicImageText", VerifyTopicText(webex,oneTopic))

        ' Increment image counter
        webex.SetParameter("CustomParameter.ImageCounter", _
                           webex.GetParameter("CustomParameter.ImageCounter") + 1)

        ' Convert image dimensions to pixels
        ImageWidth = oneImage.Width
        Utilities.MillimetersToPixels (ImageWidth)
        ImageHeight = oneImage.Height
        Utilities.MillimetersToPixels (ImageHeight)

        ' If image is too large, enforce max size
        MaxImageSize = webex.GetParameter(WeMaxTopicImageSize)
        If (MaxImageSize <> 0) And ((ImageWidth > MaxImageSize) Or (ImageHeight > MaxImageSize)) Then
        	Dim ImageWidthReductionFactor As Single
        	Dim ImageHeightReductionFactor As Single
        	Dim ImageReductionFactor As Single

			' Calculate image width reduction factor (but don't divide by zero)
            If ImageWidth = 0 Then
				ImageWidthReductionFactor = 0
            Else
				ImageWidthReductionFactor = MaxImageSize / ImageWidth
			End If

			' Calculate image height reduction factor (but don't divide by zero)
            If ImageHeight = 0 Then
				ImageHeightReductionFactor = 0
            Else
				ImageHeightReductionFactor = MaxImageSize / ImageHeight
   			End If

			' Use the minimum of the two factors
			If ImageWidthReductionFactor < ImageHeightReductionFactor Then
				ImageReductionFactor = ImageWidthReductionFactor
			Else
				ImageReductionFactor = ImageHeightReductionFactor
			End If

			' Reduce our width and height equally (rount to the nearest integer)
			ImageWidth = Round(ImageWidth * ImageReductionFactor)
			ImageHeight = Round(ImageHeight * ImageReductionFactor)
        End If

        webex.SetParameter("CustomParameter.TopicImageWidth", CStr(ImageWidth))
        webex.SetParameter("CustomParameter.TopicImageHeight", CStr(ImageHeight))
        
        ' Read our template
        topicString = template

        ' Process all commands and parameters
        ProcessString webex, topicString

        bodyString = topicString

    Else

         bodyString = ""

    End If

    ' Write out the result
    webex.PushString(bodyString)

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

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