// ver 5.2
// EJF 05/25/04

/******************************************************************************************
**
** Function findAPI(win)
** Inputs:	win - a Window Object
** Return:	If an API object is found, it is returned, otherwise null is returned.
**
** Description:
** This function looks for an object named API in the supported window hierarchy, 
** 
******************************************************************************************/

function findAPI(win) 
{
	/*DEBUG*/ if(_DebugLMS){log.write("Checking current window: "+win.location.href, "findapi.js");}

	if(win.API != null)
	{
		/*DEBUG*/ if(_DebugLMS){log.write("Found API in this window: "+win.location.href, "findapi.js");}
		return win.API;
	}

	if(win.length > 0)  // does the window have frames?
	{
		/*DEBUG*/ if(_DebugLMS){log.write("looking for api in windows frames", "findapi.js");}
		for (var i=0;i<win.length;i++)
		{
			/*DEBUG*/ if(_DebugLMS){log.write("looking for api in frames["+i+"]", "findapi.js");}
			var theAPI = findAPI(win.frames[i]);
			if(theAPI != null)
			{
				return theAPI;
			}
		}
	}

	/*DEBUG*/ if(_DebugLMS){log.write("API NOT FOUND. Returning null.", "findapi.js");}
	return null;
}


/******************************************************************************************
**
** Function getAPI()
** Inputs:	none
** Return:	If an API object is found, it is returned, otherwise null is returned.
**
** Description:
** This function looks for an object named API, first in the current window's hierarchy, 
**  and then, if necessary, in the current window's opener window hierarchy (if there is
**  an opener window).
******************************************************************************************/

function getAPI()
{
	/*DEBUG*/ if(_DebugLMS){log.write("Starting getAPI().", "findapi.js");}

	var theAPI = findAPI(this.top);

	if(theAPI == null)
	{
		// the API wasn't found in the current window's hierarchy.  If the
		// current window has an opener (was launched by another window),
		// check the opener's window hierarchy. 
		/*DEBUG*/ if(_DebugLMS){log.write("Checking to see if this window has an opener.\r\n\r\nwindow.opener typeof is: "+typeof(window.opener), "findapi.js");}

		if(typeof(this.opener) != "undefined")
		{
			/*DEBUG*/ if(_DebugLMS){log.write("Checking this windows opener.", "findapi.js");}

			if(this.opener != null)
			{
				/*DEBUG*/ if(_DebugLMS){log.write("This windows opener is NOT null - looking there.", "findapi.js");}
				theAPI = findAPI(this.opener.top);
			}
			else
			{
				/*DEBUG*/ if(_DebugLMS){log.write("This windows opener is null.", "findapi.js");}
			}
		}
	}
	return theAPI;
}



