"use strict";

// will not be obfuscated
var TTAPI;

window.onload = function () {
	// find TT-API
	if (typeof TTAPI === "undefined") {
		TTAPI = findAPI(top);
	}
	if (typeof TTAPI === "undefined" || !TTAPI) {
		return;
	}

	TTAPI.objCrossBrowserModel.AddEventListener(document, "contextmenu", TTAPI.objCrossBrowserModel.EventPreventDefault);

	// swap image?
	try {
		var sImgSrc = TTAPI.GetProperty("wbtloadingimage");
		if (sImgSrc) {
			var objImg = document.getElementById("loadingImg");
			if (objImg) {
				objImg.src = TTAPI.GetPathByProperty("wbtloadingimage") + sImgSrc;
			}
		}
	} catch (objError) {
		// Intentionally left blank
	}

	// load customized css file?
	try {
		var sCSSFile = TTAPI.GetProperty("covercssfile");
		if (sCSSFile) {
			TTAPI.objCrossBrowserModel.LoadCSSFile(TTAPI.GetPathByProperty("covercssfile") + sCSSFile, window);
		}
	} catch (objError) {
		// Intentionally left blank
	}

	// load customized js file?
	try {
		var sJSFile = TTAPI.GetProperty("coverjsfile");
		if (sJSFile) {
			TTAPI.objCrossBrowserModel.LoadJavaScriptFile(TTAPI.GetPathByProperty("coverjsfile") + sJSFile, window);
		}
	} catch (objError) {
		// Intentionally left blank
	}

	ContinueCOVOnLoad_01();
};

function ContinueCOVOnLoad_01() {
	if (typeof CustomCover === "function") {
		CustomCover(window, document);
	}
}

function findAPI(objWindow) {
	try {
		if (typeof TTAPI === "object" && TTAPI !== null) {
			return TTAPI;
		}
		if (objWindow.TTAPI) {
			return objWindow.TTAPI;
		}
		var nLen = objWindow.length;
		// does the window have frames?
		if (nLen > 0) {
			for (var i = 0; i < nLen; i++) {
				var theAPI = findAPI(objWindow.frames[i]);
				if (theAPI) {
					return theAPI;
				}
			}
		}
		return null;
	} catch (e) {
		return findNearestAPI(window);
	}
}

function findNearestAPI(win) {
	try {
		if (typeof TTAPI === "object" && TTAPI !== null) {
			return TTAPI;
		}
		if (win.TTAPI) {
			return win.TTAPI;
		}
		if (win.parent) {
			return findNearestAPI(win.parent);
		}
	} catch (e) {
		// Intentionally left blank
	}
	return null;
}
