
// Determine browser and version.

function Browser() {

  var ua, s, i;

  this.isIE    = false;  // Internet Explorer
  this.isNS    = false;  // Netscape
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

var browser = new Browser();

// For IE, adjust menu bar styling.

if (browser.isIE) {
  document.styleSheets[document.styleSheets.length - 1].addRule("#menuBar", "padding-top:4px");
  document.styleSheets[document.styleSheets.length - 1].addRule("#menuBar", "padding-bottom:3px");
}

// Global variable for tracking the currently active button, Menu Item and Head Menu Left.

var activeButton = null;
var activeItem = null;
var activeHeadMenuLeft = null;
var nomePai = ''; //Vai conter o nome do menu Pai para se poder juntar a "href"s relativos a menus filhos
//De cada vez, como só há um item (ou menu) activo, também só há um Pai a quem esse item possa pertencer...

// Capture mouse clicks on the page so any active button can be
// deactivated.

if (browser.isIE)
  document.onmousedown = pageMousedown;
if (browser.isNS)
  document.addEventListener("mousedown", pageMousedown, true);

function pageMousedown(event) {

  var el;

  // If there is no active menu, exit.
  if (!activeButton)
    return;
  
  // Find the element that was clicked on.
  if (browser.isIE)
    el = window.event.srcElement;
  if (browser.isNS)
    el = (event.target.className ? event.target : event.target.parentNode);

  // If the active button was clicked on, exit.
  if (el == activeButton)
    return;

  // If the element clicked on was not a menu button or item Head, close the active menu.
  if (el.className != "menuButton"  && el.className != "menuItemOver" && el.className != "headMenuLeftOver" &&
      el.className != "menuItemSep" && el.className != "menu")
    resetButton(activeButton);
}

function buttonClick(button, menuName) {

  // Blur focus from the link to remove that annoying outline.
  button.blur();

  // Associate the named menu to this button if not already done.
  if (!button.menu)
    button.menu = document.getElementById(menuName);

  //Aplica-se o efeito de transição, definido na CSS, ao Menu que abre, só para o IE
  if (browser.isIE) {
	for(nFt=0; nFt<=1; nFt++) {
	  button.menu.filters[nFt].Apply();
	  button.menu.filters[nFt].Play();
	}
  }

//A variavel Global menuName vai ser aqui fixada
  nomePai = menuName;

  // Reset the currently active button, if any.

  if (activeButton && activeButton != button)
    resetButton(activeButton);

  // Toggle the button's state.

  if (button.isDepressed)
    resetButton(button);
  else
    depressButton(button);

  return false;
}

function buttonMouseover(button, menuName) {

  // If any other button menu button or Item is active, deactivate it and activate this one.
  // Note: if this button has no menu, leave the active menu alone.
  if (activeButton && activeButton != button) {
    resetButton(activeButton);
    if (menuName)
	  buttonClick(button, menuName);
  }
}

function MenuLeft(button, menuName) {

  // If any other button menu is active, deactivate it and activate this one.
  // Note1: if this button has no menu, change the appearence (class)
  // Note2: if this menu is a Left one, leave this menu alone
  if (activeItem && activeItem != button) {
    // Restore the button's style class.
    activeItem.className = "menuItem";
	
	// Set button state; activeItems never have right menus
    activeItem = null;
  }
  if (activeHeadMenuLeft && activeHeadMenuLeft != button) {
    //If the mouse stays on the same layer (menu), clear right menus and restore class
    if (getPageOffsetLeft(activeHeadMenuLeft) == getPageOffsetLeft(button)) {
	  // Restore the button's style class.
      activeHeadMenuLeft.className = "menuItem";
	  // Hide the button's menu
      activeHeadMenuLeft.menu.style.visibility = "hidden";
	}
  } 
	  
  // Change the Item menu style class to make it look like mouse over
  // and distinguish beetwen those who have a right menu or not
  if (menuName) {
    button.className = "headMenuLeftOver";
	activeHeadMenuLeft = button;
  } else {
    button.className = "menuItemOver";
	activeItem = button;
  }
  // Associate, if it exists, the named menu to this button if not already done.
  if (!button.menu && menuName) 
    button.menu = document.getElementById(menuName);
	
  if (menuName) {
    // For IE, set an explicit width on the first menu item. This will
    // cause link hovers to work on all the menu's items even when the
    // cursor is not over the link's text.
    if (browser.isIE && !button.menu.firstChild.style.width) {
      w = button.menu.firstChild.offsetWidth;
      button.menu.firstChild.style.width = w + "px";
      dw = button.menu.firstChild.offsetWidth - w;
      w -= dw;
      button.menu.firstChild.style.width = w + "px";
    }
	// Position the associated "drop down" menu on the right side of the button and
    // show it. Note that the position must be adjusted according to
    // browser, styling and positioning.
    x = getPageOffsetLeft(button) + button.offsetWidth;
  	y = getPageOffsetTop(button);
  	if (browser.isIE || (browser.isNS && browser.version >= 6.1))
      x += 2;
  	if (browser.isNS && browser.version < 6.1) {
      x--;
      y--;
    }
  	button.menu.style.left = x + "px";
  	button.menu.style.top  = y + "px";
	//Aplica-se o efeito de transição, definido na CSS, ao Menu que abre, só para o IE
	if (browser.isIE) {
	  for(nFt=0; nFt<=1; nFt++) {
		button.menu.filters[nFt].Apply();
		button.menu.filters[nFt].Play();
	  }
	}
	//Et voilá:
  	button.menu.style.visibility = "visible";
  }
}

function depressButton(button) {

  var w, dw, x, y;

  // Change the button's style class to make it look like it's depressed.

  button.className = "menuButtonActive";

  // For IE, set an explicit width on the first menu item. This will
  // cause link hovers to work on all the menu's items even when the
  // cursor is not over the link's text.

  if (browser.isIE && !button.menu.firstChild.style.width) {
    w = button.menu.firstChild.offsetWidth;
    button.menu.firstChild.style.width = w + "px";
    dw = button.menu.firstChild.offsetWidth - w;
    w -= dw;
    button.menu.firstChild.style.width = w + "px";
  }

  // Position the associated drop down menu under the button and
  // show it. Note that the position must be adjusted according to
  // browser, styling and positioning.

  x = getPageOffsetLeft(button);
  y = getPageOffsetTop(button) + button.offsetHeight;
  if (browser.isIE || (browser.isNS && browser.version >= 6.1))
    y += 2;
  if (browser.isNS && browser.version < 6.1) {
    x--;
    y--;
  }
  button.menu.style.left = x + "px";
  button.menu.style.top  = y + "px";
  button.menu.style.visibility = "visible";

  // Set button state and let the world know which button is
  // active.

  button.isDepressed = true;
  activeButton = button;
}

function resetButton(button) {

  // Restore the button's...
  button.className = "menuButton";
  
  //...and Menu Item style class.
  if(activeItem) {
    activeItem.className = "menuItem";
	activeItem = null;
  }
  if(activeHeadMenuLeft) {
    //1º escondem-se possíveis menus esquerdos
    activeHeadMenuLeft.menu.style.visibility = "hidden";
	// and restore the old class
    activeHeadMenuLeft.className = "menuItem";
	activeHeadMenuLeft = null;
  }
  
  // Hide the button's menu.
  if (button.menu)
    button.menu.style.visibility = "hidden";

  // Set button state and clear active menu global.
  button.isDepressed = false;
  activeButton = null; 
}

function getPageOffsetLeft(el) {

  // Return the true x coordinate of an element relative to the page.

  return el.offsetLeft + (el.offsetParent ? getPageOffsetLeft(el.offsetParent) : 0);
}

function getPageOffsetTop(el) {

  // Return the true y coordinate of an element relative to the page.

  return el.offsetTop + (el.offsetParent ? getPageOffsetTop(el.offsetParent) : 0);
}
