'----------------------------------------------------------------------------
'			HTML-TIME-Slideshow.vbs
' This is a HTML slideshow generator written in VBScript.
' It generates a slideshow from the selected pictures of the current album.
' The HTML code uses the new HTML+TIME (Timed Interactive Multimedia Extensions)
' Microsoft Internet Explorer 5.5 or later is needed to view the slideshow.
' This script should not be used on large slideshow as IE loads all the
' pictures in memory...
'----------------------------------------------------------------------------

Option Explicit

app.ClearTrace

Dim alb, pic, nbPic, nbPicV, i, k, outputFileName, picFile, duration
Dim bAutoRepeat, sList, kT, bUseSelectionList, sListElemDS, sCommentDS

' Retrieve info on the current album
set alb = app.GetCurrentAlbum
nbPic = alb.nbSelectedPicture
nbPicV = alb.nbVisiblePicture
Const ForReading = 1, ForWriting = 2
bAutoRepeat = True
if vbNo = MsgBox( "Build an auto-repeat slideshow ?", vbYesNo or vbQuestion, "HTML+TIME slideshow") then bAutoRepeat = False
bUseSelectionList = True
if vbNo = MsgBox( "Generate a selection drop-down list ?" & vbCrLf & "(Will not work well if picture durations are less than a second)", vbYesNo or vbQuestion, "HTML+TIME slideshow") then bUseSelectionList = False
' Text for the list items (default: first line of picture comment) and the comment
sListElemDS = "%1C"
sCommentDS = "%FC"

' Create a new HTML file for output
Dim fso, f, m
Set fso = CreateObject("Scripting.FileSystemObject")
outputFileName = InputBox( "Please enter the name of the HTML file to build", "HTML+TIME slideshow", "d:\temp\AlbTime.html")
app.Trace "Creating output file '" & outputFileName & "'..."
Set f = fso.OpenTextFile( outputFileName, ForWriting, True) 

' Build header of the HTML page
f.WriteLine "<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">"
f.WriteLine "<HTML XMLNS:t=""urn:schemas-microsoft-com:time"">"
f.WriteLine "<HEAD>"
f.WriteLine "<?IMPORT namespace=""t"" implementation=""#default#time2"">"
f.WriteLine "<TITLE>" & alb.sAlbumTitle & "</TITLE>"
f.WriteLine " <META HTTP-EQUIV=""Content-Type"" CONTENT=""text/html; charset=UTF-8"">"

' The "time" style is used to apply the "time2" behavior to HTML elements
'f.WriteLine "<XML:NAMESPACE PREFIX=""t""/>"
f.WriteLine "<STYLE>"
f.WriteLine "	.time{ behavior: url(#default#time2); }"
f.WriteLine "	BUTTON {width:60px}"
f.WriteLine "</STYLE>"

' Function for pausing and restarting the slideshow
f.WriteLine "<SCRIPT LANGUAGE=javascript>"
f.WriteLine "function doPauseResume(bPause) {"
f.WriteLine "    if (bPause)"
f.WriteLine "		sequence.pauseElement();"
f.WriteLine "	else"
f.WriteLine "		sequence.resumeElement();"
f.WriteLine "    pauseBtn.disabled = bPause;"
f.WriteLine "    resumeBtn.disabled = !bPause;"
f.WriteLine "}"
f.WriteLine "function selectElement(list) {"
f.WriteLine "    var index, tValue;"
f.WriteLine "    index = list.selectedIndex;"
f.WriteLine "    tValue = list.options[index].value;"
f.WriteLine "    sequence.seekActiveTime(tValue);"
f.WriteLine "}"
f.WriteLine "</SCRIPT>"
f.WriteLine "</HEAD>"

f.WriteLine "<BODY><center>"
f.WriteLine "<font size=+3><B>" & alb.sAlbumTitle & "</B></font>"
if alb.sAlbumComment <> "" then f.WriteLine "<br><font size=+1><B>" & alb.sAlbumComment & "</B></font>"
f.WriteLine ""

' Here starts the IE5.5 specific HTML code
f.WriteLine "<!--[if gte IE 5.5000]>"

' We start the page with a wait message while the pictures are loaded, then
' we display "false" hyperlink, used to start the slideshow. It's end condition is
' the click on the element itself. The click will also start other elements.
f.WriteLine "<SPAN ID=""waitText""><br><br>Please wait...</SPAN>"
f.WriteLine "<SPAN CLASS=""time"" ID=""click"" STYLE=""CURSOR: hand; TEXT-DECORATION: underline"" END=""click.onclick"" TIMEACTION=""display"" onbegin=""waitText.style.display='none'"">"
f.WriteLine "	<BR><BR>Click here to begin"
f.WriteLine "</SPAN>"

