var timer,menuAtt="",menuSubAtt="";

var debug;

function init()
{
 var menu=document.getElementById("navmenu");

 var voci=menu.getElementsByTagName("a");
 for(var n=0; n<voci.length; n++)
  if(voci[n].addEventListener) { voci[n].addEventListener("mouseover",entra,false); voci[n].addEventListener("mouseout",esci,false); }
  else { voci[n].onmouseover=entra; voci[n].onmouseout=esci; }

 menu=document.getElementById("navnation");
 if(menu)
 {
  voci=menu.getElementsByTagName("a");
  for(var n=0; n<voci.length; n++)
   if(voci[n].addEventListener) { voci[n].addEventListener("mouseover",entra,false); voci[n].addEventListener("mouseout",esci,false); }
   else { voci[n].onmouseover=entra; voci[n].onmouseout=esci; }
 }
 debug=document.getElementById("debug").firstChild;
}

function mostra(id)
{
 var m=document.getElementById(id);
 if(!m) return false;
 if(id=="Rproducts")
 {
  menuAtt="Sproducts";
  menuSubAtt=id;
 }
 else
 {
  if(menuAtt!=m.id) nascondi();
  menuAtt=id;
  nascondiSub();
 }
 if(timer) { clearTimeout(timer); timer=false; }
 m.style.visibility="visible";
 return true;
}

function nascondi()
{
 if(!menuAtt) return false;
 var m=document.getElementById(menuAtt);
 if(!m) return false;
 if(timer) { clearTimeout(timer); timer=false; }
 nascondiSub();
 menuAtt="";
 m.style.visibility="hidden";
 return true;
}

function nascondiSub()
{
 if(menuSubAtt)
 {
  m=document.getElementById(menuSubAtt);
  if(!m) return false;
  menuSubAtt="";
  m.style.visibility="hidden";
 }
}

function entra(e)
{
 var n,li,trovato=false;
 if(!e) var e=window.event;

 if(e.target) li=e.target;
 else if(e.srcElement) li=e.srcElement;
 if(li.nodeType==3) li=li.parentNode;
 while(li.nodeName.toLowerCase()!="body" && li.nodeName!="html:body")
 {
  if(li.nodeType==1 && (li.nodeName.toLowerCase()=="li" || li.nodeName=="html:li")) { trovato=true; break; }
  li=li.parentNode;
 }
 if(!trovato) return true;
 if(li.parentNode.id=="navmenu" || li.parentNode.id=="navnation" || (li.firstChild.href && li.firstChild.href.search(/\/products\/index.php$/)!=-1))
 {
  try { n=li.getElementsByTagName("ul").item(0); } catch(e) { return true; }
  if(n && n.id)
  {
   if(li.firstChild.href && li.firstChild.href.search(/\/products\/index.php$/)!=-1) mostra(li.parentNode.id);
   mostra(n.id);
   return false;
  }
 }
 else if(li.parentNode.id) { mostra(li.parentNode.id); return false; }
 return true;
}

function esci(e)
{
 timer=setTimeout("nascondi()",800);
}
