﻿//////////////////////////////////////////////
//  AAManager.js
//  Manager for AutoAdvance feature
//////////////////////////////////////////////

if (!window.AAManager)
	window.AAManager = {};

AAManager.AutoAdvance = function() 
{
    this.timerID = null;
    this.isActive = false;
    
    this.duration = 0;
    
    this.hasOverride = false;
    this.overrideSettings = {
        overrideCount: 0,
        overrideType: null
    }
}

AAManager.AutoAdvance.prototype =
{
    configureAutoAdvance: function(advanceTime)
    {
        // Stop timer from running in case of manual move to new page
        if (this.timerID != null)
        {
            clearTimeout(this.timerID);
            this.isActive = false;

            // Reset settings
            this.duration = 0;
            this.hasOverride = false;
            this.overrideSettings.overrideCount = 0;
            this.overrideSettings.overrideType = null;
        }

        // Set time for timer if page has Auto Advance set
        if (advanceTime > 0)
        {
            var durationInMiliseconds = advanceTime * 1000;

            // Start timer
            this.isActive = true;
            this.duration = advanceTime;
            this.timerID = setTimeout("TimerCompleted()", durationInMiliseconds);
        }
    },

    timerCompleted: function()
    {
        if (this.hasOverride == false)
        {
            this.moveOn();
        }
    },

    // Move to next page (mimic forward button click)
    moveOn: function()
    {
        Forward_OnClick();
    },

    // Use to override timer
    overrideTimer: function()
    {
        clearTimeout(this.timerID);
    },

    // Use to override timer to wait for media to finish if media duration longer than timer
    // Stop timer and use media end event to cause page to auto advance
    overrideTimerWithMedia: function(mediaElement, durationInSeconds)
    {
        if (this.isActive == true)
        {
            // If media duration is less than timer duration, use existing timer
            if (durationInSeconds < this.duration)
            {
                // Do nothing
            }
            // Else override timer with media item
            else
            {
                this.hasOverride = true;
                this.overrideSettings.overrideCount++;
                this.overrideSettings.overrideType = "Media";
                clearTimeout(this.timerID);
            }
        }
    },

    // Forces override of timer
    // Stop timer and use media end event to cause page to auto advance
    // Note: This method does not consider durations to prevent
    // bug caused by media failing to buffer before timer expires
    overrideTimerWithMediaForced: function()
    {
        if (this.isActive == true)
        {
            this.hasOverride = true;
            this.overrideSettings.overrideCount++;
            this.overrideSettings.overrideType = "Media";
            clearTimeout(this.timerID);
        }
    },

    // Called when override object has finished playing
    // Check if overriding timer, and if so move to next page
    overridePlaybackComplete: function()
    {
        if (this.isActive == true && this.hasOverride == true)
        {
            // Check override type
            if (this.overrideSettings.overrideType == "Media")
            {
                // Check override count
                // If 1 this is the override so move on
                if (this.overrideSettings.overrideCount == 1)
                {
                    this.moveOn();
                }
                else if (this.overrideSettings.overrideCount > 1)
                {
                    // Reduce override count, AA is overridden by media item(s)
                    // with a longer duration so wait
                    this.overrideSettings.overrideCount--;
                }
            }
            else
            {
                // Do nothing, override is likely a button so wait for that
            }
        }
    },

    // Use to override timer to wait for a button click, e.g. self-test finished
    // Stops timer and attaches mouse event to button to cause page to auto advance
    overrideTimerWithButton: function(button)
    {
        this.hasOverride = true;
        this.overrideSettings.overrideCount = 1;
        this.overrideSettings.overrideType = "Button";
        clearTimeout(this.timerID);
        // Add event
    }
}

function TimerCompleted()
{
    if (aaMgr != null)
    {
        aaMgr.timerCompleted();
    }
}