/**
 * Contains menu related functions
 *
 * @author fritzd
 */

/** Prefix of image id names, which are graphical elements of the menu **/
var MENU_IMAGE_PREFIX = "menuImage";
/** Currently ed menu group **/ 
var ACTIVE_GROUP = "Home";
/** Name of header group for booked courses **/
var BOOKED_COURSES_HEADER_NAME = "Courses";
/** Flag in querystring that indicates, that page request needs to get forwarded**/
var FORWARD_FLAG = "forward";
/** Url to which page requests needs to get forwarded **/
var FORWARD_URL = "forwardUrl";
/** Menu group that needs to get activated for page request forward **/
var MENU_GROUP = "menuGroup";

var currentCourseWindow=null;
var closeAtTerminate=false;

var sURL = unescape(window.location.pathname);

function refresh()
{
    //  This version does NOT cause an entry in the browser's
    //  page view history.  Most browsers will always retrieve
    //  the document from the web-server whether it is already
    //  in the browsers page-cache or not.
    //  
    window.location.replace( sURL );
}



/**
 * Gets called when clicking on menu item. Triggers one of three operations dependent on the item type:
 * PARENT : extends sub menu of parent item
 * LEAF_SCO : launches sco in content window
 * LEAF_PAGE : loads page in content frame
 */
function menu_action(id, type, pageUrl, scoId, courseId) {
	switch(type) {
		case "PARENT":
			var elem = document.getElementById(courseId + "_collapse");
			if ( elem != null ){
				if (elem.style.display == "none") {
					elem.style.display = "";			
				}
				else {
					elem.style.display = "none";
				}
			}
			break;
		case "LEAF_SCO":
			parent.control_launchScoFromMenu("{target=" + scoId + "}choice", courseId)
			break;
		case "LEAF_PAGE":
			parent.control_getContentFrame().location.href = pageUrl;
			break;
		case "LEAF_ASSET":
			parent.control_getContentFrame().location.href = pageUrl;
			break;
		default:
			alert("Unsupported menu item type: " + type);
	}		
}

/**
 * Display new menu group. Hides previously displayed menu group. Updates header text.
 */
function menu_displayGroup(linkOrName, href, dontloadInitialGroupPage) {

	
    var menuFrame = parent.frames["menu"];
    var headerFrame = parent.frames["header"];
    var menuDoc = menuFrame.document;
	var newGroupName = menu_changeActiveGroup(linkOrName, menuFrame, headerFrame );
	
	//check if we want to load initial page of menu group 
	if (!dontloadInitialGroupPage && menuDoc.getElementById(newGroupName) != null )
	{
		var itemDiv = menuDoc.getElementById(newGroupName);
	    menu_displayInitialGroupPage( href, itemDiv);
	}
	
}

/**
 * Display new menu group. Hides previously displayed menu group. Updates header text.
 */
function menu_changeActiveGroup(linkOrName, menuFrame, headerFrame) {
    var newGroupName = ( typeof linkOrName == 'string' ) ? linkOrName : linkOrName.id;
	newGroupName = util_clean(newGroupName);
    var menuDoc = menuFrame.document;
    var headerDoc = headerFrame.document;
    var oldGroupName = menuFrame.ACTIVE_GROUP;
    
	if (oldGroupName && menuDoc.getElementById(oldGroupName) != null )
	{
		menuDoc.getElementById(oldGroupName).style.display = "none";

		//course sub-menu is an exception, because it does not appear in the header
		if (oldGroupName != BOOKED_COURSES_HEADER_NAME) {
			headerDoc.getElementById(oldGroupName).className = "navHeaderFont";
		}
	}
	
	if (headerDoc.getElementById(newGroupName) != null)
	{
		var div = menuDoc.getElementById(newGroupName);
		div.style.display = "";
		//course sub-menu is an exception, because it does not appear in the header
		if (newGroupName != BOOKED_COURSES_HEADER_NAME) {
			headerDoc.getElementById(newGroupName).className = "navHeaderFontActive";
		}	
	}
	
	
	menuFrame.ACTIVE_GROUP = newGroupName;

	if( menuFrame.ACTIVE_GROUP != BOOKED_COURSES_HEADER_NAME) menu_hideCourses();

	return newGroupName;
}

/**
 * show the href if, set, otherwise show first menuitem
 */
function menu_displayInitialGroupPage( href, itemDiv) {
	if ( href != "" ){
		parent.control_getContentFrame().location.href = href;	
	}
	else{
		//find the item divs in the item group and fire the onclick event on first
		//alert(href);
		var divs = itemDiv.getElementsByTagName("div");
		var menuItem=divs[1];
		if (menuItem) {
			var link=menuItem.getElementsByTagName("a")[0];
			link.onclick();		
		}
	}
}

/**
 * Displays first section of course. Puts menu into right state for course.
 */
