var g_Custom_lastMainPathStep = null;
TTonload = onload;
onload = function(e) {
	TTonload(e);

	// Control Buttons
	var sImgFolder = TTAPI.objCBTControl.sImgFolder,
		objElement;

	// Continue (Pause Toggle) Button
	objElement = document.getElementById("ttweBtnContinue");
	TTAPI.objCBTControl.SetControl("ttweBtnContinue", objElement);
	g_objCrossBrowserModel.AddEventListener(objElement, "mouseup", customOnControlPauseMouseup);
	g_objCrossBrowserModel.LoadAlphaImage(objElement, sImgFolder + "ttweBtnContinue_N.png");

	// Exit Button
	objElement = document.getElementById("ttweBtnExit");
	TTAPI.objCBTControl.SetControl("ttweBtnExit", objElement);
	g_objCrossBrowserModel.AddEventListener(objElement, "mouseup", customOnControlExitMouseup);
	g_objCrossBrowserModel.LoadAlphaImage(objElement, sImgFolder + "ttweBtnExit_N.png");
	
	/* SetAssessmentWithSolutionExtension
	 * @param {Function} fnBeforeSolution is called, if the navigation should provide control buttons to get to the question solution 
	 * @param {Function} fnBeforeTestTaskCallback is called, if the navigation should provide control buttons to go to the next question
	 * @param {Function} fnBeforeAssesmentResultCallback is called, if the navigation should provide control buttons to go to assessment result
	 */
	TTAPI.objCBTControl.SetAssessmentWithSolutionExtension(	null, null, function(){
		TTAPI.objCBTControl.ShowControl("ttweBtnEval", false);
		TTAPI.objCBTControl.ShowControl("ttweBtnTestSummary", false);
		TTAPI.objCBTControl.ShowControl("ttweBtnNext", true);
		TTAPI.objCBTControl.DisableButton(false, "ttweBtnNext");
	});

	TTAPI.AddListener(new customCBTControl());
};

function customCBTControl() {
	this.id = "id_customCBTControl";

	this.acknowledges = {};
	this.acknowledges[TTEventType["OnAfterStartSequence"]] = true;
	this.acknowledges[TTEventType["RunAnimations"]] = true;
	this.acknowledges[TTEventType["AnimationsBeforeFinished"]] = true;
	this.acknowledges[TTEventType["StopAllSounds"]] = true;
	this.acknowledges[TTEventType["SoundModeActivated"]] = true;
	this.acknowledges[TTEventType["TextModeActivated"]] = true;
	this.acknowledges[TTEventType["ContinueAllAnimations"]] = true;
	this.acknowledges[TTEventType["PauseAllAnimations"]] = true;
	this.acknowledges[TTEventType["OnRightSolution"]] = true;
	this.acknowledges[TTEventType["OnUserSolution"]] = true;
	this.acknowledges[TTEventType["DisplaySolution"]] = true;
	this.acknowledges[TTEventType["RunDisplaySolutionAnimations"]] = true;
	this.acknowledges[TTEventType["OnAfterStartSequence2"]] = true;
	this.acknowledges[TTEventType["InitVideos"]] = true;
	this.acknowledges[TTEventType["ControlOpened"]] = true;
	this.acknowledges[TTEventType["ControlClosed"]] = true;
	this.acknowledges[TTEventType["OnBeforeStartSequence"]] = true;
	this.acknowledges[TTEventType["FilmModeActivated"]] = true;
	this.acknowledges[TTEventType["NextFeedback"]] = true;
}

