/**
 * KnowledgePool Integration Engine
 * Common functions for inclusion where page navigation is managed.
 *
 * @author Jez
 * @date 29 April 2001
 * @version 1.00
 */

// -----------------------------------------------------------------------------
// Global Variables
// -----------------------------------------------------------------------------

/** Holds the window that contains the KPIE navigation code */
var KPIE_navigationWindow = findKPIENavigationWindow( parent.parent );
/** Holds the window that contains the KPIE content API code */
var KPIE_contentAPIWindow = findKPIEContentAPIWindow( parent.parent );

// -----------------------------------------------------------------------------
// Functions
// -----------------------------------------------------------------------------

/**
 * findKPIENavigationWindow
 * Searches for the window that contains the KnowledgePool content navigation
 * API.  This can then be assigned to a variable.
 *
 * @param	chkWin	Object	Window to check.  The function is called recursively
 * @return	an object representing the window in which the KnowledgePool
 *		Navigation API sits.  This will be null if the API cannot be found.
 */

function findKPIENavigationWindow( chkWin )
{

	var i = 0;
	var currWindow = chkWin;
	var foundIt = null;

	if ( currWindow.KPIE_Navigation_Loaded != null)
	{

		return currWindow;

	}

	for ( i = 0; foundIt == null && i < currWindow.frames.length; i++ )
	{
		foundIt = findKPIENavigationWindow( currWindow.frames[i] );

	}

	return foundIt;	

}

/**
 * findKPIEContentAPIWindow
 * Searches for the window that contains the KnowledgePool content tracking
 * API.  This can then be assigned to a variable.
 *
 * @param		chkWin	Object	Window to check.  The function is called recursively
 * @return	an object representing the window in which the KnowledgePool
 *					content tracking API sits.  This will be null if the API cannot be found.
 */

function findKPIEContentAPIWindow( chkWin )
{

	var i = 0;
	var currWindow = chkWin;
	var foundIt = null;

	if ( currWindow.KPIE_contentAPI_Loaded != null)
	{

		return currWindow;

	}

	for ( i = 0; foundIt == null && i < currWindow.frames.length; i++ )
	{
		foundIt = findKPIEContentAPIWindow( currWindow.frames[i] );

	}

	return foundIt;	

}


/**
 * findKPIEContentObjWindow
 * Searches for the window that contains the content created KPIE content object.
 *
 * @param	chkWin	Object	Window to check.  The function is called recursively
 * @return	an object representing the window in which the KPIE content object is
 *		found.  This will be null if not found.
 */

function findKPIEContentObjWindow( chkWin )
{

	var i = 0;
	var currWindow = chkWin;
	var foundIt = null;

	if ( currWindow.objKPIEContent != null)
	{

		return currWindow;

	}

	for ( i = 0; foundIt == null && i < currWindow.frames.length; i++ )
	{
		foundIt = findKPIEContentObjWindow( currWindow.frames[i] );

	}

	return foundIt;	

}


/**
 * finalTime
 * Stores start time
 * function called on LMSFinish works out time in session and saves to LMS
 */

var clock=new Date();	   
var startTime=clock.getTime();

function finalTime()
{
var leaveTime=new Date();
var totalTime=((leaveTime.getTime()-startTime)/1000); //total time in seconds
var hours=Math.floor(totalTime/3600);      //check hours
var minutes=Math.floor((totalTime%3600)/60); //check minutes
var seconds=Math.floor(totalTime%60);        //check seconds

// format time
var sessionTime = ((hours < 10) ? '0' + hours : hours) + 
                  ':' + ((minutes < 10) ? '0' + minutes : minutes) +
                  ':' + ((seconds < 10) ? '0' + seconds : seconds); 

var KPIE_contentObjWindow = findKPIEContentObjWindow( parent.parent );

//Check if all sections have been completed;
	var sectionLoop;
	var allDone = true;

	for ( sectionLoop = 0; sectionLoop < KPIE_contentObjWindow.objKPIEContent.sections.length; sectionLoop++ )
	{

	  if ( KPIE_contentObjWindow.objKPIEContent.sections[sectionLoop].isSectionComplete() )
		{
			void(0);
		}
		else
		{
			allDone = false;
		}

	}

	// If all the sections have been done then make sure the lesson status is updated.

	if ( allDone )	{
		KPIE_contentObjWindow.objKPIEContent.contentAPI.LMSSetValue( "kpie.core.lesson_status", "completed" );
	}
	else	{
		KPIE_contentObjWindow.objKPIEContent.contentAPI.LMSSetValue( "kpie.core.lesson_status", "incomplete" );
	}

KPIE_contentObjWindow.objKPIEContent.contentAPI.LMSSetValue( "kpie.core.session_time", sessionTime );

}

function doAudio()	{

  if (top.soundOn == 1)
     	{top.soundOn = 0;
	document.audbutt.src = "images/audbutt2.gif";}
  else
    	 {top.soundOn = 1;
	document.audbutt.src = "images/audbutt1.gif";}
}

//to open generic pop-ups for IPS

function  openCredits()  {
	win = window.open ("../scripts/credits/credits.htm", "newwin", "scrollbars=auto,width=640,height=350");
	win.creator = self;
}

function  openGuide()  {
	win = window.open ("../scripts/guide/usertxt1.htm", "newwin", "scrollbars=auto,width=640,height=350");
	win.creator = self;
}

function  openStds()  {
	win = window.open ("standards/standards.htm", "newwin", "scrollbars=yes,width=750,height=500");
	win.creator = self;
}

function  openOview()  {
	win = window.open ("objs/ovw1.htm", "newwin", "scrollbars=auto,width=640,height=400");
	win.creator = self;
}
