// JavaScript Document

// modification: 11/05/2009 - Francis Adu-Gyamfi
// modified script to use fade in and fade out effects using the opacity.js script library.
// all existing code is still maintained to make the popup simply hide and show, but has been
// commented out

var id;
var navRoot;

function showMenu(menu, left, top) {
	var menuLink = document.getElementById(menu);
	var menuObj = document.getElementById(menu + "_menu");
	
	if( menuObj == null ) {
		return;	
	}
	
	if(menuObj != null && menuObj.childNodes.length < 1) {
		return;
	}
	
	if( navRoot == menuObj ) {
		clearTimeout(id);
		id = setTimeout("hideMenu()", 5000);
		return;	
	}
	
	if(navRoot != null && navRoot != menuObj) {
		//navRoot.className = "menu";
		navRoot.style.display = "none";
		//hideMenu();
		clearTimeout(id);
	}
	
	navRoot = menuObj;
	//navRoot.className = "menu_over";
	//opacity(menu + "_menu", 100, 0, 50);
	navRoot.style.display = "block";
	opacity(menu + "_menu", 0, 100, 300);
	
	
	var obj = document.getElementById(menu) || document.images[menu] || 
							document.links[menu] || document.anchors[menu];
							
	navRoot.style.left = moveXbySlicePos(left, obj) + "px";
	navRoot.style.top = moveYbySlicePos(top, obj) + "px";
	
	// current code added to cater for when the popup menu is too large
	// and goes of screen or is resized to fit on screen
	var screenWidth = document.body.offsetWidth;
	var popupWidth = navRoot.offsetWidth;
	
	// check if the current position of the popup should make it goes off screen
	// or be resized and move it back
	var leftPos = parseInt(navRoot.style.left);
	var sizeCheck = (leftPos + popupWidth + 30);
	var amountHidden = sizeCheck - screenWidth;
	if( sizeCheck > screenWidth) {
		navRoot.style.left = (screenWidth - (popupWidth + 5 + amountHidden) ) + "px";	
		//alert("Navroot ought to move to: " + navRoot.style.left);
	}
	
	for (i=0; i < navRoot.childNodes.length; i++) {
	  node = navRoot.childNodes[i];
	  
		node.onmouseover = function() {
			clearTimeout(id);
			//navRoot.className = "menu_over";
		}
		
		node.onmouseout = function() {
			id = setTimeout("hideMenu()", 1000);
		}
	}
	
	id = setTimeout("hideMenu()", 3000);
}

function showDiv(menu, x, y) {
	if(navRoot != null) {
		navRoot.className = "menu";
	}
	
	//document.getElementById(menu).style.display = "block";
		
//	var menuItem = document.getElementById(menu + "_div");
	
	navRoot = document.getElementById(menu + "_div");
	navRoot.style.display = "block";
	navRoot.style.visibility = "visible";
	
	var obj = document.getElementById(menu) || document.images[menu] || 
							document.links[menu] || document.anchors[menu];
							
	navRoot.style.left = moveXbySlicePos(x, obj);
	navRoot.style.top = moveYbySlicePos(y, obj);
}

function hideDiv(invoker) {
	document.getElementById(invoker + "_div").style.display = "none";
	document.getElementById(invoker + "_div").style.visibility = "hidden";
}

function moveXbySlicePos (x, img) { 
	if (!document.layers) {
		var onWindows = navigator.platform ? navigator.platform == "Win32" : false;
		var macIE45 = document.all && !onWindows && getExplorerVersion() == 4.5;
		var par = img;
		var lastOffset = 0;
		if(par){
			if( par.leftMargin && ! onWindows ) x += parseInt(par.leftMargin);
			if( (par.offsetLeft != lastOffset) && par.offsetLeft ) x += parseInt(par.offsetLeft);
			if( par.offsetLeft != 0 ) lastOffset = par.offsetLeft;
			par = macIE45 ? par.parentElement : par.offsetParent;
		}
	} else if (img.x) x += img.x;
	return x;
}

function moveYbySlicePos (y, img) {
	if(!document.layers) {
		var onWindows = navigator.platform ? navigator.platform == "Win32" : false;
		var macIE45 = document.all && !onWindows && getExplorerVersion() == 4.5;
		var par = img;
		var lastOffset = 0;
		if(par){
			if( par.topMargin && !onWindows ) y += parseInt(par.topMargin);
			if( (par.offsetTop != lastOffset) && par.offsetTop ) y += parseInt(par.offsetTop);
			if( par.offsetTop != 0 ) lastOffset = par.offsetTop;
			par = macIE45 ? par.parentElement : par.offsetParent;
		}		
	} else if (img.y >= 0) y += img.y;
	return y;
}

function hideMenu(menu) {
	//navRoot.className = "menu";
	opacity( navRoot.id, 100, 0, 300);
	clearTimeout(id);
	id = setTimeout("cleanup()", 500);
}

function cleanup() {
	navRoot.style.display = "none";
	navRoot = null;
}

