var swfEmbedId = "lesson";
var swfObjectId = swfEmbedId + "Object";

var swfFileName = "lesson.swf";
var swfName	= "lesson";
var swfWidth	= winWidth;
var swfHeight	= winHeight;
if (isIE) {
	swfWidth	= "100%";
	swfHeight	= "100%";
}
var swfScale	= "showall"; //(empty) | showall | noborder | exactfit

var lastSetValues = [];

function resizeWindow() {
	window.moveTo(0, 0);

	//size it with some padding
	if (isIE) {
		winWidth	+= 14;
		winHeight	+= 61;
		if (isIE7) {
			winHeight	+= 12;
		}
	} else {
		winWidth	+= 8;
		winHeight	+= 56;
	}
	window.resizeTo(winWidth, winHeight);
	
	//center
	winLeft	= (screen.width - winWidth) / 2;
	winTop	= (screen.height - winHeight) / 2;
	window.moveTo(winLeft, winTop);
}
function bodyOnLoad() {
	document.title = titleBar;
	
	//window.setTimeout("resizeWindow()", 5000);

	doLMSInitialize();

	var objSWF = getSWF();
	try {
		objSWF.focus();
	} catch (e) {}
}
function bodyOnUnload() {
	//nothing for now
	//doLMSTerminate();

}
function getSWF() {
	var isInternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
	
	return isInternetExplorer ? document.getElementById(swfObjectId) : document.getElementById(swfEmbedId);
}
function getSCOData(objSWF) {
	var arrElementsToRead		= new Array("core.lesson_status","core.lesson_location","suspend_data");
	var arrElementsToReadFlash	= new Array("core_lesson_status","core_lesson_location","suspend_data");
	for (var item in arrElementsToRead) {
		//alert("getting cmi." + arrElementsToRead[item]);
		objSWF.SetVariable("_root.SCODataTransport." + arrElementsToReadFlash[item], String(doLMSGetValue("cmi." + arrElementsToRead[item])));
	}
	objSWF.TCallFrame("_root.SCODataTransport", 1);
}
function setSCOData(objSWF) {
	//basic elements
	var arrElementsToWrite		= new Array("core.lesson_status","core.lesson_location","core.score.max","core.score.min","core.score.raw","suspend_data");
	var arrElementsToWriteFlash	= new Array("core_lesson_status","core_lesson_location","core_score_max","core_score_min","core_score_raw","suspend_data");
	for (var item in arrElementsToWrite) {
		var cmiName = "cmi." + arrElementsToWrite[item];
		var value = objSWF.GetVariable("_root.SCODataTransport." + arrElementsToWriteFlash[item]);

		if (arrElementsToWrite[item] == "core.lesson_status" && value == "not attempted") {
			value = "incomplete";
		}

		if (hasChanged(item, value)) {
			doLMSSetValue(cmiName, value);
			reportChange(item, value)
		}
		//alert("calling doLMSSetValue: " + cmiName + ", " + value);
	}
	
	doLMSCommit();
}


function hasChanged(item, value) {
	if (!lastSetValues[item]) {
		return true;
	} else if (lastSetValues[item] != value) {
		return true;
	}
	return false;
}
function reportChange(item, value) {
	lastSetValues[item] = value;
}


function setObjective(pstrArgs) {
	if (!API) return;
	
	var arrArgs	= pstrArgs.split("|");
	var id		= arrArgs[0];
	var score   = arrArgs[1];
	var status	= arrArgs[2];
	
	var index = getObjectiveIndexById(id);
	
	//do nothing if objectives aren't supported
	if (index == -1) {
		return;
	}

	var cmiPrefix = "cmi.objectives."+index+".";
	
	doLMSSetValue(cmiPrefix+"id",		 id);
	doLMSSetValue(cmiPrefix+"status",	 status);
	doLMSSetValue(cmiPrefix+"score.raw", score);
	doLMSSetValue(cmiPrefix+"score.min", 100);
	doLMSSetValue(cmiPrefix+"score.max", 100);
}

	var objectivesIdToIndex = []; //associative array of obj[id] = index;

	function getObjectiveIndexById(id) {
		var objectiveCount = 0;
		
		if (objectivesIdToIndex[id] >= 0) {
			return objectivesIdToIndex[id];
		}
		
		objectiveCount = doLMSGetValue("cmi.objectives._count");
		if (doLMSGetLastError() != "0") {
			return -1;
		}
		objectiveCount = Number(objectiveCount);

		if (objectiveCount == 0) {
			objectivesIdToIndex[id] = 0;
			return 0;
		}
		
		for (var x = 0; x < objectiveCount; x++) {
			if (id == doLMSGetValue("cmi.objectives."+x+".id")) {
				objectivesIdToIndex[id] = x;
				return x;
			}
		}
		
		objectivesIdToIndex[id] = objectiveCount;
		return objectiveCount;
	}

