/*
 * Copyright (C) 1995-2005 by 4system Polska Sp. z o.o.
 * All Rights Reserved. v4
 */

var isWBTE = false; //pg2

function doLoad() 
{
	detect.check('online');

	if (!lms) return;
	
	var v = lms.LMSGetValue("cmi.core.lesson_location");
	var gix = (v != null && v != "") ? v : player.getStartPage();
	if (uelo) uelo.showUserAnswers = false;
	if (lms.LMSGetValue) uelo.username = lms.LMSGetValue("cmi.core.student_name");
	var d = lms.LMSGetValue("cmi.suspend_data");
	if (d) {
		d = d.split("|");
		if (d) {
			if (d.length >= 2) {
				struct.setColFromString("status", d[0]);
				suspendData.setFromString(d[1]);
				if (uelo){
					uelo.updateUELOFromSD(d[0], d[1], d[2], d[3]);
					uelo.courseOryginalStatus = lms.LMSGetValue("cmi.core.lesson_status");
					uelo.courseOryginalScore = lms.getScore();
				}
			}
		}
	}
	if (is.moz && player.enablePagesPreloader && !player.wasLoaded) {
		player.wasLoaded = true;
		player._doLoad();
	}
	//player.gotoPage(gix);
	player.restoreExitPage(gix);
	InitializeStandardTree();
	
	lms.LMSSetValue("cmi.core.lesson_status", lms.STATUS_INCOMPLETE);
};

function doUnload() 
{
	try { //pg2
		if ((typeof(top.isWBTServer) != 'undefined')) {
			isWBTE = true;
		}
	} catch(e){}
	
	player.contentDestroy();
	if (lms) {
		// jezeli przy wyjsciu masz wcisniety lewy alt to kasowane sa dane		
		if (is.ie55UP && window.event && window.event.altLeft) {
			lms.LMSSetValue("cmi.core.lesson_location", "");
			lms.LMSSetValue("cmi.suspend_data", "");
			lms.LMSSetValue("cmi.core.lesson_status", lms.STATUS_NOT_ATTEMPTED);
			lms.setScore(0);
		} else {
			if(config.sendLessonLocationToServer) lms.LMSSetValue("cmi.core.lesson_location", player.gix);	
			if (uelo) var tryNumbers = uelo.getExerciseTryNumber();			
			lms.LMSSetValue("cmi.suspend_data", struct.getColToString("status") + "|" + 
												suspendData.getToString() + "|" + 
												tryNumbers + "|" + 
												uelo.getInteractionsStatus());
			if((config.dontSendStatusOnPassedCourse) && (uelo.courseOryginalStatus == "passed")){
				lms.LMSSetValue("cmi.core.lesson_status", player.STATUS_PASSED);
				lms.LMSSetValue("cmi.core.exit", "logout");
			}else{
				var status = player.getLessonStatus();
				if (status == player.STATUS_INCOMPLETE) {
					lms.LMSSetValue("cmi.core.exit", "suspend");
				} else {
					lms.LMSSetValue("cmi.core.exit", "logout");
				}
				lms.LMSSetValue("cmi.core.lesson_status", player.getLessonStatus());
				if(config.sendScoreToServer) lms.setScore(player.getTotalScore());
				SendInteractions();
			}
		}
		lms.free();
		lms = null;
		delete lms;
	}	
};

function SendInteractions()
{
	var interactions = uelo.getInteractions("SCORM12");
	for (var k in interactions) {
		var item = interactions[k];
		
		lms.LMSSetValue("cmi.interactions." + item["number"] + ".id", item["id"]);
		lms.LMSSetValue("cmi.interactions." + item["number"] + ".time", item["time"]);
		lms.LMSSetValue("cmi.interactions." + item["number"] + ".type", item["type"]);
		lms.LMSSetValue("cmi.interactions." + item["number"] + ".latency", item["latency"]);
		lms.LMSSetValue("cmi.interactions." + item["number"] + ".result", item["result"]);
		lms.LMSSetValue("cmi.interactions." + item["number"] + ".weighting", item["weighting"]);
		lms.LMSSetValue("cmi.interactions." + item["number"] + ".student_response", item["student_response"]);
		
		if (isWBTE) { //pg2-wysylanie tylko w przypadku WBTS/EIS
			lms.LMSSetValue("wbts.interactions." + item["number"] + ".question", item["question"]); //pg2- zmiana od MK (wysylanie tresc pytania)
		}
		
		var correctResponses = item["correct_responses"];
		for (var j = 0; j < correctResponses.length; j++) {
			lms.LMSSetValue("cmi.interactions." + item["number"] + ".correct_responses."
				+ j + ".pattern", correctResponses[j]);
		}
	}
};

