﻿/*
 * JavaScript voor de frameset (index) pagina
 */

// Prevent loading in another frameset
if (top.frames.length != 0) top.location = self.document.location;

/*
 *  Constants & Variables
\**************************/

// declarations of the sitemap and the name of the current active menuItem and tab
var mainMenu, activeMenuItemName, activeTabName;

// indicates if the redirection is already handled
var redirected = false;

/*
 *  Objects  
\************/

/*
 * Image constructor to fool Opera
 */
function Image(width, height) {
	this.width = width;
	this.height = height;
	this.src = "";

	return this;
}

/*
 * Constructor of a Menu object. Contains the available menuItems.
 */
function Menu() {
	// data
	this.menuItems = new Array();
	// functions
	this.numberOfMenuItems = new Function("return this.menuItems.length");
	this.addMenuItem = menuAddMenuItem;
	this.getMenuItem = menuGetMenuItem;
	
	return this;
}

function menuAddMenuItem(menuItem) {
	this.menuItems[this.menuItems.length] = menuItem;
	return this;
}



/*
 * Constructor of a MenuItem object. Contains the name, the status text, the 
 * images of a menuItem (normal, rollover and active) and the tabs it covers.
 */
function MenuItem(name, statusText) {
	// data
	this.name = name;
	this.statusText = statusText;
	
	
	if (document.images) {
		this.imageNormal = new Image(); this.imageNormal.src = MENU_IMAGE_DIR + "/" + this.name + "-n.gif";
		this.imageRollOver = new Image(); this.imageRollOver.src = MENU_IMAGE_DIR + "/" + this.name + "-r.gif";
		this.imageActive = new Image(); this.imageActive.src = MENU_IMAGE_DIR + "/" + this.name + "-a.gif";
	}
	
	
	this.tabs = new Array();
	
	// functions
	
	this.getName = new Function("return this.name");
	this.getStatusText = new Function("return this.statusText");
	this.getImageNormalSrc = new Function("return this.imageNormal.src");
	this.getImageRollOverSrc = new Function("return this.imageRollOver.src");
	this.getImageActiveSrc = new Function("return this.imageActive.src");
	this.getTabSrc = new Function("return \"tab-\" + this.name + \".html\"");
	this.addTab = menuItemAddTab;
	this.getTab = menuItemGetTab;
	this.numberOfTabs = new Function("return this.tabs.length");
	this.equals = new Function("rhs", "return this.name == rhs.getName()");
	
	return this;
}	

function menuItemAddTab(tab) {
	this.tabs[this.tabs.length] = tab;
	return this;
}

function menuItemGetTab(index) {
	if (typeof(index) == "string") {		// index is the name of a menuItem
		for (var i = 0; i < this.numberOfTabs(); i++) {
			if (this.tabs[i].getName() == index) return this.tabs[i];
		}
		alert("Error: unknown index in menuItemGetTab: " + index);
	} else if(typeof(index) == "number") {	// index is an array-index
		if (index > this.numberOfTabs() - 1) {
			alert("Error: index out of bounds in menuItemGetTab: " + index);
		} else {
			return this.tabs[index];
		}
	} else {
		alert("Error: unknown type in menuGetTab: " + typeof(index));
	}
}

/*
 * Constructor of a Tab object. Contains the name and the covered pages.
 */ 
function Tab(name) {
	// data
	this.name = name;
	this.imageNormalSrc = TAB_IMAGE_DIR + "/" + name + "-n.gif";
	this.imageActiveSrc = TAB_IMAGE_DIR + "/" + name + "-a.gif";
	this.pages = new Array();
	// functions
	this.getName = new Function("return this.name");
	this.getImageNormalSrc = new Function("return this.imageNormalSrc");
	this.getImageActiveSrc = new Function("return this.imageActiveSrc");
	this.numberOfPages = new Function("return this.pages.length");
	this.equals = new Function("rhs", "return this.name == rhs.getName()");
	this.addPage = tabAddPage;
	this.getPage = tabGetPage;
	
	return this;		
}

function tabAddPage(page) {
	this.pages[this.pages.length] = page;
	return this;
}

function tabGetPage(index) {
	if (typeof(index) == "string") {		// index is the name of a page
		for (var i = 0; i < this.numberOfPages(); i++) {
			if (this.pages[i].getName() == index) return this.pages[i];
		}
		alert("Error: unknown index in tabGetPage: " + index);
	} else if(typeof(index) == "number") {	// index is an array-index
		if (index > this.numberOfPages() - 1) {
			alert("Error: index out of bounds in tabGetPage: " + index);
		} else {
			return this.pages[index];
		}
	} else {
		alert("Error: unknown type in tabGetPage: " + typeof(index));
	}
}
	
/*
 * Constructor of a Page object. Contains the (file)name of the page.
 */ 
function Page(url) {
	// data
	this.url = url;
	// functions
	this.getUrl = new Function("return this.url");
	this.equals = new Function("rhs", "return this.url == rhs.getUrl()");
	
	return this;		
}

