/* +--------------------------------------------------------------+
 * | This file is part of the web application framework "ISTANTE" |
 * | Copyright (c) 2005 Claudio Cicali <claudio@cicali.org>       |
 * |                and Renomo s.r.l <info@renomo.com>            |
 * | All rights reserved                                          |
 * +--------------------------------------------------------------+
 * | Author: Claudio Cicali claudio@cicali.org                    |
 * +--------------------------------------------------------------+
 */

var sfHover = function() {
  if (!document.getElementById("nav_container"))
    return;
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

var mcAccessible = function() {
  if (!document.getElementById("nav_container"))
    return;
  
	var mcEls = document.getElementById("nav").getElementsByTagName("A");
	for (var i=0; i<mcEls.length; i++) {
		mcEls[i].onfocus=function() {
			this.className+=(this.className.length>0? " ": "") + "sffocus"; //a:focus
			this.parentNode.className+=(this.parentNode.className.length>0? " ": "") + "sfhover"; //li < a:focus
			if(this.parentNode.parentNode.parentNode.nodeName == "LI") {
				this.parentNode.parentNode.parentNode.className+=(this.parentNode.parentNode.parentNode.className.length>0? " ": "") + "sfhover"; //li < ul < li < a:focus
				if(this.parentNode.parentNode.parentNode.parentNode.parentNode.nodeName == "LI") {
					this.parentNode.parentNode.parentNode.parentNode.parentNode.className+=(this.parentNode.parentNode.parentNode.parentNode.parentNode.className.length>0? " ": "") + "sfhover"; //li < ul < li < ul < li < a:focus
				}
			}
		}
		mcEls[i].onblur=function() {
			this.className=this.className.replace(new RegExp("( ?|^)sffocus\\b"), "");
			this.parentNode.className=this.parentNode.className.replace(new RegExp("( ?|^)sfhover\\b"), "");
			if(this.parentNode.parentNode.parentNode.nodeName == "LI") {
				this.parentNode.parentNode.parentNode.className=this.parentNode.parentNode.parentNode.className.replace(new RegExp("( ?|^)sfhover\\b"), "");
				if(this.parentNode.parentNode.parentNode.parentNode.parentNode.nodeName == "LI") {
					this.parentNode.parentNode.parentNode.parentNode.parentNode.className=this.parentNode.parentNode.parentNode.parentNode.parentNode.className.replace(new RegExp("( ?|^)sfhover\\b"), "");
				}
			}
		}
	}
}

add_event(window, "load", mcAccessible);

function get_ul_maxsize(ul)
{
  var menuSize=0;
  c = ul.childNodes;

  for (var i=0; i < c.length; i++)
  {
    if (c[i].nodeType == 1 & c[i].tagName == 'LI')
    { 
      items = c[i].childNodes;
      for (n=0; n < items.length; n++)
      {
        if (items[n].nodeType == 1 & items[n].tagName == 'A')
        {
          if (items[n].offsetWidth > menuSize)
            menuSize = items[n].offsetWidth;
        }
      }
    }
  }
  return menuSize;
}

function set_ul_size(ul, menuSize)
{
  ul.style.width = menuSize + "px";
  first_level = ul.childNodes;          
  for (var xxx=0; xxx < first_level.length; xxx++) 
  {
    if (first_level[xxx].nodeType == 1 && 
        first_level[xxx].tagName == 'LI')
    {
      first_level[xxx].style.width = menuSize + "px";
      items = first_level[xxx].childNodes;
      for (var xxxx=0; xxxx < items.length; xxxx++) 
      {
        // This is normal menu item, not a submenu
        if (items[xxxx].nodeType == 1 && 
            items[xxxx].tagName == 'A')
          items[xxxx].style.width = menuSize;
      }
    }
  }
}

function find_submenus(ul)
{
  c = ul.childNodes;
  submenus = new Array(0);
  for (var i=0; i < c.length; i++)
  {
    if (c[i].nodeType == 1 & c[i].tagName == 'LI')
    { 
      items = c[i].childNodes;
      for (var n=0; n < items.length; n++)
        if (items[n].nodeType == 1 & items[n].tagName == 'UL')
          submenus.push(items[n]);
    }
  }
  return submenus;
}

var set_menu = function () {

  if (!document.getElementById("nav_container"))
    return;
  
  document.getElementById("nav_container").style.height =document.getElementById("nav").offsetHeight + "px"; 
  
	var top = document.getElementById("nav").childNodes;
  
	for (var x=0; x < top.length; x++) 
  {
    // They should always be LIs, but sometimes we get spurious text nodes...
    if (top[x].nodeType == 1 && 
        top[x].tagName == 'LI')
    {
      for (var xx=0; xx < top[x].childNodes.length; xx++) 
      {
        if (top[x].childNodes[xx].nodeType == 1 && 
            top[x].childNodes[xx].tagName == 'UL')
        {
          // Set the size of the first level menu
          menuSize = get_ul_maxsize(top[x].childNodes[xx]);
          set_ul_size(top[x].childNodes[xx], menuSize);

          // Find eventual submenus
          submenus = find_submenus(top[x].childNodes[xx]);
          
          for (var i=0; i < submenus.length; i++)
          {
            menuSize2 = get_ul_maxsize(submenus[i]);
            set_ul_size(submenus[i], menuSize2);
            submenus[i].style.marginLeft = menuSize + "px";
          }
        }
        else
        {
          // Check if it is an actuator
          if (top[x].childNodes[xx].nodeType == 1 && 
              top[x].childNodes[xx].tagName == 'A' &&
              top[x].childNodes[xx].className == "actuator")  
          {
            act = top[x].childNodes[xx];

            if (act.accessKey)
            {
              act.innerHTML =  act.innerHTML.replace(act.accessKey, "<span style='text-decoration: underline'>" + act.accessKey + "</span>");
            }
          }
        }
      }     
    }
  }
  
  return;
}

add_event(window, "load", set_menu)
