"use strict";

////
// THIS FILE WILL NOT BE OBFUSCATED!
//
var bCreated = false,
	TTAPI,
	g_objCrossBrowserModel;

window.onresize = function () {
	CreateStepList(!bCreated);
	UpdateStepSelect();
};

window.onload = function () {
	try {
		// Attention! parent.TTAPI can be null but is of type "object"!
		TTAPI = parent && typeof parent.TTAPI === "object" && parent.TTAPI ? parent.TTAPI : {
			GetProperty: function () {
				return "";
			},
			GoToStep: function () {

			},
			objCBTControl: {
				OnConsoleButtonPressed: function () {
					try {
						window.close();
					}
					catch (e) {
						// Intentionally left blank
					}
				}
			},
			objSequenceControl: {
				objSequence: {},
				sActiveStepID: ""
			}
		};
		if (g_objCrossBrowserModel) {
			g_objCrossBrowserModel.AddEventListener(document, "keydown", OnConsoleKeydown);
		}
		InitConsole();
	} catch (objError) {
		// Intentionally left blank
	}
};

window.onunload = function () {
	try {
		if (g_objCrossBrowserModel) {
			g_objCrossBrowserModel.RemoveEventListener(document, "keydown", OnConsoleKeydown);
		}
	} catch (objError) {
		// Intentionally left blank
	}
};

/**
 * @private TODO: Hide from global scope.
 */
function OnConsoleKeydown(e) {
	if (e.keyCode === 86 && e.ctrlKey && e.shiftKey) { // Ctrl + Shift + v
		TTAPI.objCBTControl.OnConsoleButtonPressed();
	}
}

/**
 * @public
 */
function RefreshList() {
	CreateStepList(true);
	UpdateStepSelect();
}

/**
 * @public
 */
function ClearOutput() {
	var objOutput = document.getElementById("output");
	if (objOutput) {
		objOutput.innerHTML = "";
	}
}

/**
 * @public
 */
function InitConsole() {
	var header = document.getElementById("header"),
		browserName = g_objBrowser["name"],
		userAgent = g_objBrowser["userAgent"],
		isEdge = g_objBrowser["isEdge"],
		isIE = g_objBrowser["isIE"],
		isOpera = g_objBrowser["isOpera"],
		isAppleWebKit = !isEdge && g_objBrowser["isAppleWebKit"],
		onIPad = g_objBrowser["isiPad"],
		ieVersion = g_objBrowser["ieBrowserVersion"],
		ieTridentVersion = g_objBrowser["ieTridentVersion"],
		ie64Bit = g_objBrowser["ie64bit"],
		ie10OrHigher = false,
		ie9 = false,
		isChrome = !isEdge && g_objBrowser["isChrome"];

	// IE > 6 with MSHTML > 4 ? (is IE 9 or higher):
	if (ieVersion >= 7.0 && ieTridentVersion >= 5.0) {
		ie9 = true;
	}
	// IE > 6 with MSHTML > 5 ? (is like IE 10):
	if (ieVersion >= 7.0 && ieTridentVersion >= 6.0) {
		ie10OrHigher = true;
		ie9 = false;
	}

	var textHeader = "TT KF player console started at " +
		(new Date()).toUTCString() +
		"<br/>" +
		"player version: " + g_objVersion.ttkf +
		"<br/>" + "<br/>" +
		"++++++ User agent ++++++" +
		"<br/>" +
		"browser : " + (isChrome ? "chrome" : (isAppleWebKit ? "safari" : (isOpera ? "opera" : (isEdge ? "edge" : browserName)))) +
		"<br/>" +
		"user agent string : " + userAgent +
		"<br/>" + (isIE ?
			"IE document mode : " + ieVersion +
			"<br/>" +
			(
				ie64Bit ?
					"Your're running 64 bit version of IE" + "<br/>"
					:
					""
			) +
			(
				ie10OrHigher ?
					"You're running IE 10 or higher" + "<br/>"
					:
					(
						ie9 ?
							"You're running IE 9 or higher" + "<br/>"
							:
							""
					)
			)
			:
			""
		) +
		(onIPad ? "You're using an iPad." + "<br/>" : "") +
		"++++++++++++++++++++++++" +
		"<br/>";

	header.insertAdjacentHTML("AfterBegin", textHeader);
}