var arrInteractionIdIndexLink = new Array();
function setInteraction(pstrArgs) {
	var arrArgs	= pstrArgs.split("|||");
	var strQuestion	= arrArgs[0];
	var strType	= arrArgs[1];
	var strResult	= arrArgs[2];
	var strCorrect	= arrArgs[3];
	var strResponse	= arrArgs[4];

	//escape characters in the identifier
	//spaces
	var strId = strQuestion.split(" ").join("_").substr(0, 255);	//id will be first 255 chars of question, w/ underscores as spaces

	//< and >
	strId = strId.split("<").join("_");
	strId = strId.split(">").join("_");

	//&
	strId = strId.split("&").join("_");

	var intCount = Number(doLMSGetValue("cmi.interactions._count"));
	var intIndex = intCount;

	if (intCount > 0) {
		//see if the id has already been indexed
		for (var id in arrInteractionIdIndexLink) {
			if (id == strId) {
				intIndex = arrInteractionIdIndexLink[id];
			}
		}
	}
	arrInteractionIdIndexLink[strId] = intIndex;
	
	var strPrefix = "cmi.interactions." + intIndex + ".";
	
	doLMSSetValue(strPrefix + "id",					strId);
	doLMSSetValue(strPrefix + "type",				strType);
	doLMSSetValue(strPrefix + "correct_responses.0.pattern",	strCorrect);
	doLMSSetValue(strPrefix + "student_response",			strResponse);
	doLMSSetValue(strPrefix + "result",				strResult);	//"correct" or "wrong"

	//alert(strPrefix + "id " +					strId);
	//alert(strPrefix + "type " +				strType);
	//alert(strPrefix + "correct_responses.0.pattern " +	strCorrect);
	//alert(strPrefix + "student_response " +			strResponse);
	//alert(strPrefix + "result " +				strResult);	//"correct" or "wrong"
	
	doLMSCommit();
}
function exit() {
	if (API == null) {
		closeAfterDelay();
		//window.close();
	} else {

		doLMSTerminate();

		//custom for Lab in a Box - close the window 
		closeAfterDelay();
	}
}
	function closeAfterDelay() {
		window.setTimeout(closeAfterDelayAux, 1);
	}
	function closeAfterDelayAux() {
		window.close();
	}
function showGlossary() {
	var page	= "glossary.html";
	var name	= "glossary";
	var width	= 650;
	var height	= 350;
	
	var left, top, settings, win;
	
	if (screen.width) {
		left	= (screen.width - width)	/ 2;
		top	= (screen.height - height)	/ 2;
	}

	settings = "height=" + height + ",width=" + width + ",top=" + top + ",left=" + left;
	settings += ",scrollbars=yes,toolbar=no,location=no,status=yes,menubar=yes,resizable=yes,dependent=no";
	
	win = window.open(page, name, settings)
}

function lesson_DoFSCommand(command, args) {
	var objSWF = getSWF();
	
	switch (command) {
		case "getSCOData":	getSCOData(objSWF);	break;
		case "setSCOData":	setSCOData(objSWF);	break;
		case "setInteraction":	setInteraction(args);	break;
		case "setObjective":	setObjective(args);	break;
		case "exit": 		exit();			break;
		case "showGlossary":	showGlossary();		break;
		case "trace": 		alert(args);		break;
	}
}
if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
	document.write('<script language=\"VBScript\"\>\n');
	document.write('On Error Resume Next\n');
	document.write('Sub lesson_FSCommand(ByVal command, ByVal args)\n');
	document.write('	Call lesson_DoFSCommand(command, args)\n');
	document.write('End Sub\n');
	document.write('</script\>\n');
}