var global = parent.parent
global.slideSelector = "slideSelector"
var undefined

if ( ! window.home ) window.home = 'http://www.SpiritAndTruth.org'

global.slideList = undefined
if ( ! global.showingIndex ) global.showingIndex = 0
global.controlText = undefined

// Alternative between choose pane contents.
global.choose1 = 'choose1.htm'
global.choose2 = 'choose2.htm'
if ( ! global.choose ) global.choose = global.choose1

// Alternative between control pane contents.
global.control1 = 'control1.htm'
global.control2 = 'control2.htm'
if ( ! global.control ) global.control = global.control1

// auto advance time setting
if ( window.advanceSeconds === undefined ) 
    {
    window.advanceSeconds = 0
    }
global.seconds = 0

// auto advance control
if ( global.autoadvance === undefined ) 
    {
    global.autoadvance = 0
    }

// Is player internal or external?
if ( global.playerInternal === undefined )
    {
    global.playerInternal = true
    global.prevPlayerInternal = global.playerInternal
    }

// Is audio on or off?
if ( global.play === undefined )
    {
    global.play = true
    global.prevPlay = global.play
    }

// The current slide number.
if ( global.slideIndex === undefined )
    {
    global.slideIndex = 0
    }

global.isMicrosoft = function()
    {
    var appName = navigator.appName;
    var split = appName.split( ' ' );
    return ( split[ 0 ] == 'Microsoft' );
    }

global.onHome = function()
    {
    parent.location.href = window.home
    }

// Exit button pressed.
// Now kept by global.
//if ( global.iHistory == undefined ) global.iHistory = global.history.length
global.onExit = function()
    {

    // Forget slide number so we start with first slide next time.
    //document.cookie = 'slide='

    // Just in case history isn't working properly,
    // we force close on two consecutive close attempts.
    if (! window.exitActivated )
        {
        window.exitActivated = 1
        }
    else
        {
        window.exitActivated += 1
        if ( 1 < window.exitActivated )
            {
            alert( 'Unable to close webshow. Please close using "File...  Exit..." from the menu instead.' )
            }
        global.close()
        return false
        }

    var histLength = global.history.length
    var delta = histLength - global.iHistory
    delta = delta + 1
    if (! global.iHistory || ! delta ) 
        {
        global.close()
        }
    else
        {
        global.history.go( -delta )
        }
    return false
    }

// Help button pressed.
global.onHelp = function( arg )
    {
    global.needHelp = !global.needHelp
    parent.view_pane.location.href = 'splash.htm'
    return false
    }

// Previous button pressed.
global.onPrevious = function( arg )
    {

    // disable any auto advance
    window.advanceSeconds = 0
    global.seconds = 0

    // Move to previous slide (may wrap around the last slide).
    prev = global.slideIndex
    global.slideIndex = global.slideIndex - 1
    if ( global.slideIndex < 1 )
        {
        global.slideIndex = global.lastSlide
        }
    // return global.S( global.slideIndex )
    return global.S( global.slideIndex )
    }

// Begin button pressed.
global.onBegin = function( soundOnly )
    {
    if (! global.start ) return

    if ( soundOnly && global.play ) global.onPlay()
    if ( !soundOnly && !global.play ) global.onPlay()

    global.slideIndex = 1
    global.isFirstSlide = 1
    global.onNext()
    return false
    }

// Play audio only pressed.
global.onAudioOnly = function()
    {
    if (! global.start ) return

    //parent.audio_pane.location.replace( 'index.m3u' )
    top.location.replace( 'index.m3u' )
    return false

    }

// Next button pressed.
global.onNext = function( arg )
    {

    //alert( 'onNext ' + arg )
    // If timeout called us and we've disabled auto-advance.
    if ( arg !== undefined && arg == 'timeout' && global.seconds == 0 )
        {
        return false
        }
    // Auto advance, if necessary.
    if ( window.advanceSeconds != 0 )
        {
        global.seconds = window.advanceSeconds
        }

    // Handle auto-advance if set.
    secs = global.seconds - 0
    if ( secs != 0 )
        {
        //alert( 'onNext timeout ' + secs )
        window.setTimeout( "window.onNext( 'timeout' );", 1000 * secs )
        }

    // Move to next slide, may wrap around to first slide.
    if ( global.isStarted() )
        {
        if ( global.slideIndex < global.lastSlide )
            {
            global.slideIndex = global.slideIndex + 1
            }
        else
            {
            global.slideIndex = 1
            }
        }

    global.isFirstSlide = 0
    return global.S( global.slideIndex )
    }