/**
 * @private TODO: Hide from global scope.
 */
function CreateStepList(bNew) {
	var sWBTMode,
		objSelList,
		objSeq,
		objOption,
		sLastPathID = "",
		nCount = 0,
		i,
		sPathId;
	if (!bNew) {
		return;
	}
	// @type {HTMLSelectElement} objSelList
	objSelList = document.getElementById("ttweStepSelect");
	if (objSelList) {
		try {
			while (objSelList.options.length > 0) {
				objSelList.remove(0);
			}
			sWBTMode = TTAPI.GetProperty("wbtmode");
			switch (parseInt(sWBTMode, 2)) {
				case WBTMode["Learning"]["value"] :
					break;
				case WBTMode["Assessment"]["value"] :
					if (TTAPI.editMode.isProduct) {
						objSelList.disabled = true;
					}
					break;
				default:
			}
			objSeq = TTAPI.objSequenceControl.objSequence;
			for (i in objSeq) {
				if (!objSeq.hasOwnProperty(i)) {
					continue;
				}

				sPathId = objSeq[i]["Path"];
				if (sLastPathID !== sPathId) {
					sLastPathID = sPathId;
					nCount = 0;
				}

				nCount++;
				objOption = document.createElement("option");
				objOption.text = nCount.toString() + ". " + objSeq[i]["Title"] + " (" + sPathId + "/" + i + ")";
				objOption.value = i;	// Step-ID
				g_objCrossBrowserModel.AddOptionInSelect(objOption, objSelList);
				// release memory in IE
				objOption = null;
			}
			g_objCrossBrowserModel.AddEventListener(objSelList, "change", OnControlSelectChanged);
		} catch (objError) {
		}
	}
	bCreated = true;
}

/**
 * @private TODO: Hide from global scope.
 */
function UpdateStepSelect() {
	try {
		// Aktiven Step in Controlleiste aktualisieren:
		var objStepSelect = document.getElementById("ttweStepSelect");
		if (typeof(objStepSelect) !== "undefined" && TTAPI && TTAPI.objSequenceControl) {
			objStepSelect.value = TTAPI.objSequenceControl.sActiveStepID;
		}
	} catch (objError) {
		// Intentionally left blank
	}
}

/**
 * @private TODO: Hide from global scope.
 */
function OnControlSelectChanged(e) {
	var objSelectList = g_objCrossBrowserModel.EventSource(e);
	if (objSelectList) {
		if(TTAPI.editMode.isWysiwyg) {
			return;
		}
		if(TTAPI.editMode.isAnyPreview) {
			TTKFGlobals.SendMessage({
				"type": "TTN_Request",
				"sender": "TTN_Editor",
				"message": "GoToStep",
				"stepID": objSelectList.options[objSelectList.selectedIndex].value
			});
		} else {
			TTAPI.GoToStep(objSelectList.options[objSelectList.selectedIndex].value);
		}
	}
}

/**
 * @public
 */
function CloseConsole() {
	if (!TTAPI || !TTAPI.objCBTControl || typeof TTAPI.objCBTControl.OnConsoleButtonPressed !== "function") {
		return;
	}
	TTAPI.objCBTControl.OnConsoleButtonPressed();
}

function detachConsole() {
	var win = g_objCrossBrowserModel.GetFrameFromWindow(top, "start");
	if(!win) {
		return;
	}
	win.SetConsoleHeight(0, false);
	win.SetConsoleHeight(parseInt(TTAPI.GetProperty("consoleheight")), false, true);
	TTAPI.SetProperty("showconsole", "false");
}

/**
 * @public
 */
function CloseQuickWindow() {
	if (!this.checked) {
		var quickWindow = document.getElementById("mouseMoveInfo");
		if (!quickWindow) {
			return;
		}
		quickWindow.style.visibility = "hidden";
		quickWindow.style.display = "none";
	}
}
