/*
 * These three values should be changed during publishing 
 * to reflect the chosen settings.
 */
var strFailStatus = "%FAIL_STATUS%";
var strPassStatus = "%PASS_STATUS%";


function isLMS() {
  return ("%LMS_IND%"=="Y");
}

var intMaxTrials = 5;
var API = null;
var LMSMode;
var LMSStatus;
var bAllowLessonStatusChange = true;
 
/* 
 * Gets the API from the given window.
 * Returns null if not found.
 */
function GetAPIFromWindow(window)
{
    var intTrials = 0;
    
    while ((window.API == null) && (window.parent != null) && (window.parent != window))
    {
        intTrials++;
        if (intTrials > intMaxTrials) { return null; }
        window = window.parent;
    }
    
    return window.API;
}

/* 
 * Looks for the API in the containing window or the parent window,
 * places it on the global variable API.
 * Returns the api found.
 */
function GetAPI()
{
    // If we found it already, just pass it.
    if (API != null) { return API; }
    
    // Find the api in the window.
    API = GetAPIFromWindow(window);
    
    // Find the api in the parent window.
    if (API == null && window.opener != null && typeof(window.opener) != "undefined")
    { API = GetAPIFromWindow(window.opener); }
    
    return API;
}

/*
 * Returns true if the API is found, false otherwise.
 */
function APIExists()
{
    return (GetAPI() != null);
}

/* 
 * Initializes LMS.
 */
function LMSInitialize()
{
    GetAPI().LMSInitialize("");
}

/* 
 * Closes LMS.
 */
function LMSFinish()
{
    GetAPI().LMSFinish("");
}

function GetLesson()
{
    LMSMode = GetAPI().LMSGetValue("cmi.core.lesson_mode");
    return LMSMode;
}

function GetLessonStatus()
{
    LMSStatus = GetAPI().LMSGetValue("cmi.core.lesson_status");
    return LMSStatus;
}

function SetLessonStatus(strStatus)
{
    LMSStatus = strStatus;
    GetAPI().LMSSetValue("cmi.core.lesson_status", LMSStatus.toLowerCase());
    return LMSStatus;
}

function SaveSessionTime()
{
    if (GetAPI() == null)
    { return; }
    
    var now = new Date();
    var hrs = now.getHours().toString();
    var mins = now.getMinutes().toString();
    var secs = now.getSeconds().toString();
    if (hrs.length == 1) { hrs = "0" + hrs; }
    if (mins.length == 1) { mins = "0" + mins; }
    if (secs.length == 1) { secs = "0" + secs; }
    time = hrs + ":" + mins + ":" + secs;

    GetAPI().LMSSetValue("cmi.core.session_time", time);
}

/*
 * Initializes the lesson presentation. 
 * Must be called when the page is loaded.
 */
function InitSCO()
{
    if (GetAPI() == null)
    { return; }
    
    LMSInitialize();
    GetLesson();
    GetLessonStatus();

    if (LMSMode.toLowerCase() == "browse")
    { SetLessonStatus("browsed"); }
    else if (LMSMode.toLowerCase() == "review")
    { bAllowLessonStatusChange = false; }
    else // LMSMode.toLowerCase() == "normal"
    { SetLessonStatus(strFailStatus); }
    
}

function SetPassed()
{
    if (GetAPI() == null)
    { return; }
    
    SetLessonStatus(strPassStatus);
}

/*
 * Tells the LMS that the viewing is done.
 * Must be called when the user unloads the presentation.
 */
function ExitSCO()
{
    if (GetAPI() == null)
    { return; }
    
    SaveSessionTime();
    LMSFinish();
}