// Return the name of the file for the specified slide number.
slideFile = function( slideNumber )
    {
    // May be asking for very first slide.
    if ( 0 == slideNumber )
        {
        slideNumber = 1
        }
    var file =  slideNumber + '.htm'
    if ( window.slides !== undefined && 0 < window.slides.length )
        {
        file = window.slides[ slideNumber - 1 ]
        }
    return file
    }

// Return the name of the associated audio file for the specified slide
// number.
audioFile = function( slideNumber )
    {
    var file = ''
    if ( global.play )
        {
        if ( window.audios !== undefined )
            {
            file = window.audios[ slideNumber - 1 ]
            }
        }
    //alert( 'audioFile ' + file )
    return file
    }



// A new slide has been selected.
newSlide = function()
    {
    var iSlide = global.slideIndex

    // Save current slide in case leave and return so
    // can resume where we left off.
    document.cookie = 'slide=' + iSlide

    // Show the new selection.
    updateSelection( global.slideSelector, iSlide )

    // If audio is enabled or it has just been turned on or off.
    if ( global.play || global.play != global.prevPlay )
        {

        // Prepare to load the audio file.
        global.aFile = audioFile( global.slideIndex )

        // If turning off the audio. 
        if ( ! global.play )
            {

            // If we've already started playing audio and
            // we are using an external player.
            if ( global.audioStarted && ! global.playerInternal )
                {

                // Load 'silence' file to stop the player.
                global.aFile = window.silence

                }

            // Else haven't started playing yet and the user is just
            // turning off audio prior to starting the show.
            else
                {

                // Don't load any file.
                global.aFile = undefined

                }

            }

        // Play audio file externally?
        if ( global.aFile && ! global.playerInternal )
            {
            //parent.audio_pane.location.href = global.aFile
            parent.audio_pane.location.replace( global.aFile )
            }

        // We've now played at least one audio file.
        global.audioStarted = true

        // Keep track of state for next time.
        global.prevPlay = global.play

        }

    // Show current slide position in list of slides.
    showSlideList( iSlide )

    // Change the slide
    hrefPrev = parent.view_pane.location.href
    href = slideFile( global.slideIndex )
    if ( href != hrefPrev ) parent.view_pane.location.href = href
    global.isFirstSlide = 0

    // Play audio file internally?
    if ( global.aFile && global.playerInternal )
        {
        redrawControls()
        }
    }

global.netscape = function( )
    {
    var reply = navigator.appName
    var idx = reply.search( 'Netscape' )
    return ( -1 != idx )
    }

global.msie4 = function( )
    {
    var reply = navigator.appVersion
    var idx = reply.search( 'MSIE 4.' )
    return ( -1 != idx )
    }

// Display as bold.
bold = function( text )
    {
    return '<b>' + text + '</b>'
    }

