var flyoutArray = null;
var hideFlyoutTimer = null;
var arrowImg = null; //the single arrow image that can be highlighted at any one time
var arrowImgSrc = '';

function startFlyoutTimer(hideTimeout) {
	hideFlyoutTimer = setTimeout("hideSubmenus()", hideTimeout);
	resetArrow();	

}
function resetFlyoutTimer() {
	if (hideFlyoutTimer) clearTimeout(hideFlyoutTimer);
	hideFlyoutTimer = null;
}

function hideFlyoutMenus() {
	resetFlyoutTimer();
	if( flyoutArray ) {
		for(var i in flyoutArray) {
			var m = document.getElementById(flyoutArray[i]);
			m.style.visibility = "hidden";			
		}
		flyoutArray = null;
	}
}
function resetArrow() {
	if (arrowImg != null) {
		arrowImg.src = arrowImgSrc;
		arrowImg = null;
	}
}

function hideSubmenus(containerName) {

	if( flyoutArray ) {
		
		var h = false;
		if (!containerName || containerName.match(/###/)) {
			hideFlyoutMenus();
			return;
		}			
		var c = 0;
		for(var i=0; i< flyoutArray.length; i++) {
			if( h ) {
				var m = document.getElementById(flyoutArray[i]);				
				m.style.visibility = "hidden";
			} else if( flyoutArray[i] == containerName ) {
				h = true;
			} else {
				c++;
			}
		}
		flyoutArray.length = c+1;
	}
}


function menuOverMenuItem(menuName, horizMenu, subMenuName, siteFolder, arrowName, isIE5) {

	resetFlyoutTimer();

	//get the parent container, and hide all below this
	var id = "";
	var parent = document.getElementById(menuName);	
	do  {
		id = parent.id;
		if (id) {
			if (id.match(/FlyContainer/)) break;
		}
	} while (parent = parent.offsetParent);
	

	hideSubmenus(id);
	
	if (subMenuName) showSubMenu(menuName, horizMenu, subMenuName, isIE5);

	//change the arrow image to reflect the text state
	//resetArrow();
	var img = document.getElementById(arrowName);
	if (img) {
		arrowImgSrc = img.src;
		arrowImg = img;
		if (arrowName.match(/Button/)) img.src=siteFolder+"/images/menu/arrowbuttonover.gif";
		else img.src=siteFolder+"/images/menu/arrowflyoutover.gif"; //arrow_Flymenu

		arrowImg = img;
	}
}

function showSubMenu(menuName, horizMenu, subMenuName, isIE5) {
	resetFlyoutTimer();
	var parent = document.getElementById(menuName);
	var e = document.getElementById(subMenuName)
	var w = parent.offsetWidth;	
	var h = parent.offsetHeight;

	//IE5, parent.offsetLeft includes the text alignment, not other browsers, 
	//so right-aligned text (will offset flyout block wrongly
	//Horizontal menus need left alignment added, which is OK since they are always left text-aligned anyway
	var x = parent.offsetWidth;
	if (isIE5 != "true" || horizMenu) x += parent.offsetLeft;
	var y = parent.offsetTop + h;
	
	while (parent.offsetParent)	{
		parent=parent.offsetParent;
		x += parent.offsetLeft;
		y += parent.offsetTop;
	}
	
	var s = e.style;
	s.visibility = "visible";
	s.position='absolute';
	
/*	if (horizMenu && menuName.match(/###/)) {
		x -= w;	x += 4;	y -= 1; //first flyout underneath menu item
	}
	else {
		x-=7; y-=12;
	}
*/
	if (horizMenu && menuName.match(/###/)) {
		x -= w;	x += 4;	y -= 1; //first flyout underneath menu item
	}

	else {
		if (menuName.match(/###/)) {
			x-=7; y-= h; y+= 8;
		}
		else {
			x-=7; y-=12;		
		}
	}
	s.left = x  + "px";
	s.top = y + "px";
		
	if( !flyoutArray ) {
		flyoutArray = new Array;
	}
	flyoutArray[flyoutArray.length] = "" + subMenuName;
}

var helpWindow = null;
function openHelpWin(page) {
	url = "http://www.nantinet.co.uk/cmshelp/" + page + ".html";
	if (helpWindow) helpWindow.close();
	helpWindow = window.open(url,"subwnd","height=400,width=400,scrollbars");
	helpWindow.focus( );
}