/*
 * Functions
\*************/

/*
 * Highlights a menu item
 *
 * Parameters:	name:	the name of the menu item
 */
function turnOnMenuItem(name) {
	var menuItem = mainMenu.getMenuItem(name);
	if (document.images && (name != activeMenuItemName)) {
		eval("menu.document." + name).src = menuItem.getImageRollOverSrc();
	}
	return setStatus(menuItem.getStatusText());
}

/*
 * Unhighlights a menu item
 *
 * Parameters:	name:	the name of the menu item
 */
function turnOffMenuItem(name) {
	var menuItem = mainMenu.getMenuItem(name);
	if (document.images && (name != activeMenuItemName)) {
		eval("menu.document." + name).src = menuItem.getImageNormalSrc();
	}
	emptyStatus();
}

/*
 * Highlights the given menu item and loads the given tab.
 *
 * Parameters:	menuItemName:	the name of the menu item
 *				tabName:		the name of the tab
 */
function navigate(menuItemName, tabName) {
	var activatedMenuItem = mainMenu.getMenuItem(menuItemName);
	var deactivatedMenuItem = mainMenu.getMenuItem(activeMenuItemName);
	var activatedTab = activatedMenuItem.getTab(tabName);
	var deactivatedTab = deactivatedMenuItem.getTab(activeTabName);
	if (document.images) {
		if (!activatedMenuItem.equals(deactivatedMenuItem)) {
			// change menuItem images
			eval("menu.document." + activeMenuItemName).src = deactivatedMenuItem.getImageNormalSrc();
			eval("menu.document." + menuItemName).src = activatedMenuItem.getImageActiveSrc();
			// load the tab
			tab.location.replace(activatedMenuItem.getTabSrc());
		}
		if (!activatedTab.equals(deactivatedTab)) {
			// If the menu item is changed, a new tab frame is loaded and therefor
			// the active tab doesn't need to be switched off. In this case the new
			// active tab is loaded by the tab window itself.
			// If the menu item isn't changed, the tabs have to be switched on and off
			if (activatedMenuItem.equals(deactivatedMenuItem)) {
				// menu item didn't change, so switch tabs
				eval("tab.document." + activeTabName).src = deactivatedTab.getImageNormalSrc();
				eval("tab.document." + tabName).src = activatedTab.getImageActiveSrc();
			}
		}
		if (IE) {
			// remove focus from images (to avoid the silly line around the image in IE)
			self.focus();		
		}
	}
	activeMenuItemName = menuItemName;
	activeTabName = tabName;
}

/*
 * Finds the menuItem that covers the given page or tab
 *
 * Parameters:	id:		the url of a page or the name of a tab
 *
 * Returns:		the menuItem that covers given the page or tab
 */
function getCoveringMenuItem(id) {
	if (id.substring(id.length - 5) == ".html") {
		// id is the url of a page
		for (var mi = 0; mi < mainMenu.numberOfMenuItems(); mi++) {
			var menuItem = mainMenu.getMenuItem(mi);
			for (var t = 0; t < menuItem.numberOfTabs(); t++) {
				var thisTab = menuItem.getTab(t);
				for (var p = 0; p < thisTab.numberOfPages(); p++) {
					if (thisTab.getPage(p).getUrl() == id) return menuItem;
				}
			}
		}
		alert("Error: unknown pageUrl in getCoveringMenuItem: " + id);
	} else {
		// id is the name of a tab
		for (var mi = 0; mi < mainMenu.numberOfMenuItems(); mi++) {
			var menuItem = mainMenu.getMenuItem(mi);
			for (var t = 0; t < menuItem.numberOfTabs(); t++) {
				if (menuItem.getTab(t).getName() == id) return menuItem;
			}
		}
		alert("Error: unknown tabName in getCoveringMenuItem: " + id);
	}
}

/*
 * Finds the tab that covers the given page
 *
 * Parameters:	pageUrl:	the url of the page
 *
 * Returns:		the menuItem that covers the page
 */
function getCoveringTab(pageUrl) {
	for (var mi = 0; mi < mainMenu.numberOfMenuItems(); mi++) {
		var menuItem = mainMenu.getMenuItem(mi);
		for (var t = 0; t < menuItem.numberOfTabs(); t++) {
			var thisTab = menuItem.getTab(t);
			for (var p = 0; p < thisTab.numberOfPages(); p++) {
				if (thisTab.getPage(p).getUrl() == pageUrl) return thisTab;
			}
		}
	}
	alert("Error: I can't find the page " + pageUrl + " in the sitemap in index.html");
}

/*
 * Prints text on the status bar of the browser
 *
 * Parameters:	txt:	the text to printf
 */
function setStatus(txt) {
	top.status = txt;
	return true;
}

/*
 * Clears the status bar
 */
function emptyStatus() {
	self.status="";
}	

