﻿/**************************************************************************************

	Description: 
		Contains functionality for logging through a SCORM based API

**************************************************************************************/

var LMS = new Object();
LMS.today = SCORMdate(new Date());
LMS.interactionCnt = 0;
LMS.lesson_id = "lesson2"; // Developer defined id - must be uniqe for this learning object
LMS.SCORMversion = null; // Defines which version of SCORM that has been found

function join( arr, separator )
{
   res = "";

   for( i = 0; i < arr.length; i++ )
   {
      if( i )  res += separator;
      if( arr[ i ] <= 9 ) res += "0";
      res += arr[ i ];
   }

   return res;
}

function SCORMdate(date)  {  return join([ date.getFullYear(), date.getMonth()+1, date.getDate()],    "/" );}
function SCORMtime(time)  {  return join([ time.getHours(),    time.getMinutes(), time.getSeconds()], ":" );}
function secsToTime( sec ) { return join([ Math.floor( sec/3600 ), Math.floor((sec%3600)/60), sec%60], ":");}
function secsToTime2004( sec ) { return "PT" + Math.floor( sec/3600 ) + "H" + Math.floor((sec%3600)/60) + "M" + sec%60 + "S"; }

/*
	returns the LMS API object (may be null if not found)
*/
var findAPITries = 0;
function FindAPIinWin( win, version )
{
   // Difference between 1.2 and 2004
   var APIname = "API";

   if( version == "2004" )
   {
     APIname = "API_1484_11";
   }
         
   // Search for API
   while ( (win[ APIname ] == null) && (win.parent != null) && (win.parent != win))
   {		   
      findAPITries++;
      // Note: 7 is an arbitrary number, but should be more than sufficient
      if (findAPITries > 7) 
      {
         //alert("Error finding API -- too deeply nested.");
         return null;
      }
      
      win = win.parent;
 
   }
   return win[ APIname ];

}


function scanForAPI( version )
{
   var theAPI		= null;
   LMS.SCORMversion = null;
   
   // Start looking for API in current window
   // Catch error to avoid javascript runtime error
   // Errors are handled separately
   try
   {
      theAPI = FindAPIinWin( window, version );
   }
   catch( e )
   {
      return null;
   }


   if ( ( theAPI == null ) && ( window.opener != null ) && ( typeof( window.opener ) != "undefined" ) )
   {
      try
      {
         theAPI = FindAPIinWin( window.opener, version );
      }
      catch( e )
      {
      	 return null;
      }
   }
	/*
	// create a dummy API for testing purposes
   theAPI = {};
   theAPI.LMSFinish = function() { };
   theAPI.LMSInitialize = function() { };
   theAPI.LMSGetValue = function( v ) 
   {
	   if( v == "cmi.lesson_location" ) return "17365";
	   if( v == "cmi.entry" ) return "resume";
	   return "";
   };

   theAPI.LMSSetValue = function( n, v ) {};
	*/
	
	
    LMS.SCORMversion = ( version == "2004" ? "2004" : "1.2" );

	return theAPI;
}


// If forceSCORMversion != null, search for defined version
// else search for highest version
function findAPI( forceSCORMversion )
{
   var theAPI = null;

   if( forceSCORMversion == null || forceSCORMversion == "" )
   {
      // If forceSCORMversion is undefined, first scan for version 2004
      theAPI = scanForAPI( "2004" );
      
      if( theAPI == null )
      {
         // If not found, try to scan for version 1.2
         theAPI = scanForAPI( "1.2" );
      }
   }
   else
   {
      // Scan for the required version
      theAPI = scanForAPI( forceSCORMversion );            
   }    

   return theAPI;
}

