/***************************************************************************************
NAVIGATION MENU

This collection of parameters and functions supports the navigation menu that is
defined in "index.html" as a structure of <ul><li><a> tags with style classes that
control their looks and behaviors. The menu is hierachical and allows to select
exactly one page per menu-item. It also supports browsing from page to page -
assuming that one page is represented by exactly one menu-item.

To make the menu work it is important to follow the file-structure of the pages 
(see "pages" directory) and the call of a page through "javascript:selectPage()" as
shown in "index.html". Check also the use of classes in "index.html" carefully (<ul> 
and <a> tags).

@copyright: Crealogix AG
@author: Christoph Künzler
@version: 1.0 / 2006-07-26
***************************************************************************************/

// array of subnav (<ul>) objects, collected by getElementsByTagName('ul')
var subnav;

// array (sequence list) of menu-items (references to objects of the class "MenuItem")
var pageSequence = new Array();

// helper array that allows a quick access of menu-items by their pageId
var pageIndexes = new Array();

// last page that was opened
var lastPageId;
var actualPageIndex = 0;

// URL snippets
page_path = getPagesFolder();
page_extension = ".html";

function getPagesFolder() {
	// PRECONDITION: "index.html" is in the document-root of the site
	var root = "./";

	// try with "index.html"
	if (language != 'd') {
		var i = document.location.pathname.indexOf("index_" + language + ".html");
	}
	else var i = document.location.pathname.indexOf("index.html");
	
	if (i > 0) {
		root = document.location.pathname.substring(0, i);
	}
	else root = document.location.pathname;
	
	return root + "pages/content/";
}

// BUILD MENU -----------------------------------------------------------------------------
function startNavMenu() {
	MM_preloadImages('/images/suvapro_arrow.gif', '/images/suva_arrow-white.gif', '/images/suva_arrow-down-white.gif','/images/suvapro_arrow_down.gif');
	setChapterStructure();
	selectPage(pageSequence[0].id);
}

function setChapterStructure() {
	// set collection of subnav elements (at least the root <ul> tag should be available)
	var navigation = document.getElementById('navigation');	
	if (navigation) subnav = navigation.getElementsByTagName('ul');
	if (!subnav || subnav.length == 0) return;

	// register menu-items
	var liItems = subnav[0].getElementsByTagName('li');
	var aItems, ulItems;
	var i, p1, p2;
	var itemID, ulItem, menuItem;
	
	for (i=0; i<liItems.length; i++) {
		aItems = liItems[i].getElementsByTagName('a');
		ulItems = liItems[i].getElementsByTagName('ul');
		
		// register first <a> tag with subnav-node
		p1 = aItems[0].href.indexOf("('");
		p2 = aItems[0].href.indexOf("')");
		
		if (p1 > 0 && p2 > 0) {
			itemId = aItems[0].href.substring(p1 + 2, p2);
			ulItem = (ulItems && ulItems.length > 0) ? ulItems[0] : null;
			menuItem = new MenuItem(itemId, aItems[0], pageSequence.length, ulItem);
			pageSequence[pageSequence.length] = menuItem;
			pageIndexes[itemId] = menuItem;
		}
	}
}

// MENU ITEM CLASS -----------------------------------------------------------------------------------
function MenuItem(id, aItem, index, ulItem) {
	this.id = id;
	this.item = aItem;
	this.index = index;
	this.path = id.split("_");
	this.level = this.path.length;
	this.subnav = ulItem;
}

// SELECT PAGE ---------------------------------------------------------------------------------------

function selectPage(pageId) {
	actualPageId = pageId;
	
	if (!checkFinalTestActive()) return; // see in "behCourse.js"
//alert("LAGUAGE: " + language);	
	// open page's URL
	if (parent.content) {
		var chapterPath = pageId.split("_");
		parent.content.document.location = page_path + chapterPath[0] + "\/" + pageId + '_' + language + page_extension;
	}

	// reset menu
	closeAllSubchapters();
	
	if (lastPageId) {
		expandMenuItem(lastPageId, false);
		hiliteMenuItem(lastPageId, false);
	}
	
	// expand and hilite new page (expand parent chapters and subchapter)
	expandMenuItem(pageId, true);
	hiliteMenuItem(pageId, true);

	// buffer state data
	lastPageId = pageId;
	refreshActualPageIndex(pageId);
}

function expandMenuItem(pageId, expand) {
	var id = "";
	var level;
	var chapterPath = pageId.split("_");
	
	for (level = 0; level < chapterPath.length; level++) {
		if (level > 0) id += "_";
		id += chapterPath[level];
		
		menuItem = getMenuItem(id);

		if (menuItem && menuItem.subnav) {
			// expand <ul> block
			menuItem.subnav.style.display = (expand) ? "block" : "none";
	
			// expanding chapter shows turned triangle bitmap
			menuItem.item.className = (expand) ? "subnavOpen" : "subnav";
		}
        else return;
	}	
}

function closeAllSubchapters() {
	for (var i = 1; i < subnav.length; i++) {
		subnav[i].style.display = "none";
	}
}

function hiliteMenuItem(pageId, hilite) {
	var menuItem = getMenuItem(pageId);
	if (menuItem) menuItem.item.id = (hilite) ? "navLevelActive" : "";
}

function refreshActualPageIndex(pageId) {
	var menuItem = getMenuItem(pageId);
	
	if (menuItem) {
		actualPageIndex = menuItem.index;

		//FELS 28.07.2006 insert actualPageName to pageTitle
		document.getElementById("pageTitle").firstChild.data = menuItem.item.firstChild.data;
	}
}

function openSamePage(lang) {
	language = lang;
	selectPage(actualPageId);
}


function getMenuItem(pageId) {
	return pageIndexes[pageId];
}

// PREV / NEXT PAGE -------------------------------------------------------------------------
function prevPage() {
	if (!checkFinalTestActive()) return; // see in "behCourse.js"
	
	actualPageIndex--;
	if (actualPageIndex < 0) actualPageIndex = pageSequence.length - 1;
	
	selectPage(pageSequence[actualPageIndex].id);
}

function nextPage() {
	if (!checkFinalTestActive()) return; // see in "behCourse.js"
	
	actualPageIndex++;
	if (actualPageIndex >= pageSequence.length) actualPageIndex = 0;
	
	selectPage(pageSequence[actualPageIndex].id);
}

// RELOAD PAGE -------------------------------------------------------------------------
function reloadPage() {
	var frame = parent.content.document.location;

	parent.content.document.location = frame;
}