function menu_launchFirstSection(courseName, courseId, contentObjectId, bookingId) {
	parent.frames["menu"].document.getElementById(ACTIVE_GROUP).style.display = "none";
	parent.frames["menu"].document.getElementById(BOOKED_COURSES_HEADER_NAME).style.display = "";
	parent.frames["menu"].ACTIVE_GROUP = BOOKED_COURSES_HEADER_NAME;
	menu_hideCourses();
	menu_unHideCourse(BOOKED_COURSES_HEADER_NAME);
	parent.control_launchSco("START_OR_RESUME", courseId, bookingId);
}


function menu_launchCourseFromTrainingplan(launchInNewWindow, courseName, courseId, contentObjectId, bookingId, courseType){

	if(launchInNewWindow && launchInNewWindow=='true'){
		menu_launchCourseInIndependentWindow(courseId, bookingId, courseType);
	}else{
		menu_getCourseMenuForCourse(courseName, courseId, contentObjectId, bookingId);
	}

}


function menu_getCourseMenuForCourse(courseName, courseId, contentObjectId, bookingId){
	parent.frames["menu"].document.body.innerHTML = loadXMLDoc("/frontcontroller.lms?slMethod=com.atl.lms.servicelayer.Service.getMenuForCourse&viewName=view&courseId=" + courseId);
	parent.frames["menu"].document.getElementById("Home").style.display = "none";
	menu_launchFirstSection(courseName, courseId, contentObjectId, bookingId);
}


function menu_getCourseMenuForCourseIndex(courseName, courseId, contentObjectId, bookingId){
	parent.frames["menu"].document.body.innerHTML = loadXMLDoc("/frontcontroller.lms?slMethod=com.atl.lms.servicelayer.Service.getMenuForCourse&viewName=view&courseId=" + courseId);
	parent.frames["menu"].document.getElementById("Home").style.display = "none";

	parent.frames["menu"].document.getElementById(ACTIVE_GROUP).style.display = "none";
	parent.frames["menu"].document.getElementById(BOOKED_COURSES_HEADER_NAME).style.display = "";
	parent.frames["menu"].ACTIVE_GROUP = BOOKED_COURSES_HEADER_NAME;
	menu_hideCourses();
	menu_unHideCourse(BOOKED_COURSES_HEADER_NAME);
	
	parent.control_launchSco("{target=firstScoIndexATL}choice", courseId, bookingId);

}

function menu_launchCourseTestSco(testableId, courseId, bookingId){ 



	if (this.currentCourseWindow)
	{	
		this.currentCourseWindow.close();
		this.currentCourseWindow=null;
		this.closeAtTerminate=false;

	}
	
	parent.control_launchCourseTestSco(testableId, courseId, bookingId);
	
}
	

function menu_launchCourseInIndependentWindow(courseId, bookingId, courseType){

	if (this.currentCourseWindow)
	{	
		this.currentCourseWindow.close();
		this.currentCourseWindow=null;
		this.closeAtTerminate=false;

	}
	
	var htmlPage = "";

	if (courseType=="ATL" || courseType=="ATL_NO_TEST"){
		this.closeAtTerminate=false;
		this.currentCourseWindow = 	window.open("ATL_C.html?78e67865f05f="+courseId+"&528ff62ce="+bookingId, "flpage","titlebar=0, resizable=1, toolbar=0,menubar=0,status=0,scrollbars=0,location=0");
	}else if(courseType=="ABSOLUTELY_FLASH_PLAYER"){
		this.closeAtTerminate = false;
		this.currentCourseWindow = 	window.open("FLASH_PLAYER.html?78e67865f05f="+courseId+"&528ff62ce="+bookingId, "flpage","titlebar=0,toolbar=0,menubar=0,status=0,scrollbars=0,location=0, width=870,height=720");
	}else if(courseType=="THIRD_PARTY_PLUS_TEST"){
		this.closeAtTerminate = false;
		this.currentCourseWindow = 	window.open("THIRDPARTY_PLUS_TEST.html?78e67865f05f="+courseId+"&528ff62ce="+bookingId, "flpage","titlebar=0, resizable=1, toolbar=0,menubar=0,status=0,scrollbars=0,location=0");
	}else{
		this.closeAtTerminate=false;
		this.currentCourseWindow = 	window.open("FLASH_PLAYER.html?78e67865f05f="+courseId+"&528ff62ce="+bookingId, "flpage","titlebar=0,toolbar=0,menubar=0,status=0,scrollbars=0,location=0");
	}
	
}

function menu_currentCourseWindowAtTerminate(){

	// this is deprecated now. Flash Player courses don't close on terminate.
	if (this.currentCourseWindow && this.closeAtTerminate)
	{
		this.currentCourseWindow.close();
		this.currentCourseWindow=null;
		this.closeAtTerminate=false;
	}
	
}

function menu_currentCourseWindow(){


	if (this.currentCourseWindow)
	{
		this.currentCourseWindow.close();
		this.currentCourseWindow=null;
		this.closeAtTerminate=false;
	}
	
	
}