' Create two buttons and a frame counter. The will appear after the user has clicked the begin text
f.Write     "<SPAN class=""time"" BEGIN=""click.onclick"""
if bUseSelectionList then f.Write " onBegin=""loadList();"""
f.WriteLine ">"
f.WriteLine "  <BR>"
f.WriteLine "  <BUTTON ID=""pauseBtn"" onclick=""doPauseResume(true);"">Pause</BUTTON>&nbsp;&nbsp;"
f.WriteLine "  <BUTTON ID=""resumeBtn"" DISABLED onclick=""doPauseResume(false);"">Resume</BUTTON>&nbsp;&nbsp;"
f.WriteLine "  <SPAN id=""counter"">0</span>/" & nbPic & "<br>"
if bUseSelectionList then
  f.WriteLine "  <SPAN id=""navSelect"">"
  f.WriteLine "    <SELECT ID=""selectelt"" onChange=""selectElement(this);"">"
  f.WriteLine "    </SELECT>"
  f.WriteLine "  </SPAN>"
end if
f.WriteLine "</SPAN>"

' The HTML+TIME seq will display all the pictures in sequence
f.WriteLine "<DIV STYLE=""HEIGHT: 100%; BACKGROUND-COLOR: #c0c0c0; TEXT-ALIGN: center"">"
f.WriteLine "<SPAN CLASS=""time"" BEGIN=""click.onclick"">"
f.WriteLine "<!-- sequence of pictures -->"
f.WriteLine "  <t:SEQ ID=""sequence"" BEGIN=""click.onclick"" DUR=""indefinite"">"

' Process all the selected pictures
k = 1
kT = 0
for i=0 to nbPicV-1
  Set pic = alb.GetVisiblePicture(i)
  if pic.bSelected then
    app.trace pic.sFileName      
    picFile = alb.ExpandMacro( pic, "%RP" )
    ' Retrieve the duration from the picture if it is define, if not take the
    ' default value from the album (values in 1/10 of s).
    duration = pic.lTransition and &hffff
    if duration = 65535 then duration = alb.nDelaySlideshow
    duration = duration / 10
    if bUseSelectionList then
      sList = sList & "  s += '<option value=""" & ceil(kT+duration/2) & """>" & replace(alb.ExpandMacro( pic, sListElemDS ), "'", "\'") & "</option>';" & vbCrLf
'app.Trace "  " & kT & "-->" & ceil(kT)
      kT = kT + duration
    end if

    ' Include the picture and its comment in a span that have the "time" style
    ' The Begin of the span will also update the frame counter
    f.Write "    <SPAN CLASS=""time"" DUR=""" & replace( duration, ",", ".") & """ TIMEACTION=""display"" onbegin=""counter.innerText='" & k & "';" 
    if bUseSelectionList then f.Write " selectelt.selectedIndex=" & k-1 & ";"
    f.Write """"
    if bAutoRepeat and i = nbPic-1 then
      ' If auto-repeat is selected and it's the last picture, restart the sequence
      f.Write " onend=""sequence.beginElement();"""
    end if
    f.WriteLine ">"
    f.Write "     <br><IMG ID=""img" & k & """ SRC=""" & app.HTMLFileName(picFile, "") & """"
    ' Adapt the size using the orientation of the picture
    if pic.w > pic.h then
        f.WriteLine " width=""90%"">"
    else
        f.WriteLine " width=""50%"">"
    end if
    f.WriteLine "     <br>" & app.ConvertString( alb.ExpandMacro( pic, sCommentDS ), 0 )
    f.WriteLine "    </SPAN>"
    k = k + 1
  end if
next 

if not bAutoRepeat then
  ' Auto-repeat not selected; display a "Finished" text and a button to restart the slideshow
  f.WriteLine "    <p CLASS=""time"" DUR=""indefinite"" TIMEACTION=""display"" onbegin=""pauseBtn.disabled = true;"">"
  f.WriteLine "      <br><b>Finished !</b><br><br>"
  f.WriteLine "      <BUTTON ID=""restartBtn"" onclick=""sequence.beginElement(); pauseBtn.disabled = false;"">Restart</BUTTON><br>&nbsp;"
  f.WriteLine "    </p>"
end if

f.WriteLine "  </t:SEQ>"
f.WriteLine "</SPAN>"
f.WriteLine "</DIV>"
f.WriteLine "<i>Built with <a href=""http://www.myalbumpro.com/MyAlbum.html"" target=""new"">MyAlbum</a></i>"
f.WriteLine "</center>"

if bUseSelectionList then
  ' As we have the complete list at the end, use DHTML to add a drop-down list containing the items
  f.WriteLine "<SCRIPT>"
  f.WriteLine "function loadList() {"
  f.WriteLine "  var s;"
  f.WriteLine "  s = '';"
  f.WriteLine "  s += '<SELECT ID=""selectelt"" onChange=""selectElement(this);"">';"
  f.WriteLine app.ConvertString( sList, 0 )
  f.WriteLine "  s += '</SELECT>';"
  f.WriteLine "  navSelect.innerHTML = s;"
  f.WriteLine "}"
  f.WriteLine "</SCRIPT>"
end if
f.WriteLine "<![endif]-->"

f.WriteLine "<![if lt IE 5.5000]>"
f.WriteLine "<br><br><center>This demo requires Microsoft Internet Explorer 5.5 or later.</center>"
f.WriteLine "<![endif]>"

f.WriteLine "</BODY></HTML>"

f.Close
'app.Run outputFileName, True, -1

app.Trace "HTML file generation complete !", -1, TRACE_GREENDOT


function ceil(val)
  ceil = fix(val)
  if ceil <> val then ceil = ceil + 1
end function
