//global variables
var isLMSInitialized = false;
var isLMSFinished = false;
var lmBrowse = '-browse';

// CMI Data Model variables
var cmiCredit;
var cmiLessonStatus;
var cmiLessonMode;
var cmiMasteryScore;
var cmiStudentName = null;

// SCO variables
var scoSessionTime = null;
var scoTotalScore  = null;
var scoCompleted   = null; 


function checkLMSPresence()
{
	var x, y, iWidth, iHeight, oWidth, oHeight;

	if (doLMSInitialize() != 'true')
		return false;

	// Check window size
	iWidth =  (browser.ie) ? document.body.offsetWidth - 4  : self.innerWidth;
	iHeight = (browser.ie) ? document.body.offsetHeight - 4 : self.innerHeight;
	top.resizeBy (getWidthOfWBC() - iWidth, getHeightOfWBC() - iHeight);

	oWidth =  (browser.ie) ? getWidthOfWBC() + 10  : top.outerWidth;
	oHeight = (browser.ie) ? getHeightOfWBC() + 120 : top.outerHeight;
	x = (screen.availWidth - oWidth) / 2;
	y = (screen.availHeight - oHeight) / 2;
	
	if ( x < 0 ) x = 0;
	if ( y < 0 ) y = 0;
	
	top.moveTo(x, y);

	return true;
}

function checkLMSInitialization()
{
	var studentID = null;

	if (parent.location.search.indexOf('scorm=true') >= 0)
	{
		isLMSInitialized = true;		

		cmiCredit = doLMSGetValue('cmi.core.credit');
		if (doLMSGetLastError() != _NoError)
			cmiCredit = '';

		cmiLessonStatus = doLMSGetValue('cmi.core.lesson_status');
		if (doLMSGetLastError() != _NoError)
			cmiLessonStatus = '';

		cmiLessonMode = doLMSGetValue('cmi.core.lesson_mode');
		if (doLMSGetLastError() != _NoError)
			cmiLessonMode = _CMILessonModeNormal;

		cmiMasteryScore = doLMSGetValue('cmi.student_data.mastery_score');
		if (doLMSGetLastError() != _NoError)
			cmiMasteryScore = '';

		// deliver IO-Applet with LMS student ID
		studentID = doLMSGetValue('cmi.core.student_id');
		if (doLMSGetLastError() == _NoError && studentID && document.io && document.io.isActive())
		{
			// if the SCO will be only browsed provide the IO-Applet with an own student ID
			if (cmiLessonMode == _CMILessonModeBrowse)
				studentID += lmBrowse;

			//document.io.setSCORMUsage(true);
			document.io.setSCORMEnabled();
			document.io.setStudentID(studentID);
		}

		if (cmiLessonStatus == _CMILessonStatusNotAttempted)
			doLMSSetValue('cmi.core.lesson_status', _CMILessonStatusIncomplete);
	}
}

function getStudentName()
{
	if (isLMSInitialized)
	{
		cmiStudentName = doLMSGetValue('cmi.core.student_name');
		if (doLMSGetLastError() != _NoError)
			cmiStudentName = null;
	}
	return cmiStudentName;
}

function getSCOData()
{
	if (parent.login && parent.login.mainnav && parent.login.mainnav.document.mainnav &&
	    parent.login.mainnav.document.mainnav.isActive())
	{
		scoSessionTime = parent.login.mainnav.document.mainnav.getSessionTime();		
		scoTotalScore  = parent.login.mainnav.document.mainnav.getTotalScore();
		scoCompleted   = parent.login.mainnav.document.mainnav.getCompleted(); 
	}
}

function finishLMS()
{
	if (isLMSInitialized && !isLMSFinished)
	{
		isLMSFinished = true;
		getSCOData();
		
		if (scoSessionTime != null && scoTotalScore != null)
		{
			if (cmiLessonMode == _CMILessonModeNormal)
			{
				if (String(scoTotalScore) != '' && !isNaN(scoTotalScore))
					scoTotalScore = parseFloat(scoTotalScore);
				else
					scoTotalScore = -1;

				doLMSSetValue('cmi.core.session_time', scoSessionTime);
				if (scoTotalScore >= 0)
					doLMSSetValue('cmi.core.score.raw', scoTotalScore);

				if (scoCompleted)
				{
					if (cmiCredit == _CMICreditYes && scoTotalScore >= 0 && 
					    String(cmiMasteryScore) != '' && !isNaN(cmiMasteryScore))
					{
						if (parseFloat(scoTotalScore) < parseFloat(cmiMasteryScore))
							doLMSSetValue('cmi.core.lesson_status', _CMILessonStatusFailed);
						else
							doLMSSetValue('cmi.core.lesson_status', _CMILessonStatusPassed);
					}
					else
						doLMSSetValue('cmi.core.lesson_status', _CMILessonStatusCompleted);
				}
				else
					doLMSSetValue('cmi.core.lesson_status', _CMILessonStatusIncomplete);
			}
			else if (cmiLessonMode == _CMILessonModeBrowse && cmiCredit == _CMICreditNo &&
				 cmiLessonStatus != _CMILessonStatusBrowsed)
				doLMSSetValue('cmi.core.lesson_status', _CMILessonStatusBrowsed);
		}
		
		if (scoCompleted) doLMSSetValue('cmi.core.exit', _CMIExitNormal);
		else doLMSSetValue('cmi.core.exit', _CMIExitSuspend);
		doLMSCommit();
		doLMSFinish();
	}
}
//Aenderung fuer VR enthalten
