//*********************************************************************
// Copyright 2001 by TWI AG, CH-2503 Biel/Bienne
//*********************************************************************

var _Debug = false;
// *****************************************************************************
// FindAPIInWindowHierarchy()
//
// p_oWindow	(Object) Topmost window object of the window hiearchy to search 
//				through.
// Returns		(Object) API object found in the window hierarchy.
//			    null => API object not found
//
// This function looks for an object named API in a window hierarchy.
//
function FindAPIInWindowHierarchy( p_oWindow ) 
{
	// Search the window hierarchy for an object named "API"  
	// Look in the current window (p_oWindow) and recursively look in any child frames
	if (_Debug) alert( "FindAPIInWindowHierarchy( '" + p_oWindow.location.href + "' ) called." );

	if (p_oWindow.API != null)
	{
		if (_Debug)	alert( "FindAPIInWindowHierarchy(): Found API in this window." );
		return p_oWindow.API;
	}
		
	if (p_oWindow.length > 0)  // does the window have frames?
	{
		if (_Debug) alert( "FindAPIInWindowHierarchy():  Looking for API in window's frames." );
		for ( var i=0; i<p_oWindow.length; i++ )
		{
			if (_Debug) alert( "FindAPIInWindowHierarchy():  Looking for api in frames[" + i + "]." );
			var theAPI = FindAPIInWindowHierarchy( p_oWindow.frames[i] );
			if ( theAPI != null )
				return theAPI;
		}
	}

	if (_Debug) alert( "FindAPIInWindowHierarchy():  Didn't find API in this window (or in its children)." );
	return null;
}


// *****************************************************************************
// FindAPI()
//
// Returns		(Object) API object found in the windows hierarchy.
//			    null => API object not found
//
// 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 FindAPI()
{	
	var oAPI;
	if (_Debug) alert( "FindAPI() called." );

	// start at the topmost window - findAPI will recurse down through
	// all of the child frames
	oAPI = FindAPIInWindowHierarchy( this.top );

	if ( oAPI == 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. 
		if (_Debug) alert( "FindAPI(): Checking to see if this window has an opener. window.opener typeof is> " + typeof(window.opener) );

		if ( typeof(this.opener) != "undefined" )
		{
			if (_Debug) alert( "FindAPI(): Checking this window's opener." );
			if ( this.opener != null )
			{
				if (_Debug) alert( "FindAPI(): This window's opener is NOT null - looking there." );
				oAPI = FindAPIInWindowHierarchy( this.opener.top );
			}
			else
			{
				if (_Debug) alert( "FindAPI(): This window's opener is null." );
			}
		}
	}
	return oAPI;
}

// *****************************************************************************	
var yaenviado=false;
	function send2Ims(score_total,tiempo_total){ 
	if (yaenviado) return;
	 var LMS=FindAPI();
	    if (LMS!=null){    	    	    	
	   	LMS.LMSInitialize("");
		 var student_id=LMS.LMSGetValue ("cmi.core.student_id");		
		 LMS.LMSSetValue('cmi.core.score.raw',score_total);
	    	 var lesson_status=(score_total)>50?"passed":"failed";    	
		 LMS.LMSSetValue('cmi.core.lesson_status',lesson_status);	
	    	 LMS.LMSSetValue('cmi.core.session_time',sco_parseSecondsToString(tiempo_total));
		 LMS.LMSCommit("");
		 LMS.LMSFinish("");
		 yaenviado=true;
     	     }else{
     	     	//alert("No se encontro el API.");
     	     }
	}