var subMenusOpen = new Array();
var mainMenusOpen = new Array();

function containsDOM (container, containee) {
  var isParent = false;
  do {
    if ((isParent = container == containee))
      break;
    containee = containee.parentNode;
  }
  while (containee != null);
  return isParent;
}

function checkMouseEnter (element, evt) {
  if (element.contains && evt.fromElement) {
    return !element.contains(evt.fromElement);
  }
  else if (evt.relatedTarget) {
    return !containsDOM(element, evt.relatedTarget);
  }
}

function checkMouseLeave (element, evt) {
  if (element.contains && evt.toElement) {
    return !element.contains(evt.toElement);
  }
  else if (evt.relatedTarget) {
    return !containsDOM(element, evt.relatedTarget);
  }
}

function menuClick(e)
{

	var tg = getTarget( e );
	
	//Check if we have children
	if( hasChildren( tg ) == false )
	{
		return;
	}
	
	imgObj = tg.previousSibling;
	imgObj.src = imagePath + 'freccia_on.gif';
	tg.onmouseover = null;
	tg.onmouseout = null;
	tg.className = 'menuOpen';
	
	while(tg.nodeName != 'LI') 
	{
		tg = tg.parentNode;
	}


	listOfNodes = tg.childNodes;
	for( i = 0 ; i < listOfNodes.length && listOfNodes[i].nodeName != 'UL' ; i++ )
	{
	}

	nextSib = listOfNodes[i];

	var currentSibStatus = nextSib.style.display;
	if( currentSibStatus == 'block' )
	{
		nextSib.style.display = 'none';
	}
	else
	{
		nextSib.style.display = 'block';
		//nextSib.className = 'currentSection';
	}
	checkClose( nextSib );
	setCurrentSection( nextSib );
	//fixIEBug();
return true;
}

function getCurrentMenus( el )
{
	pdiv = getParentTag( el, 'DIV' );
	// Check if the id of this div is in the array we use for closing sub menus
	if( document.menuList )
	{
		for( i = 0 ; i < menuList.length ; i++ )
		{
			if( menuList[i] == pdiv.id )
			{
				return subMenusOpen;
			}
		}
	}

	// We didn't find it in the sub list so send back the main list of menus open
	return mainMenusOpen;
}

function checkClose( menuItem )
{
	// We have 2 arrays of open menus. One for the main navigation + one for the other menus
	// This is to make sure we don't collapse the main menu if we click on a sub menu
	menusOpen = getCurrentMenus( menuItem );

	// If this is newly opened then check if we should be closing other items
	// The easiest way to do this is to check if the parent of this item is the last item
	// on the list of open menus
	var menuToClose = null;
	while( menusOpen.length != 0 && menuItem.parentNode.parentNode != menusOpen[menusOpen.length-1] )
	{
		menuToClose = menusOpen.pop();
		menuToClose.style.display = 'none';
		
		setMenuState( menuToClose, false );
	}

	
	// if we just closed ourselves then don't add the menu to the stack
	if( menuItem != menuToClose )
	{
		menusOpen.push( menuItem );
	}
}


function menuOver( e )
{

	var tg = getTarget( e );

	// We first need to check if this item has children or not
	if( hasChildren( tg ) )
	{
		// Get the depth of the menu item
		depth = getMenuDepth( tg );

		// Check we have a valid depth
		// Level 5 has no arrow
		if( depth > 0 && depth < 5 )
		{
			// We have the a which was clicked, our image should be the previous sibling
			imgObj = tg.previousSibling;
			imgObj.src = imagePath + 'freccia.gif';
		}
	}
	return true;
}

function hasChildren( el )
{
	while( el.nodeName != 'LI' )
	{
		el = el.parentNode;
	}
	
	// Now go down looking for UL
	children = el.childNodes;
	var childrenFound = false;
	for( i = 0 ; i < children.length ; i++ )
	{
		if( children[i].nodeName == 'UL' )
		{
			childrenFound = true;
			break;
		}
	}
	return childrenFound;
}

function getTarget( e )
{
	if(!e)
	{
		var e = window.event;
	}
	if( e.target )
	{
		var tg = e.target;
	}
	else 
	{
		if (e.srcElement)
		{
			var tg = e.srcElement;
		}
	}

	while (tg.nodeType != 1) // Safari GRRRRRRRRRR
	{
		tg = tg.parentNode;
	}

	return tg;
}

function menuOut( e )
{
	var tg = getTarget( e );

	imgObj = tg.previousSibling;
	imgObj.src = imagePath + 'freccia_trasp.gif';
	return true;
}

