//Global variables.
var ajw_selectsHidden;
var ajw_heightDiff = 0;
var ajw_widthDiff = 0;

function MouseEvent(eventType, menuItem, parent, menuIsVertical, newCSSClass, oldCSSClass, menuName, fadeSubMenu, menuTimeOut, selectedName)
{ 
	if (parent != null && parent != undefined)
	{
		switch(eventType)
		{
			case 'MouseOver':
				stopTick(menuName);
				var currentClassName = menuItem.className;
				if (currentClassName.indexOf("MouseClick") == -1)
					menuItem.className = newCSSClass;
				//Whack in the name of this control to the method call...
				closeSubMenus(menuItem, menuName);
				
				//Display any subMenu.
				var subMenuID = menuItem.id + '_subMenu';
				var subMenu = document.getElementById(subMenuID);
				if (subMenu != null)
				{
					// make the child menu visible and specify that its position is specified in absolute coordinates
					subMenu.style.display = 'block';
					subMenu.style.position = 'absolute';	
					subMenu.style.zIndex = '999';
					parent.style.zIndex = '998';
			
					if (menuIsVertical)
					{
						// Set the child menu's left and top attributes according to the menu's offsets
						subMenu.style.left = getAscendingLefts(parent) + parent.offsetWidth + 'px';
					    subMenu.style.top = getAscendingTops(menuItem) - parseInt(subMenu.cellSpacing) + 'px';
					    
					    if ( ajw_vericalScrollRequired(subMenu) )
                        {
                            var newTop = parseInt(subMenu.style.top.substr(0, (subMenu.style.top.length - 2))) - parseInt(ajw_heightDiff) - 5;
                            subMenu.style.top = newTop + 'px';
                            
                            //Reset variable.
                            ajw_heightDiff = 0;
                        }

                        if ( ajw_horizontalScrollRequired(subMenu) )
                        {
                            var newLeft = parseInt(subMenu.style.left.substr(0, (subMenu.style.left.length - 2))) - parseInt(ajw_widthDiff) - 5;
                            subMenu.style.left = newLeft + 'px';
                            
                            // Reset variable
                            ajw_widthDiff = 0;
                        }
					}
					else
					{
						subMenu.style.left = getAscendingLefts(menuItem) + 'px';
						subMenu.style.top = getAscendingTops(parent) + parent.offsetHeight + 'px';
						
						if ( ajw_vericalScrollRequired(subMenu) )
                        {
                            var newTop = parseInt(subMenu.style.top.substr(0, (subMenu.style.top.length - 2))) - parseInt(ajw_heightDiff) - 5;
                            subMenu.style.top = newTop + 'px';
                            
                            //Reset variable.
                            ajw_heightDiff = 0;
                        }
                        
                        if ( ajw_horizontalScrollRequired(subMenu) )
                        {
                            var newLeft = parseInt(subMenu.style.left.substr(0, (subMenu.style.left.length - 2))) - parseInt(ajw_widthDiff) - 5;
                            subMenu.style.left = newLeft + 'px';
                            
                            // Reset variable
                            ajw_widthDiff = 0;
                        }
					}
					if (fadeSubMenu == 'True')
						fadeIn(0, subMenu.id, DEFAULT_INTERVAL);
				}
				break;
			case 'MouseOut':
				var currentClassName = menuItem.className;
				if (currentClassName.indexOf("MouseClick") == -1)
				{
					menuItem.className = newCSSClass;
					doTick(menuName, menuTimeOut);
				}
				break;
			case 'MouseDown':
				var selectedItem = document.getElementById(menuName + "_SelectedItem");
				selectedItem.value = selectedName;
				if (newCSSClass != null && newCSSClass != undefined && newCSSClass != '')
				{
					var tds = document.getElementsByTagName("td");
					for (var i = 0; i < tds.length; i++)
					{
						var prefix = tds[i].id.substr(0,menuName.length);
						if (prefix == menuName)
							if (tds[i].id == menuItem.id)
							{
								tds[i].className = newCSSClass;
							}
							else
							{
								tds[i].className = oldCSSClass;
							}	
					}
				}
				break;
		}
	}
}

//Close the sub menus.
function closeSubMenus(parent, menuName)
{
	// Hide **all** lower-ordered submenus
	var tables = document.getElementsByTagName("table");
	for (var i = 0; i < tables.length; i++)
	{
		var prefix = tables[i].id.substr(0,menuName.length);
		if (prefix == menuName)
			if (tables[i].id.length > parent.id.length)
			{
				document.getElementById(tables[i].id).style.display = 'none';
			}	
	}
}