// Display the slide list with the current slide highlighted.
showSlideList = function( iSlide )
    {

    // Show current slide position in list of slides.
    //var slideList = '<body bgcolor="#B7CCCF" >'
    var slideList = ''
    slideList += '<head>'
    slideList += '<style>'
    slideList += '<!--'
    slideList += 'a{text-decoration:none}'
    slideList += '//-->'
    slideList += '</style>'
    //slideList += '<style fprolloverstyle>A:hover {font-weight: bold; font-size: 110%;}'
    slideList += '<style fprolloverstyle>A:hover {text-decoration: underline; font-size: 110%;}'
    slideList += '</style>'
    slideList += '</head>'
    slideList += '<body bgcolor="black" alink="white" vlink="white" link="white" text="white" onKeyDown="return parent.parent.onKey( event )" >'

    //var slideList = '<body  >'
    slideList += '<center>'
    if ( global.banner ) slideList += '<H3><i>' + global.banner + '</i></H3>'
    slideList += '<b>SLIDES</b><br><br><table border=0><tbody valign="top">'
    for (var i = 1 ; i <= global.lastSlide ; ++i )
        {

        // Set attributes specially for the current slide.
        var hash = iSlide
        var anchor = ''
        var bgcolor = 'black'
        var fontcolor = 'white'
        if ( i == iSlide )
            {
            anchor = 'NAME="' + hash + '" '
            bgcolor = 'white'
            fontcolor = 'black'
            }

        slideList += '\n<tr align=left>'
        slideList += '<td>'
        slideList += '<font size="-1">'
        slideList += bold( i )
        slideList += '</font>'
        slideList += '</td><td bgcolor=' + bgcolor + ' >'
        slideList += '<a ' + anchor
        slideList += 'HREF=' + jscriptUrl( i ) + '>'
        slideList += '<font size="-1" color=' + fontcolor + '>'
        slideList += bold( slideInfo( i, 0, '<br>' ) )
        slideList += '</font>'
        slideList += '</a>'
        slideList += '</td></tr>'
        slideList += '<tr></tr>'
        }

    slideList += '\n</tbody></table>'
    slideList += '</center></body>'

    // Rewrite slide list.
    global.slideList = slideList

    // If the slide choose pane exists now.
    // Filter out redundant calls to change chooser contents.
    // DISABLED
    if ( parent.choose_pane && (true || iSlide != global.showingIndex ) )
        {

        global.slideIndex = iSlide
        global.showingIndex = iSlide

        // We alternate between file names to force a reload.
        // (Both files share the same contents from a javascript include.)
        var chooseFile = global.choose
        if ( chooseFile == global.choose1 ) 
            {
            chooseFile = global.choose2
            }
        else
            {
            chooseFile = global.choose1
            }
        global.choose = chooseFile
        //parent.choose_pane.location.href = global.choose + '#' + global.slideIndex
        parent.choose_pane.location.replace( global.choose + '#' + global.slideIndex )
        }
    }

// Return a URL suitable for changing to a specified slide number.
jscriptUrl = function( slideNumber )
    {
    return '"JAVASCRIPT:void global.S(' + "'" + slideNumber + "'" +  ')"'
    }

// Get the information associated with a slide (the title,
// the slide file name, and any audio file).
slideInfo = function( slideNumber, allInfo, sep )
    {
    var info = ''
    if ( window.titles !== undefined && window.titles[ slideNumber - 1 ] !== undefined )
        {
        info = info + window.titles[ slideNumber - 1 ]
        }
    else
        {
        info = info + 'Slide ' + slideNumber
        }
    if ( allInfo )
        {
        info = info + sep + '<font size=-1>'
        info = info + sep + slideFile( slideNumber )
        if ( global.play )
            {
            info = info + sep + audioFile( slideNumber )
            }
        if ( slideNumber == global.slideIndex )
            {
            var advance = ''
            if ( window.advanceSeconds !== undefined && window.advanceSeconds != 0 )
                {
                info = info + '<br>advance = ' + window.advanceSeconds + ' secs'
                }
            }
        info = info + '</font>'
        }
    return info
    }

// Find a control on our control form.
controlFromName = function ( controlName )
    {

    var doc = parent.control_pane.document
    //alert( 'controlFromName doc ' + doc )
    var form = doc[ 'xForm' ]
    //alert( 'controlFromName ' + form )
    var control = form[ controlName ]
    return control
    }

// Read the value of a specified control.
readValue = function ( controlName )
    {
    var control = controlFromName( controlName )
    var value = control.value
    return value
    }

// Read the selection of a specified control.
readSelection = function ( controlName )
    {
    var control = controlFromName( controlName )
    var index = control.selectedIndex
    var opt = control.options[ index ]
    var sel = opt.text
    return sel
    }

// Update the slide selector control.
updateSelection = function( controlName, text )
    {
    control = controlFromName( controlName )
    if ( control !== undefined )
        {
        var options = control.options;
        if ( 
            options !== undefined && 
            options.length !== undefined &&
            options.length != '' 
            )
            {
            for ( var i = 0 ; i < options.length ; ++i )
                {
                if ( options[ i ].text == text )
                    {
                    control.selectedIndex = i;
                    return;
                    }
                }
            }
        }
    }

