/*
var gLinkObj = null;
var lastState = false;

function toggleMenu(linkObj, state) {
	if (gLinkObj != null && gLinkObj != linkObj) {
		setSubMenuVisibility(gLinkObj, false);
	}

	if (state == null) {				
		if (gLinkObj == linkObj) {
			if (lastState) {
				setSubMenuVisibility(linkObj, false);
			} else {
				setSubMenuVisibility(linkObj, true);
			}
			lastState = !lastState;
		} else {
			setSubMenuVisibility(linkObj, true);
			lastState = true;
		}

		gLinkObj = linkObj;
	} else {
		setSubMenuVisibility(linkObj, state);
		lastState = state;
		gLinkObj = linkObj;
	}
}

function setSubMenuVisibility(menuObj, visible) {
	if (visible) {
		var state = 'visible';
		menuObj.setAttribute('class', 'selected');
	} else {
		var state = 'hidden';
		menuObj.setAttribute('class', '');
	}

	var menuItems = menuObj.parentNode.childNodes;
	var empty = true;
	
	if (menuItems != null) {
		for (var j = 0; j < menuItems.length; j++) {
			if (menuItems.item(j).className == 'underMeny') {
				menuItems.item(j).style.visibility = state;
				empty = false;
			}
		}
	}
	
	if (empty && visible) {
		window.location = linkObj.href;
	}
}
*/
var gLinkObj = new Array();
var gLevel = new Array();

function toggleMenu(linkObj, level) {
	/* 	Hide all menuitems beneath a certain level if:
		- This is not the first menu to be opened, meaning there exists menuitems stored in the gLinkObj variable.
		- The last menuitem toggled (gLinkObj[gLinkObj.length - 1]) is not the same as the current menuitem (linkObj).
		- The menuitem stored in gLinkObj for the current level is not the same as the current menuitem.
		- The previous toggled level is deeper or at the same level as the current one.
	*/
	if ((gLinkObj.length != 0) &&
		(gLinkObj[gLinkObj.length - 1] != linkObj) &&
		(gLinkObj[level - 1] != linkObj) &&
		(gLevel[gLevel.length - 1] >= level)) {
		setSubMenuVisibility(level, false);
	}
	
	/*	If the current menuitem is the same as the last one toggled,
		then toggle it and all it's submenus, otherwise just show the current menuitem.
	*/
	if (gLinkObj.length != 0 && gLinkObj[level - 1] == linkObj) {
		setSubMenuVisibility(level, null);
	} else {
		setMenuItemVisibility(linkObj, true);
	}

	//	Store the current menuitem and it's level.
	gLinkObj[level - 1] = linkObj;
	gLevel[level - 1] = level;
}

function setMenuItemVisibility(mi, visible) {			
	if (visible == true) {
		var state = 'visible';
		mi.setAttribute('class', 'selected');
	} else if (visible == false) {
		var state = 'hidden';
		mi.setAttribute('class', '');
	} else {
		var state = null;
	}
	
	var submenu = mi.nextSibling;
	while (submenu != null && submenu.nodeType != 1) {
		submenu = submenu.nextSibling;
	}

	if (submenu == null && visible) {
		window.location = linkObj.href;
	} else {
		if (state != null) {
			submenu.style.visibility = state;
		} else {
			if (submenu.style.visibility == 'visible') {
				submenu.style.visibility = 'hidden';
				mi.setAttribute('class', '');
			} else {
				submenu.style.visibility = 'visible';
				mi.setAttribute('class', 'selected');
			}
		}
	}
}

function setSubMenuVisibility(level, visible) {
	if (visible == true) {
		var state = 'visible';
	} else if (visible == false) {
		var state = 'hidden';
	} else {
		var state = null;
	}
	
	for (i = (level - 1); i < gLinkObj.length && gLinkObj[i] != undefined; i++) {
		if (visible) {
			gLinkObj[i].setAttribute('class', 'selected');
		} else {
			gLinkObj[i].setAttribute('class', '');
		}
		
		var submenu = gLinkObj[i].nextSibling;
		while (submenu != null && submenu.nodeType != 1) {
			submenu = submenu.nextSibling;
		}

		if (submenu == null && visible) {
			window.location = linkObj.href;
		} else {
			if (state != null) {
				submenu.style.visibility = state;
			} else {
				if (submenu.style.visibility == 'visible') {
					submenu.style.visibility = 'hidden';
				} else {
					submenu.style.visibility = 'visible';
				}
			}
		}
	}
	
	var start = gLinkObj.length - 1;
	for (j = start; j > (level - 1); j--) {
		delete gLinkObj[j];
	}
}

function highLightMenuItem(id, state) {
	var obj = document.getElementById(id);
	
	if (state) {
		obj.src = obj.src.replace('.jpg', '_over.jpg');
	} else {
		obj.src = obj.src.replace('_over.jpg', '.jpg');
	}
}

function preload(name) {
	var img = new Image();
	img.src = '/img/'+name+'_over.jpg';
}

function openWindow(anchor, width, height) {
	var left = 0;
	var top = 0;
	
	if (screen.availWidth > 0 && screen.availHeight > 0) {
		left = (screen.availWidth / 2) - (width / 2);
		top = Math.floor(((screen.availHeight - 35) / 2) - (height / 2));
	}

	win = window.open(anchor.href, '', "height="+height+",width="+width+",left="+left+",top="+top+",scrollbars=1,resizable=1,menubar=0,toolbar=0,status=0,location=0,directories=0");
}