function getMenuDepth( tg )
{
	// The theory is that we just count ULs until we reach the top
	var level = 0;
	while( tg != null )
	{
		if( tg.nodeName == 'UL' )
		{
			level++;
		}
		tg = tg.parentNode;
	}
	return level;
}

function getParentTag( el, tag )
{
	// Make the compare case insensitive
	tag = tag.toLowerCase();
	
	// Move up one level to make sure we don't match the item sent
	el = el.parentNode;
	while( el != null && el.nodeName.toLowerCase() != tag )
	{
		el = el.parentNode;
	}
	return el;
}

function getChildTag( el, tag )
{
	var children = el.childNodes;
	tag = tag.toLowerCase();
	for( i = 0 ; i < children.length ; i++ )
	{
		if( children[i].nodeName.toLowerCase() == tag )
		{
			return children[i];
		}
	}
	return null;
}


function setCurrentSection( menuItem )
{
	// Use the list of open menus to track down which should be highlighted
	menusOpen = getCurrentMenus( menuItem );

	// If there are no open menus then set the top item
	if( menusOpen.length == 0 )
	{
		
	}
	else
	{
		for( i = 0 ; i < menusOpen.length ; i++ )
		{
			// Just turn off the class for all of these
			menusOpen[i].className = '';
		}
		menusOpen[i-1].className = 'currentSection';
	}
}

function setMenuState( el, state )
{
	// We need to get the <a> element under this
	
	// We go up from this UL first to find the LI
	while( el != null && el.nodeName != 'LI' )
	{
		el = el.parentNode;
	}
	if( el != null )
	{
		// Now go through the children of this el to find the <A> tag
		children = el.childNodes;
		var childFound = false;
		for( i = 0 ; i < children.length ; i++ )
		{
			if( children[i].nodeName == 'A' )
			{
				childFound = true;
				tg = children[i];
				break;
			}
		}
		if( childFound == false )
		{
			return;
		}
	}
	else
	{
		return;
	}
	
	if( state == true )
	{
		// Switch on the arrow + disable mouseover + mouseout
	// Get the dept of the menu item
	var depth = getMenuDepth( tg );

	// Check we have a valid depth
	// Level 5 has no arrow
	if( depth > 0 && depth < 5 )
	{
		// We have the a which was clicked, our image should be the previous sibling
		imgObj = tg.previousSibling;
		imgObj.src = imagePath + 'freccia_on.gif';
	}
	tg.onmouseover = null;
	tg.onmouseout = null;
	tg.className = 'menuOpen';
	}
	else
	{
		tg.onmouseover = menuOver;
		tg.onmouseout = menuOut;
		tg.className = '';
		imgObj = tg.previousSibling;
		imgObj.src = imagePath + 'freccia_trasp.gif';
	}
}

/* This is different from setMenuState */
function highlightCurrentPage( el )
{
	// What we have been passed should be the LI
	// So go down to find the <A> we are interested in
	var tg = getChildTag( el, 'A' );

	// Switch on the arrow + disable mouseover + mouseout
	// Get the dept of the menu item
	var depth = getMenuDepth( el );

	// Check we have a valid depth
	if( depth > 0 && depth <=5 )
	{
		// We have the a which was clicked, our image should be the previous sibling
		imgObj = tg.previousSibling;
		imgObj.src = imagePath + 'freccia_arancio.gif';
	}
	tg.onmouseover = null;
	tg.onmouseout = null;

	// We have to set the class change on the <li>
	// this allows us to control the :hover of the <a> tag	
	el.className = 'currentPage';
}

function openMenuItem( menuID )
{
	var tg = document.getElementById( menuID );
	
	// Store it away for later
	var tgStore = tg;
	
	// First set the arrow of where we are
	highlightCurrentPage( tg );
	
	// Keep going upwards opening every UL we find
	var newMenus = new Array();
	
	while( tg != null )
	{
		if( tg.nodeName == 'UL' )
		{
			tg.style.display = 'block';
			setMenuState( tg, true );
			newMenus.push( tg );
		}
		tg = tg.parentNode;
	}
	
	// Put the menus we opened into menusOpen
	// We use mainMenusOpen here as this must be for the main menu of the page
	for( i = (newMenus.length - 2) ; i >= 0 ; i-- )
	{
		mainMenusOpen.push( newMenus[i] );
	}
	setCurrentSection( tgStore );	
}

function openMenu()
{
	var pageUrl= location.href;
	if( pageUrl.indexOf( 'id=' ) != -1 )
	{
		id_pos = pageUrl.indexOf( 'id=' ) + "id=".length;
		// Get the number out
		pnumber = pageUrl.substring( id_pos, pageUrl.length );
		pid = "p" + eval( pnumber );
	}
	else
	{
		pid = null;
	}
	if( pid != null )
	{
		openMenuItem( pid );
	}
}
