window.onload = DoOnloadStuff;

function DoOnloadStuff() {
	
	var element = GetElementWithId("menu");
	if (element) {
		var menuNodes = element.childNodes;
		var done = false;
		for (var j = 0; j < menuNodes.length && !done; ++j) {
			if (menuNodes[j].nodeType == 1) {
				var listNodes = menuNodes[j].childNodes;
				for (var i = 0; i < listNodes.length; ++i) {
					var node = listNodes[i];
					if (node.nodeType == 1) {
						node.onmouseover = MenuItem_mouseover;
						node.onmouseout = MenuItem_mouseout;
						node.onclick = MenuItem_click;
						}
					}
				done = true;
				}
			}
		}

	var popups = document.getElementsByTagName("a");
	for (i = 0; i < popups.length; ++i) {
		if (popups[i].rel.indexOf("popup") != -1) {
			popups[i].onclick = DoPopup;
			}
		}
	}

var currentShower = null;
var planningToHide = false;
var t_hide;
var plannedShower = null;
var t_show;

function MenuItem_mouseover() {
	
	if (currentShower == this) {
		if (planningToHide) {
			clearTimeout(t_hide);
			planningToHide = false;
			}
		}
	else if (plannedShower != this) {
		if (plannedShower != null) {
			clearTimeout(t_show);
			plannedShower = null;
			}
		plannedShower = this;
		t_show = setTimeout(DoPlannedShowing, currentShower == null ? 200 : 1);
		}
	}

function MenuItem_mouseout() {
	
	if (plannedShower == this) {  // Cancel
		clearTimeout(t_show);
		plannedShower = null;
		}
	else if (currentShower == this  &&  !planningToHide) {
		planningToHide = true;
		t_hide = setTimeout(DoPlannedHiding, 500);
		}
	}

function MenuItem_click() {
	
	if (plannedShower == this) {
		clearTimeout(t_show);
		plannedShower = null;
		}
	else if (currentShower == this) {
		planningToHide = true;
		DoPlannedHiding();
		}
	}

function DoPlannedShowing() {
	
	if (plannedShower != null) {
		if (planningToHide) {
			DoPlannedHiding();
			clearTimeout(t_hide);
			}
		var elements = plannedShower.getElementsByTagName('div');
		if (elements.length > 0) {
			elements[0].style.display='block';
			}
		currentShower = plannedShower;
		plannedShower = null;
		}
	}

function DoPlannedHiding() {
	
	if (currentShower != null  &&  planningToHide) {
		 var elements = currentShower.getElementsByTagName('div');
		 if (elements.length > 0) {
			 elements[0].style.display='none';
			 }
		currentShower = null;
		planningToHide = false;
		}
	}

function GetElementWithId(id) {

	var obj = null;
	if (document.getElementById) {
		obj = document.getElementById(id);
		}
	else if (document.all) {
		obj = document.all[id];
		}
	else if (document.layers) {
		obj = document.layers[id];
		}
	
	return obj;
	}

var newWindow = null;

function ShowInPopup(url, strWidth, strHeight) {
	
	var features = "left=10, top=30, resizable, toolbar=no, status=no, directories=no, width="+strWidth+", height="+strHeight+"";
	newWindow = window.open(url, 'window', features);
	newWindow.focus();
	}


function DoPopup(e) {
	
	var width = "600";
	var height = "450";
	
	attribs = this.rel.split(" ");
	if (attribs[1] != null) {
		width = attribs[1];
		}
	if (attribs[2] != null) {
		height = attribs[2];
		}
	
	ShowInPopup(this.href, width, height);
	
	if (window.event) {
		window.event.returnValue = false;
		window.event.cancelBubble = true;
		} 
	else if (e) {
		e.stopPropagation();
		e.preventDefault();
		}
	}