/**
 * loads all JavaScript files of the package and starts the application
 *
 * @author	Thomas Biller, Michael Weber <t.biller@online-congress.com, m.weber@online-congress.com>
 * @package	OCTV
 * @subpackage	OCTV load script
 */

/**
 * includes JavaScript files
 *
 * @param	String		optional parameters: the urls to include
 * @return	String		the path to the engine
 */
function octvLoadJavaScripts() {
	var nameOfThisScript = "octv/load.js";
	var preventFromCaching = true;
	var scripts2include = arguments;
	var scripts2includeCount = scripts2include.length;
	if (scripts2includeCount === 0) {
		return;
	}
	var head = document.getElementsByTagName("head")[0];
	var includedScripts = head.getElementsByTagName("script");
	var includedScriptsCount = includedScripts.length;
	var includedScriptsSources = new Array();
	var includedScriptsSourcesCount = 0;
	var path2engine = "";
	var nameOfThisScriptLength = nameOfThisScript.length;
	for (var i=0; i<includedScriptsCount; i++) {
		var source = includedScripts[i].getAttribute("src");
		if (source) {
			var indexOfThisScript = source.lastIndexOf(nameOfThisScript);
			if (indexOfThisScript!==-1 && source.substr((-1*nameOfThisScriptLength)===nameOfThisScript)) {
				path2engine = source.substr(0, indexOfThisScript);
			}
			includedScriptsSources[includedScriptsSourcesCount] = source;
			includedScriptsSourcesCount++;
		}
	}
	if (path2engine.substr(0, 4) !== "http" && document.getElementsByTagName("base").length>0) {
		path2engine = document.getElementsByTagName("base")[0].href + path2engine;
	}
	var killCacheParam = "";
	if (preventFromCaching === true) {
		var timestamp = new Date().valueOf();
		killCacheParam = "?cache_killer="+timestamp;
	}
	for (var i=0; i<scripts2includeCount; i++) {
		var source2include = path2engine+scripts2include[i]+killCacheParam;
		var alreadyIncluded = false;
		for (var j=0; j<includedScriptsCount; j++) {
			if (includedScriptsSources[j] === source2include) {
				alreadyIncluded = true;
				break;
			}
		}
		if (alreadyIncluded === false) {
			var newScriptElement = document.createElement("script");
			newScriptElement.setAttribute("src", source2include);
			newScriptElement.setAttribute("type", "text/javascript");
			newScriptElement.setAttribute("charset", "utf-8");
			head.appendChild(newScriptElement);
			includedScriptsSources[includedScriptsSourcesCount] = source2include;
			includedScriptsSourcesCount++;
			includedScriptsCount++;
		}
	}
	return path2engine;
}

/**
 * includes CSS files
 *
 * @param	String		optional parameters: the urls to include
 * @return	Void		returns nothing
 */
function octvLoadStyleSheets() {
	var css2include = arguments;
	var css2includeCount = css2include.length;
	if (css2includeCount === 0) {
		return;
	}
	
	var path2engine = path2OCTVengine;
	
	var head, frameDocument;
	if (frames.length ===2 && typeof(frames[0].document.getElementsByTagName("head")[0])!=='undefined') {
		frameDocument = frames[0].document;
		head = frames[0].document.getElementsByTagName("head")[0];
	} else {
		frameDocument = document;
		head = document.getElementsByTagName("head")[0];
	}
	var includedCss = head.getElementsByTagName("style");
	var includedCssCount = includedCss.length;
	var includedCssSources = new Array();
	var includedCssSourcesCount = 0;
	
	for (var i=0; i<includedCssCount; i++) {
		var source = includedCss[i].getAttribute("src");
		if (source) {
			includedCssSources[includedCssSourcesCount] = source;
			includedCssSourcesCount++;
		}
	}
	for (var i=0; i<css2includeCount; i++) {
		var source2include = path2engine+css2include[i];
		var alreadyIncluded = false;
		for (var j=0; j<includedCssCount; j++) {
			if (includedCssSources[j] === source2include) {
				alreadyIncluded = true;
				break;
			}
		}
		if (alreadyIncluded === false) {
			var newCssElement = frameDocument.createElement("link");
			newCssElement.setAttribute("href", source2include);
			newCssElement.setAttribute("type", "text/css");
			newCssElement.setAttribute("rel", "stylesheet");
			head.appendChild(newCssElement);
			includedCssSources[includedCssSourcesCount] = source2include;
			includedCssSourcesCount++;
			includedCssCount++;
		}
	}
}

/**
 * checks if all classes are loaded and calls the application starter
 *
 * @return	Void		returns nothing
 */