customCBTControl.prototype = Object.create(Listener.prototype);
customCBTControl.prototype.solvedAnimationsStarted = false;
customCBTControl.prototype.nextStepIsAssessmentResult = function() {

	var sNextStepID = TTAPI.GetNextStep(),
		sNextAssessmentResultID = TTAPI.objSequenceControl.GetFirstStepByCategory("AssessmentResult");

	return TTAPI.GetNextAvailableStep(sNextStepID) === sNextAssessmentResultID;
};
customCBTControl.prototype.On = function(sEventID) {
	var objEvent = Listener.prototype.On.call(this, sEventID),
		nWBTMode = parseInt(TTAPI.GetProperty("wbtmode"), 2);

	if(objEvent === null) {
		return;
	}

	switch(objEvent.type) {
		case TTEventType["OnAfterStartSequence"]:
			TTAPI.objCBTControl.ShowControl("ttweBtnContinue", false);
			var isQuestionStep = TTAPI.objSequenceControl.objTest["TestRuns"] > 0,
				state,
				isSolution;

			if(TTAPI.objSequenceControl.objTest && isQuestionStep) {
				switch(nWBTMode) {
					// im Lernmodus:
					case WBTMode["Learning"]["value"]:
						// fixed: #8120
						g_objCrossBrowserModel.RemoveEventListener(TTAPI.objCBTControl.objControls["ttweBtnNext"], "mouseup", TTAPI.NextTestTask);
						g_objCrossBrowserModel.RemoveEventListener(TTAPI.objCBTControl.objControls["ttweBtnNext"], "mouseup", TTAPI.Solution);
						TTAPI.objCBTControl.ShowControl("ttweBtnNext", false);
						TTAPI.objCBTControl.ShowControl("ttweBtnEval", true);
						TTAPI.objCBTControl.DisableButton(false,"ttweBtnEval");
						break;
					// im Assessmentmodus:
					case WBTMode["Assessment"]["value"]:
						g_objCrossBrowserModel.RemoveEventListener(TTAPI.objCBTControl.objControls["ttweBtnNext"], "mouseup", TTAPI.Solution);
						TTAPI.objCBTControl.DisableButton(false,"ttweBtnEval");
						TTAPI.objCBTControl.ShowControl("ttweBtnEval", false);
						TTAPI.objCBTControl.ShowControl("ttweBtnTestSummary", false);
						g_objCrossBrowserModel.AddEventListener(TTAPI.objCBTControl.objControls["ttweBtnNext"], "mouseup", TTAPI.EvaluateAndExitTestTask);
						break;
				}
			}
			break;
		case TTEventType["OnRightSolution"]:
			TTAPI.objCBTControl.ShowControl("ttweBtnPause", false);
			TTAPI.objCBTControl.ShowControl("ttweBtnContinue", false);

			TTAPI.objCBTControl.ShowControl("ttweBtnSkip", false);
			TTAPI.objCBTControl.DisableButton(true, "ttweBtnEval");

			TTAPI.objCBTControl.ShowControl("ttweBtnNext", true);

			// fixed: #8120
			g_objCrossBrowserModel.RemoveEventListener(TTAPI.objCBTControl.objControls["ttweBtnNext"], "mouseup", TTAPI.Next);
			g_objCrossBrowserModel.AddEventListener(TTAPI.objCBTControl.objControls["ttweBtnNext"], "mouseup", TTAPI.NextTestTask);
			if(nWBTMode == WBTMode["Assessment"]["value"]) {
				TTAPI.objCBTControl.ShowControl("ttweBtnRightSolution", false);
				TTAPI.objCBTControl.ShowControl("ttweBtnUserSolution", true);
				TTAPI.objCBTControl.DisableButton(false, "ttweBtnUserSolution");
				g_objCrossBrowserModel.RemoveAllEventListeners(TTAPI.objCBTControl.objControls["ttweBtnUserSolution"]);
				g_objCrossBrowserModel.AddEventListener(TTAPI.objCBTControl.objControls["ttweBtnUserSolution"], "mouseup", TTAPI.UserSolution);

				TTAPI.objCBTControl.ShowControl("ttweBtnTestSummary", false);
				
				switch(TTAPI.objSequenceControl.objTestMode["value"]) {
					default:
					case TestMode["FreeBrowsing"]["value"]:
					case TestMode["DrillAndPractice"]["value"]:
						break;
					case TestMode["DrillAndPracticeWithMultipleTries"]["value"]:
					case TestMode["DrillAndPracticeWithSolution"]["value"]:
						if( this.nextStepIsAssessmentResult() ) {
							TTAPI.objCBTControl.ShowControl("ttweBtnEval", false);
						}
						TTAPI.objCBTControl.DisableButton(false, "ttweBtnNext");
						break;
				}
			}
			break;
		case TTEventType["OnUserSolution"]:
			TTAPI.objCBTControl.ShowControl("ttweBtnPause", false);
			TTAPI.objCBTControl.ShowControl("ttweBtnContinue", false);

			TTAPI.objCBTControl.ShowControl("ttweBtnSkip", false);
			TTAPI.objCBTControl.DisableButton(true, "ttweBtnEval");

			TTAPI.objCBTControl.ShowControl("ttweBtnNext", true);

			// fixed: #8120
			g_objCrossBrowserModel.RemoveEventListener(TTAPI.objCBTControl.objControls["ttweBtnNext"], "mouseup", TTAPI.Next);
			g_objCrossBrowserModel.AddEventListener(TTAPI.objCBTControl.objControls["ttweBtnNext"], "mouseup", TTAPI.NextTestTask);

			if(nWBTMode == WBTMode["Assessment"]["value"]) {
				TTAPI.objCBTControl.ShowControl("ttweBtnEval", false);
				TTAPI.objCBTControl.ShowControl("ttweBtnUserSolution", false);
				TTAPI.objCBTControl.ShowControl("ttweBtnRightSolution", true);
				TTAPI.objCBTControl.DisableButton(false, "ttweBtnRightSolution");
				g_objCrossBrowserModel.RemoveAllEventListeners(TTAPI.objCBTControl.objControls["ttweBtnRightSolution"]);
				g_objCrossBrowserModel.AddEventListener(TTAPI.objCBTControl.objControls["ttweBtnRightSolution"], "mouseup", TTAPI.RightSolution);

				TTAPI.objCBTControl.ShowControl("ttweBtnTestSummary", false);
				
				switch(TTAPI.objSequenceControl.objTestMode["value"]) {
					default:
					case TestMode["FreeBrowsing"]["value"]:
					case TestMode["DrillAndPractice"]["value"]:
						break;
					case TestMode["DrillAndPracticeWithMultipleTries"]["value"]:
					case TestMode["DrillAndPracticeWithSolution"]["value"]:
						if( this.nextStepIsAssessmentResult() ) {
							TTAPI.objCBTControl.ShowControl("ttweBtnEval", false);
						}
						TTAPI.objCBTControl.DisableButton(false, "ttweBtnNext");
						break;
				}
			}
			break;
		case TTEventType["DisplaySolution"]:
			switch(nWBTMode) {
				// im Lernmodus:
				case WBTMode["Learning"]["value"]:
					break;

				// im Assessmentmodus:
				case WBTMode["Assessment"]["value"]:
					this.toggleAssessmentControls();

					switch(TTAPI.objSequenceControl.objTestMode["value"]) {
						default:
						case TestMode["FreeBrowsing"]["value"]:
						case TestMode["DrillAndPractice"]["value"]:
							TTAPI.objCBTControl.ShowControl("ttweBtnJumpOver", true);
							break;
						case TestMode["DrillAndPracticeWithMultipleTries"]["value"]:
						case TestMode["DrillAndPracticeWithSolution"]["value"]:
							break;
					}

					TTAPI.objCBTControl.ShowControl("ttweBtnTestSummary", false);
					TTAPI.objCBTControl.ShowControl("ttweBtnEval", false);
					TTAPI.objCBTControl.ShowControl("ttweBtnNext", false);
					break;
			}
			break;
		case TTEventType["PauseAllAnimations"]:
			state = TTAPI.objSequenceControl.GetState();
			isSolution = state == SequenceState["TestRightSolution"] || state == SequenceState["TestUserSolution"];
			if (isSolution) {
				return;
			}

			if(TTAPI.objCBTControl.objControls["ttweBtnPause"]) {
				TTAPI.objCBTControl.ShowControl("ttweBtnPause", false);
			}
			if(TTAPI.objCBTControl.objControls["ttweBtnContinue"]) {
				TTAPI.objCBTControl.ShowControl("ttweBtnContinue", true);
			}
			break;
		case TTEventType["FilmModeActivated"]:
		case TTEventType["ContinueAllAnimations"]:
			state = TTAPI.objSequenceControl.GetState();
			isSolution = state == SequenceState["TestRightSolution"] || state == SequenceState["TestUserSolution"];
			if (isSolution) {
				return;
			}

			if(TTAPI.objCBTControl.objControls["ttweBtnPause"]) {
				TTAPI.objCBTControl.ShowControl("ttweBtnPause", true);
			}
			if(TTAPI.objCBTControl.objControls["ttweBtnContinue"]) {
				TTAPI.objCBTControl.ShowControl("ttweBtnContinue", false);
			}
			break;
		case TTEventType["ControlClosed"]:
			break;
		case TTEventType["OnAfterStartSequence2"]:
		case TTEventType["InitVideos"]:
			switch(nWBTMode) {
				// im Lernmodus:
				case WBTMode["Learning"]["value"]:
					TTAPI.objCBTControl.ShowControl("ttweBtnSkip", false);
					TTAPI.objCBTControl.ShowControl("ttweBtnJumpOver", false);
					break;

				// im Assessmentmodus:
				case WBTMode["Assessment"]["value"]:
					TTAPI.objCBTControl.ShowControl("ttweBtnJumpOver", false);
					this.toggleAssessmentControls();
					break;
			}
			break;
		case TTEventType["OnBeforeStartSequence"]:
			TTAPI.OnAfterStartSequence();
			this.toggleAssessmentControls();
			break;
		case TTEventType["StopAllSounds"]:
		case TTEventType["SoundModeActivated"]:
		case TTEventType["TextModeActivated"]:
			var soundMode = TTAPI.objCBTControl.bSoundMode;
			TTAPI.objCBTControl.ShowControl("ttweItemSoundOn", soundMode);
			TTAPI.objCBTControl.ShowControl("ttweItemSoundOff", !soundMode);
			break;
	}
};