function menu_unHideCourse(courseName) {
	var div = parent.frames["menu"].document.getElementById(util_clean(courseName));
	var innerDivs = div.getElementsByTagName("div");
	for (var i = 0; i < innerDivs.length; i++)
	{
		innerDivs[i].style.display = "";
	}
}

function menu_hideCourses() {
	var div = parent.frames["menu"].document.getElementById(BOOKED_COURSES_HEADER_NAME);
	var innerDivs = div.getElementsByTagName("div");
	for (var i = 1; i < innerDivs.length; i++)
	{
		innerDivs[i].style.display = "none";
	}
}

/**
 * Handles javascript side of forwarding functionality. This includes, extracting the forwaring url from the query string,
 * loading it into the content frame and updating the menu.
 */
function menu_forwardToUrl() {
				
	if (String(top.location).match(/\?(.*)$/)) {
					
		var queryString = RegExp.$1;
		var arr = queryString.split(/\&/g);
		var parameterMap = new Array();
		for (var i = 0; i < arr.length; i++) {
			var pair = arr[i].split(/=/);
	
			parameterMap[pair[0]] = pair[1];
		}

		if (parameterMap[FORWARD_FLAG] == "true") {
			var url = unescape(parameterMap[FORWARD_URL]);
			var menuGroup = parameterMap[MENU_GROUP];
			parent.frames["content"].document.location.href = url;
			menu_displayGroup(menuGroup, "",true);
			
		}
		return true;
	}
	return false;	
	
}

/**
 * Gets called when clicking on an sco outside the menu. Loads the course menu and launches the sco
 */
function menu_launchScoAndGetMenu(courseName, courseId, itemIdentifier)  {

	parent.frames["menu"].document.body.innerHTML = 
			loadXMLDoc("/frontcontroller.lms?slMethod=com.atl.lms.servicelayer.Service.getMenuForCourse&viewName=view&courseId=" + courseId);
	parent.frames["menu"].document.getElementById("Home").style.display = "none";
	parent.frames["menu"].document.getElementById(ACTIVE_GROUP).style.display = "none";
	parent.frames["menu"].document.getElementById(BOOKED_COURSES_HEADER_NAME).style.display = "";
	parent.frames["menu"].ACTIVE_GROUP = BOOKED_COURSES_HEADER_NAME;
	menu_hideCourses();
	menu_unHideCourse(BOOKED_COURSES_HEADER_NAME);


	parent.control_launchScoFromMenu("{target=" + itemIdentifier + "}choice", courseId);
		
}

/**
 * Gets called when clicking on an sco outside the menu. Loads the course menu and launches the sco
 */
function menu_launchScoAndGetMenuForCOIds(courseName, contentObjectCourseId, contentObjectSCOId)  {

	parent.frames["menu"].document.body.innerHTML = 
			loadXMLDoc("/frontcontroller.lms?slMethod=com.atl.lms.servicelayer.Service.getMenuForCourseForCOId&viewName=view&contentObjectCourseId=" + contentObjectCourseId);
	parent.frames["menu"].document.getElementById("Home").style.display = "none";
	parent.frames["menu"].document.getElementById(ACTIVE_GROUP).style.display = "none";
	parent.frames["menu"].document.getElementById(BOOKED_COURSES_HEADER_NAME).style.display = "";
	parent.frames["menu"].ACTIVE_GROUP = BOOKED_COURSES_HEADER_NAME;
	menu_hideCourses();
	menu_unHideCourse(BOOKED_COURSES_HEADER_NAME);


	parent.control_launchScoFromMenuForCOIds("{target=" + "Sco"+contentObjectSCOId + "}choice", contentObjectCourseId);
		
}



/**
 * Gets called when clicking on an sco outside the menu. Loads the course menu and launches the sco
 */
function menu_launchScoAndGetMenuForContentObjectLink(courseName, contentObjectCourseId, contentObjectSCOId)  {

	parent.frames["menu"].document.body.innerHTML = 
			loadXMLDoc("/frontcontroller.lms?slMethod=com.atl.lms.servicelayer.Service.getMenuForCourseForCOId&viewName=view&contentObjectCourseId=" + contentObjectCourseId);
	parent.frames["menu"].document.getElementById("Home").style.display = "none";
	parent.frames["menu"].document.getElementById(ACTIVE_GROUP).style.display = "none";
	parent.frames["menu"].document.getElementById(BOOKED_COURSES_HEADER_NAME).style.display = "";
	parent.frames["menu"].ACTIVE_GROUP = BOOKED_COURSES_HEADER_NAME;
	menu_hideCourses();
	menu_unHideCourse(BOOKED_COURSES_HEADER_NAME);


	parent.control_launchScoFromMenuForContentObjectLink("{target=" + "Sco"+contentObjectSCOId + "}choice", contentObjectCourseId);
		
}