function octvClassCheck() {
	if (octvCheckLoadedClasses(octvClasses2check) === true) {
		window.clearInterval(octvClassChecker);
		octvStartApplication();
		delete octvClasses2check;
	}
}

/**
 * checks if all JavaScript files are loaded
 *
 * @param	Array		the class names which should be included
 * @return	Boolean		returns if all included classes are loaded
 */
function octvCheckLoadedClasses(classes2check) {
	var count = classes2check.length;
	for (var i=0; i<count; i++) {
		try {
			var class2check = classes2check[i];
			var classObject = eval("new "+class2check);
		} catch (e) {
			return false;
		}
	}
	return true;
}

/**
 * starts the application
 *
 * @return	Void		returns nothing
 */
function octvStartApplication() {
	try {
		octv = new OCTV();
		octv.init()
	}
	catch (e) {
		var error = new OCTV_Error(e);
	}
}

/**
 * hides the no JavaScript hint
 *
 * @return	Void		returns nothing
 */
function octvHideNoJavaScriptHint() {
	if (!frames || frames.length === 0) return;
	var jsHint = frames[0].document.getElementById("octvPrompt");
	if (jsHint !== null) {
		jsHint.className = "octvPrompt octvPromptLoader";
		frames[0].document.getElementById("octvPromptMessageText").innerHTML = "Loading engine";
	} else if (frames[0].location.href.indexOf('activate_javascript.') !== -1) {
		// frames[0].location.href = 'about:blank';
	}
}

// do onload
if (document.getElementById && document.createTextNode) {
	path2OCTVengine = octvLoadJavaScripts(
		"octv/js/octv_engine_compressed.js"
		// "octv/js/octv_engine.js"
		
		/*
		"octv/js/class_octv.js",
		"octv/js/class_octv_configuration.js",
		"octv/js/class_octv_prompt.js",
		"octv/js/class_octv_error.js",
		"octv/js/class_octv_debug.js",
		"octv/js/class_octv_ajax.js",
		"octv/js/class_octv_json.js",
		"octv/js/class_octv_helper.js",
		"octv/js/class_octv_environment.js",
		"octv/js/class_octv_authentication.js",
		"octv/js/class_octv_gateway.js",
		"octv/js/class_octv_protocol.js",
		//"octv/js/class_octv_cookie.js",
		//"octv/js/class_octv_domaincookie.js",
		//"octv/js/class_octv_lightbox.js",
		"octv/js/class_octv_interval.js",
		"octv/js/class_octv_ctrl.js",
		"octv/js/class_octv_module_player.js",
		"octv/js/class_octv_module_screen.js",
		"octv/js/class_octv_module_languagemenu.js",
		"octv/js/class_octv_module_moviemenu.js",
		"octv/js/class_octv_module_chaptermenu.js",
		"octv/js/class_octv_module_slidemenu.js",
		"octv/js/class_octv_module_variable.js",
		"octv/js/class_octv_module_questions.js",
		"octv/js/class_octv_module_answers.js"
		*/
	);
	octvClasses2check = new Array(
		"OCTV()",
		"OCTV_Configuration()",
		"OCTV_Prompt()",
		"OCTV_Error('octvLoaderTest')",
		"OCTV_Debug()",
		"OCTV_Ajax()",
		"OCTV_Json()",
		"OCTV_Helper()",
		"OCTV_Environment()",
		"OCTV_Authentication()",
		"OCTV_Gateway()",
		"OCTV_Protocol()",
		//"OCTV_Cookie()",
		//"OCTV_DomainCookie()",
		//"OCTV_Lightbox()",
		"OCTV_Interval()",
		"OCTV_Ctrl()",
		"OCTV_Module_Player()",
		"OCTV_Module_Screen()",
		"OCTV_Module_LanguageMenu()",
		"OCTV_Module_MovieMenu()",
		"OCTV_Module_ChapterMenu()",
		"OCTV_Module_SlideMenu()",
		"OCTV_Module_Variable()",
		"OCTV_Module_Questions()",
		"OCTV_Module_Answers()"
	);
	octvOldOnload = window.onload;
	window.onload = function() {
		if (typeof(octvOldOnload) === "function") {
			octvOldOnload();
		}
		octvLoadStyleSheets(
			"octv/css/octv.css",
			"octv/css/frameset.css"
		);
		octvHideNoJavaScriptHint();
		octvClassChecker = window.setInterval("octvClassCheck();", 10);
	};
} else {
	alert("The installed JavaScript version is too old.\nPlease update your browser.");
}

