/////////////////////////////////////////////////////////////////////////////
//
//  Functions for interacting with the Falsh movie
//
/////////////////////////////////////////////////////////////////////////////

var movieName = "CarneyContentShell";

function getMovieObject(myMovieName) {
	//  Returns a reference to the internally linked Flash movie.
	if (document.all) {
		return window[myMovieName];
	} else {
		return document[myMovieName];
	}
}

function myGetVar(name) {
	//  A useful way to peer inside the Flash files
	var myMovie = getMovieObject(movieName);
	return myMovie.GetVariable(name);
}

//
//REVISIT
//
//  WHY DOESN'T THIS FUNCTION USE getMovieObject() TO GET A REFERENCE TO THE MOVIE?
//
function loadJSParams() {
	var mov =  InternetExplorer ? CarneyContentShell : document.CarneyContentShell;

	mov.SetVariable("_level0.thisAUJS",            thisAU);
	mov.SetVariable("_level0.linksJS",             links);
	mov.SetVariable("_level0.useEmbeddedShowText", useEmbeddedShowText);
	mov.SetVariable("_level0.debugMode",           debugMode);
	mov.SetVariable("_level0.debugBehaviorFlag",   debugBehavior);
	mov.SetVariable("_level0.reviewerMode",        reviewerMode);
	mov.SetVariable("_level0.showTextEditMode",    showTextEditMode);
	mov.SetVariable("_level0.accessibilityMode",   accessibilityMode);
	mov.SetVariable("_level0.debug508",            debug508);
	mov.SetVariable("_level0.parameterMode",       parameterMode);
	mov.SetVariable("_level0.project",             project);
	mov.SetVariable("_level0.pkg",                 pkg);
	mov.SetVariable("_level0.launchPageURL",       location.href);
	mov.SetVariable("_level0.host",                location.host);
	mov.SetVariable("_level0.scormEntry",          scormEntry);
	mov.SetVariable("_level0.scormMode",           mode);
	mov.SetVariable("_level0.showtxtDfltOpen",     showtxtDfltOpen);

	for (var paramName in urlParams) {
		mov.SetVariable("_level0.urlParams." + paramName,   urlParams[paramName]);
	}

	//
	//  Set a flag on the movie that tells it we have
	//  finished sending it parameter values.
	//
	mov.SetVariable("_level0.isReady", 1);
}


//
//  This function is invoked by the Flash movie whenever
//  the fscommand() function is executed in Flash action script.
//
function CarneyContentShell_DoFSCommand(command, args) {
	//
	//  Tell _level0 of the movie the command that we received.
	//
	var myMovie = getMovieObject(movieName);
	myMovie.SetVariable("_level0.lastFSCommand", command);

	if (command == "debug") {
		alert(args);
	}
	else if (command == "ExitModule") {
		exitModule(args);
	}
	else if (command == "CloseWindow") {
		closeWindow(args);
	}
	else if (command == "LoadStoredParameters") {
		loadStoredParameters(args);
	}
	else if (command == "StoreAUParameters") {
		storeParameters(args);
	}
	else if (command == "StoreLinkParameters") {
		storeParameters(args);
	}
	else if (command == "StoreParameters") {
		storeParameters(args);
	}
	else if (command == "ChangeShowTextStatus") {
		changeShowTextStatus(args);
	}
	else if (command == "SetShowTextTxt") {
		setShowTextTxt(args);
	}
	else if (command == "MarkModuleComplete") {
		//markModuleComplete();
		setSCOCompletionStatus(args);
	}

	else if (command == "SetSCORMObjNotAttempted") {
		setObjCompletionStatus(args, "not attempted");
	}
	else if (command == "SetSCORMObjIncomplete") {
		setObjCompletionStatus(args, "incomplete");
	}
	else if (command == "SetSCORMObjCompleted") {
		setObjCompletionStatus(args, "completed");
	}

	else if (command == "SetSCORMObjPassed") {
		setObjSuccessStatus(args, "passed");
	}
	else if (command == "SetSCORMObjFailed") {
		setObjSuccessStatus(args, "failed");
	}

	else if (command == "SetSCORMObjRelativeScore") {
		setSCORMObjRelativeScore(args);
	}
	else if (command == "GetSCOCompletionStatus") {
		getSCOCompletionStatusFlash(args);
	}
	else if (command == "GetObjectiveSuccessStatus") {
		getObjSuccessStatusFlash(args);
	}
	else if (command == "GetObjectiveCompletionStatus") {
		getObjCompletionStatusFlash(args);
	}

	else if (command == "SetSCOCompletionStatus") {
		setSCOCompletionStatus(args);
	}
	else if (command == "SetSCOSuccessStatus") {
		setSCOSuccessStatus(args);
	}

	else if (command == "ExitSCO") {
		if (parameterMode == "LMS") {
			var idx = args.indexOf("|");

			var navRequest = args.substring(0, idx);
			var exitParam  = args.substring(idx + 1);

			exitSCO(navRequest, exitParam);
		}
	}
	else if (command == "ConfirmExit") {
		confirmExit();
	}
	else if (command == "Abort") {
		alert(args);
		window.close();
	}
	else if (command == "eval") {
		eval(args);
	}
	else if (command == "switchToTxtOnly") {
		switchToTxtOnly();
	}

	else {
		var fserr = "FSCommand not recognized. (" + command + ", " + args + ")"
		if (debugMode == 1) {
			alert(fserr);
		}
	}
}

function setSCORMObjRelativeScore(data) {
	var idx = data.indexOf('|');
	if (idx != -1) {
		var objID  = data.substring(0, idx);
		var score  = data.substring(idx + 1);
		setObjScoreScaled(objID, score);
	}
	else {
		alert("INVALID DATAIN setSCORMObjRelativeScore: " + data);
	}
}

function confirmExit() {
	var mov        =  InternetExplorer ? CarneyContentShell : document.CarneyContentShell;
	var answer     = confirm("Are you sure you want to exit?");
	var answerFlag = (answer) ? "confirmed" : "cancelled";
	mov.SetVariable("_level0.exitConfirmationStatus", answerFlag);
}

function jumpToPage(pgNbr) {
	var mov        =  InternetExplorer ? CarneyContentShell : document.CarneyContentShell;
	mov.SetVariable("_level0.jumpToPageIdx", pgNbr);
}

function loadStoredParameters(args) {
	var parameterData = getStoredParameters();
	parseAndSetParameters(parameterData, "_level0.params.");

	var mov =  InternetExplorer ? CarneyContentShell : document.CarneyContentShell;
	mov.SetVariable("_level0.paramsLoaded",  1);
}


function parseAndSetParameters(paramString, tgtObj) {
	var myMovie = getMovieObject(movieName);

	if (paramString != false) {
		var returnArraySize = customSplit(paramString, "&", "parameter");
		for (var i = 0; i < returnArraySize; i++) {
			var size = customSplit(parameter[i], "=", "pair");
			myMovie.SetVariable(tgtObj + pair[0], pair[1]);
		}
	}
}