// Change to a new slide.
// We use a short function name because this function
// name appears in all the slide URLs.
global.S = function( arg )
    {
    //alert( 'S1 + ' + arg )
    var value = arg
    if ( value === undefined )
        {
        value = readSelection( global.slideSelector )
        }
    value = value - 0
    //alert ( 'S2 ' + value )
    global.slideIndex =  value
    newSlide()
    return true
    }

// Has the slide show been started?
global.isStarted = function()
    {
    return ( global.isFirstSlide != undefined  && !global.isFirstSlide )
    }

// User pressed play button to toggle audio on/off.
global.onPlay = function( arg )
    {
    //alert( 'onPlay' )
    global.prevPlay = global.play
    global.play = ! global.play

    // Update button title.
    redrawControls()

    // Wait for show to start before re(starting) audio for the
    // current slide.
    if ( global.isStarted() )
        {
        newSlide()
        }
    return false
    }

// User pressed advance button to toggle auto advance on/off.
global.onAdvance = function( arg )
    {
    //alert( 'onAdvance' )
    if ( ! global.autoadvance && ! global.advancewarning )
        {
        alert( 'Auto-advance is not compatible with all internet browsers. If you encounter problems try turning auto-advance off.' )
        global.advancewarning = 1
        }
    global.autoadvance = ! global.autoadvance

    // Update button title.
    redrawControls()

    return false
    }


// User pressed embedded button.
global.onEmbedded = function( arg )
    {
    //alert( 'onEMbedded' )
    global.prevPlayerInternal = global.playerInternal
    global.playerInternal = ! global.playerInternal

    // Update button title.
    redrawControls()

    // Wait for show to start before relocating audio for the
    // current slide.
    if ( global.isStarted() )
        {
        newSlide()
        }
    return false
    }

// Redraw the controls.
redrawControls = function()
    {

    // Alternate names with identical contents forces reload.
    var controlFile = global.control
    if ( controlFile == global.control1 ) 
        {
        controlFile = global.control2
        }
    else
        {
        controlFile = global.control1
        }
    global.control = controlFile
    //parent.control_pane.location.href = global.control
    parent.control_pane.location.replace( global.control )

    }

// Draw a graphics button.
buttonGraphic = function( src, alt, call )
    {
    text = '<A HREF="JAVASCRIPT:void ' + call + '"><IMG SRC="'
    text += src + '" ALT="' + alt + '" BORDER=0 WIDTH="25" HEIGHT="25" align="absbottom" ></A>'
    return text
    }

// Draw a text button.
buttonText = function( val, call )
    {
    text = '<input type="button" value="' + val + '" '
    text += 'onClick="' + call + '">'
    return text
    }

