///////////////////////////////////////////////////////////////////////////////////////////////////
//
//	Copyright 2001, 2002, 2003 Microsoft Corporation.  All Rights Reserved.
//	This documentation is only for the internal, personal use by customers who have
//	licensed the Microsoft eLearning Library or who are entitled to use the 
//	eLearning benefits provided via Microsoft's Software Assurance program.  
//	It may not be modified or distributed outside of your organization. 
//
//	@doc External
//
// 	@module FindLMSAPI.js |
//		Contains the client-side Javascript that searches the window 
//		and framesets heirarchy to find an implementation of the AICC LMS API.
//	@end
//
///////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////
//
//	@doc Internal
// 
//	@jsfunc FindLMSAPI | 	This function retrieves the JScript LMSAPI object.
//				Starting with the current window, it checks for the LMSAPI object.
//				It then trace back through the windows' parents.  If the LMSAPI object
//				is found, it is returned.  Once the trace back ends, the algorithm
//				checks the opener window and all its parents for the LMSAPI object. 
//
//	@rdesc  A valid LMSAPI object or null if not found
//
////////////////////////////////////////////////////////////////////////////////////////////////////
function FindLMSAPI()
{
	var oWalkOpeners = window;
	var oWalkParents = window;
	
	try
	{
		while (oWalkOpeners)
		{
			while (oWalkParents)
			{
				if ("object" == typeof(oWalkParents.API)) 
				{
					try
					{
						// Call one of the SCORM APIs.  If it doesn't throw,
						// we have found the correct object.
						oWalkParents.API.LMSGetLastError();
						return oWalkParents.API;
					}
					catch(e)
					{
					}
				} 
				
				if (oWalkParents == oWalkParents.parent) break;

				oWalkParents = oWalkParents.parent;
			}
			
			if (oWalkOpeners == oWalkOpeners.opener) break;

			oWalkOpeners = oWalkOpeners.opener;
			oWalkParents = oWalkOpeners;
		}
	}
	catch(e)
	{
	}
	return null;	// Couldn't find it.
}