customCBTControl.prototype.toggleAssessmentControls = function() {
	var	objCBTCtrl = TTAPI.objCBTControl,
		sNextStepID = TTAPI.GetNextStep(),
		sNextAssessmentResultID = TTAPI.objSequenceControl.GetFirstStepByCategory("AssessmentResult"),
		bFriendly = TTAPI.GetProperty("friendlyassessmentexit") == "true",
		sEditMode = TTAPI.GetProperty("editmode"),
		isQuestionStep = TTAPI.objSequenceControl.objTest["TestRuns"] > 0;

	if(sEditMode == "wysiwyg") {
		objCBTCtrl.ShowControl("ttweBtnTestSummary", false);
		objCBTCtrl.ShowControl("ttweBtnEval", false);
		objCBTCtrl.ShowControl("ttweBtnNext", true);
		objCBTCtrl.ShowControl("ttweBtnSkip", false);
		objCBTCtrl.ShowControl("ttweBtnJumpOver", false);
		return;
	}

	objCBTCtrl.ShowControl("ttweBtnNext", false);

	if(!bFriendly) {
		return;
	}

	if(sNextAssessmentResultID) {
		if(sNextStepID) {
			var sNextAvailStepID = TTAPI.GetNextAvailableStep(sNextStepID);
			
			var objSeqControl = TTAPI.objSequenceControl;
			switch(objSeqControl.objTestMode["value"])
			{
				default:
		        case TestMode["DrillAndPractice"]["value"]:
		        case TestMode["Pool"]["value"]:
					// Nächster Schritt ist Auswertungsseite
					if( sNextAvailStepID == sNextAssessmentResultID ) {
						objCBTCtrl.ShowControl( "ttweBtnJumpOver", false);
						objCBTCtrl.ShowControl( "ttweBtnEval", false );
						objCBTCtrl.ShowControl("ttweBtnTestSummary", false);
						objCBTCtrl.ShowControl("ttweBtnNext" , true);
						TTAPI.objCBTControl.DisableButton(false, "ttweBtnNext");
					}
					// Nächster Schritt ist KEINE Auswertungsseite
					else {
						objCBTCtrl.ShowControl( "ttweBtnEval", true );
						objCBTCtrl.ShowControl( "ttweBtnTestSummary", false );
					}
					break;
		        case TestMode["FreeBrowsing"]["value"]:
		            // Nächster Schritt ist Auswertungsseite
		            if( sNextAvailStepID == sNextAssessmentResultID ) {
		                objCBTCtrl.ShowControl( "ttweBtnJumpOver", false);
		                objCBTCtrl.ShowControl( "ttweBtnEval", false );
		                objCBTCtrl.ShowControl( "ttweBtnTestSummary", true );
		                objCBTCtrl.DisableButton(false,"ttweBtnTestSummary");
		            }
		            // Nächster Schritt ist KEINE Auswertungsseite
		            else {
		                objCBTCtrl.ShowControl( "ttweBtnEval", true );
		                objCBTCtrl.ShowControl( "ttweBtnTestSummary", false );
		            }
		            break;
				case TestMode["DrillAndPracticeWithSolution"]["value"]:
				case TestMode["DrillAndPracticeWithMultipleTries"]["value"]:
					objCBTCtrl.ShowControl("ttweBtnJumpOver", false);
					objCBTCtrl.ShowControl("ttweBtnTestSummary", false);
					if( sNextAvailStepID == sNextAssessmentResultID && !isQuestionStep ) {
						objCBTCtrl.ShowControl("ttweBtnEval", false);
						objCBTCtrl.ShowControl("ttweBtnNext" , true);
						objCBTCtrl.DisableButton(false, "ttweBtnNext");
					}
					else {
						objCBTCtrl.ShowControl("ttweBtnEval", true);
					}
					break;
			}
		}
		// Aktueller Schritt ist Auswertungsseite ODER letzter Schritt
		if(TTAPI.objSequenceControl.sActiveStepID == sNextAssessmentResultID || !sNextAvailStepID){
			objCBTCtrl.ShowControl("ttweBtnTestSummary", false);
			objCBTCtrl.ShowControl("ttweBtnEval" , false);
			objCBTCtrl.ShowControl("ttweBtnNext" , true);
		}
	}
	else {
		// Es existiert kein Auswertungsschritt im Dokument
		objCBTCtrl.ShowControl("ttweBtnTestSummary", false);
		objCBTCtrl.ShowControl("ttweBtnEval" , true);
	}
};

function customOnControlPauseMouseup(e) {
	TTAPI.objCBTControl.OnPauseButtonPressed(e);
}

function customOnControlExitMouseup(e) {
	TTAPI.objMFControl.LoadModule("close");
	TTAPI.objMFControl.OpenModule();
}