//Timing stuff for closing the menus...
var ajw_clockValue = [0];
var ajw_ticker = [];
function doTick(menuName, menuTimeOut)
{
	if (ajw_clockValue[menuName] > 1)
	{
		stopTick(menuName);
		closeSubMenus(document.getElementById(menuName), menuName);
	}
	else
	{
		ajw_clockValue[menuName]++;
		ajw_ticker[menuName] = setTimeout('doTick("' + menuName + '")',parseInt(menuTimeOut));
	}
}

function stopTick(menuName)
{
	ajw_clockValue[menuName] = 0;
	clearTimeout(ajw_ticker[menuName]);
}

//Ascertains the correct left position...
function getAscendingLefts(elem)
{
	if (elem == null)
		return 0;
	else
		return elem.offsetLeft + getAscendingLefts(elem.offsetParent);
}

//Ascertains the correct top position...
function getAscendingTops(elem)
{
	if (elem == null)
		return 0;
	else
		return elem.offsetTop + getAscendingTops(elem.offsetParent);
}

// Work out whether the pop up window when opened will be taller than the screen available
function ajw_vericalScrollRequired(subMenu)
{
    var scrollY, screenHeight;

    if (document.all) 
    {
        if (!document.documentElement.scrollTop)
            scrollY = document.body.scrollTop;
        else
            scrollY = document.documentElement.scrollTop;
    }
    else
        scrollY = window.pageYOffset;

    if ( document.all )
       screenHeight = document.documentElement.clientHeight;
    else
       screenHeight = window.innerHeight;

    var bottomPos = parseInt(subMenu.style.top.substr(0, (subMenu.style.top.length - 2))) + parseInt(subMenu.clientHeight);
    var totalHeight = parseInt(scrollY) + parseInt(screenHeight);

    //Non IE shift.
    if (!document.all)
    {
        bottomPos = bottomPos + 10;
    }

    if ( bottomPos > totalHeight )
    {
        ajw_heightDiff = parseInt(bottomPos) - parseInt(totalHeight);
        return true;
    }
    else
        return false;
}

// Work out whether the pop up window when opened will be wider than the screen available
function ajw_horizontalScrollRequired(subMenu)
{
    var scrollX, screenWidth;
 
    if (document.all) 
    {
        if (!document.documentElement.scrollLeft)
            scrollX = document.body.scrollLeft;
        else
            scrollX = document.documentElement.scrollLeft;
    }
    else
        scrollX = window.pageXOffset;

    if ( document.all )
       screenWidth = document.documentElement.clientWidth;
    else
       screenWidth = window.innerWidth;

    var rightPos = parseInt(subMenu.style.left.substr(0, (subMenu.style.left.length - subMenu.style.left.indexOf("px")) + 1)) + parseInt(subMenu.clientWidth);
    var totalWidth = parseInt(scrollX) + parseInt(screenWidth);

    if ( rightPos > totalWidth )
    {
        ajw_widthDiff = parseInt(rightPos) - parseInt(totalWidth);
        return true;
    }
    else
        return false;
}

//Simply gets the actual number (nn) from an nnpx style attribute...
function extractNumber(styleProperty)
{
	var i = styleProperty.indexOf('px');
	var number = styleProperty.substr(styleProperty, 1, i)
	return parseInt(number);
}

//Menu fading code...
var DEFAULT_INTERVAL = 5;

function fadeIn(opac,menuItemID,interval) 
{	
	if(opac != 100 && interval != 0)
	{
		opac+=Math.min(100,interval);
		var div = document.getElementById(menuItemID);
		div.filters.alpha.opacity = opac;
		
		div.style.opacity = (opacity / 100);
        div.style.MozOpacity = (opacity / 100);
        div.style.KhtmlOpacity = (opacity / 100);
        div.style.filter = 'alpha(opacity=' + opacity + ')';
		
		setTimeout('fadeIn('+opac+',""'+ div.id+'"",'+interval+')',0);
	}
}

function fadeOut(opac,menuItemID,interval) 
{	
	if(opac != 0 && interval != 0)
	{
		opac-=Math.max(0,interval);
		var div = document.getElementById(menuItemID);
		div.filters.alpha.opacity = opac;
		
	    div.style.opacity = (opacity / 100);
        div.style.MozOpacity = (opacity / 100);
        div.style.KhtmlOpacity = (opacity / 100);
        div.style.filter = 'alpha(opacity=' + opacity + ')';
		
		setTimeout('fadeOut('+opac+',""'+ div.id+'"",'+interval+')',0);
	}
}