// Draw the controls.
draw = function()
    {
    
    // Wait for all panes to be defined before drawing.
    if (! global.start ) return

    var controlText = ''
    controlText += '<body bgcolor="black" text="white" onKeyDown="return parent.parent.onKey( event )" ><center>'
    controlText += '<form NAME="xForm" onSubmit ="return global.S( undefined )">'
    controlText += '<table><tbody valign="middle"><tr><td>'

    controlText += buttonGraphic( 'btn_close.gif', 'Exit', 'global.onExit( this )' )
    controlText += '</td><td>'
    controlText += buttonGraphic( 'btn_house.gif', window.home, 'global.onHome( this )' )
    controlText += '</td><td>'

    controlText += buttonGraphic( 'btn_help.gif', 'Help', 'global.onHelp( this )' )
    controlText += '</td><td>'

    var src = 'btn_spkroff.gif'
    var alt = 'Audio is off.'
    if ( global.play ) 
        {
        src = 'btn_spkr.gif'
        alt = 'Audio is on.'
        }
    controlText += buttonGraphic( src, alt, 'global.onPlay( this )' )
    controlText += '</td><td>'

    var src = 'btn_spkrout.gif'
    var alt = 'Audio plays outside browser.'
    if ( global.playerInternal ) 
        {
        src = 'btn_spkrin.gif'
        alt = 'Audio plays inside browser.'
        }
    controlText += buttonGraphic( src, alt, 'global.onEmbedded( this )' )

    if ( global.aFile && global.playerInternal )
        {
        //controlText +=  '<br><embed width="250" height="45" autostart="true" src="' + global.aFile + '">'
        controlText += '</td><td>'

        // Use autoadvance features?
        // CRASHES IN SOME VERSIONS
        // IE = 6.0.2800.1106.xpsp2.030422-1633
        // OBJECT for Microsoft with support for callback.
        if ( global.autoadvance  )
            {
            controlText += '<OBJECT ID="MediaPlayer" width=250 height=45 '
            controlText += 'classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95"'
            controlText += 'codebase= "http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,02,902"'
            controlText += 'standby="Loading Microsoft Windows Media Player components..." '
            controlText += 'type="application/x-oleobject" >'
            controlText += '<PARAM NAME="FileName" VALUE="' + global.aFile + '">'
            controlText += '<PARAM NAME="AutoStart" VALUE="true">'
            }

        // EMBED for Netscape.
        controlText +=  '<embed name="MediaPlayer" width="250" height="45" autostart="true" src="' + global.aFile + '" ShowStatusBar="false"></embed>'

        // End of Microsoft object.
        if ( global.autoadvance  )
            {
            controlText += '</OBJECT>'

            // Code to detect end of playback and autoadvance in IE.
            controlText += '<SCRIPT FOR="MediaPlayer" EVENT="EndOfStream(lResult)" LANGUAGE="Jscript">'
            controlText += 'global.onNext( this );'
            controlText += '</SCRIPT>'

            }

        }

    controlText += '</td><td>'

    controlText += '<SELECT NAME="' + global.slideSelector + '" onChange="return global.S( undefined )" >'
    global.lastSlide = 100
    if ( window.slides !== undefined && 0 < window.slides.length )
        {
        global.lastSlide = window.slides.length
        }
    for (count = 1 ; count <= global.lastSlide; ++count )
        {
        controlText += '<OPTION>' + count
        }
    controlText += '</SELECT>'

    controlText += '</td><td>'

    controlText += buttonGraphic( 'btn_lt1.gif', 'Previous slide.', 'global.onPrevious( this )' )

    var src = 'btn_advancenot.gif'
    var alt = 'Auto-advance is off.'
    if ( global.autoadvance ) 
        {
        src = 'btn_advance.gif'
        alt = 'Auto-advance is on.'
        }
    controlText += buttonGraphic( src, alt, 'global.onAdvance( this )' )

    controlText += buttonGraphic( 'btn_rt1.gif', 'Next slide.', 'global.onNext( this )' )

    controlText += '</td></tr></tbody></table>'
    controlText += '</form>'
    controlText += '</center></body>'


    document.open()
    document.writeln( controlText )
    document.close()

    // Autostoart the first slide?
    // (Otherwise the splash slide remains visible.)
    var autostart = global.autostart
    var slide = 1
    if ( ! autostart ) autostart = window.autostart
    if ( ! autostart && ! parent.cookieRead  )
        {
        parent.cookieRead = 1
        var crumb = document.cookie
        var parts = crumb.split( '=' )
        if ( 1 < parts.length )
            {
            autostart = parts[ 1 ]
            global.autostart = autostart
            }
        }
    if ( 
        autostart !== undefined && 
        ( 0 < autostart  && autostart <= global.lastSlide )
        )
        {
        // Force conversion to number from string.
        var startSlide = autostart - 0
        // Disable subsequent autostart.
        global.autostart = 0
        window.autostart = 0
        global.isFirstSlide = 0
        global.S( startSlide )
        }
    else
        {

        // Show the first slide.
        //slide = 1
        //if ( global.slideIndex ) slide = global.slideIndex
        slide = global.slideIndex
        if ( ! slide ) 
            {
            global.isFirstSlide = 1
            slide = 1
            }
            showSlideList( slide )
        }

    updateSelection( global.slideSelector, slide )
    }

// Draw ourselves each time we are loaded.
draw()

