/*
*       javascript multilevel menu developed by Matteo Bicocchi
*       © 2002-2005 Open Lab srl, Matteo Bicocchi
*					www.open-lab.com - info@open-lab.com
*       version 3.2     
*       free to use - please don't cut this lines
*       tested on: Explorer, Mozilla, Netscape and Opera for PC
*                  Mozilla, Netscape and Safari for Mac Os X

*		This .js must be used with the 'mbMenu.css' file
*		
*		to be impruved:
			positioning of elements that go out of the window
*/


//browser detection
var isOpera=false;
 if(navigator.appName == "Netscape")var isNetscape = true;
 if(navigator.appName == "Microsoft Internet Explorer") var isExplorer = true;
 var useragent= navigator.userAgent.toLowerCase();
 if(useragent.indexOf("opera")>-1) isOpera = true;
  //alert(useragent);

 if (isOpera) alert('Opera not supported    ' + useragent+"    "+isOpera+"   "+useragent.indexOf("opera"))

//OS detection
 if (navigator.appVersion.indexOf("Win")!=-1) var isWin = true;
 if (navigator.appVersion.indexOf("Mac")!=-1) var isMac = true;
 if (navigator.appVersion.indexOf("X11")!=-1) var isUnix = true;
 if (navigator.appVersion.indexOf("Linux")!=-1) var isLinux = true;

//Version detection
  var is_major = parseInt(navigator.appVersion);
  var is_minor = parseFloat(navigator.appVersion);
  var version= navigator.appVersion.substring(0,1);


// inizializing variables

var divId;
var ContentLine;
var contentMenu;
var theLayer;
var trueX;
var trueY;
var windowHeight;
var winWidth,winHeight;
var zMax;
var menuDiv;
var actualMenu="";

var submenuOnLeft=false;
var submenuOnTop=false;
var activeOnClick=false;
var menuWidth=120;
var menuZIndex=2000;
var contentLineAlign="left";
var separatorHeight=0; // separator height
var lineHeight = 0;
var distanceY=0; // menu top adjust
var distanceX=0; // menu left adjust
var subMenuMarginLeft=0; // subMenu left adjust
var subMenuMarginTop=0;  // subMenu top adjust
var imagesUrl="/local/parts/part_horz_menu/img/";
var actualId="";
var isTxt;
var arrowImg= "menuArrow.gif";
var documentLoaded=false;
//-----------------link the right css class to the normal and the over state for the menubar---
var barMenuClass="barElement";
var barMenuOverClass="barElementOver";

//-----------------define if you have to click for the first open-------------------------

var menuOnClick=false;
var show= (menuOnClick)? false : true;

//-----------------if you attach the menu to a textual link define the normal and the over style---

var rollOverOnBar=true;
var whichMenuBar;

//-----------------define if you have images or webdings in menu lines-------------------

var hasImg=true;
var hasTxtImg=false;

function createImg(imgDef){
  if (hasImg){
    if (!imgDef ||(isNetscape && isWin)){
        imgString="<td style='width:16' width='16' class='imgSeparator'><img src='"+imagesUrl+"blank.gif' class='img' border='0' width='16' height='16'></td>";
    }else{
    imgString="<td style='width:16' width='16' class='imgSeparator'>"+(!hasTxtImg ? "<img src='"+imagesUrl+imgDef+"' class='img' border='0' width='16' height='16'>":"<font class='imgFontFamily'>"+imgDef+"</font>")+"</td>";
    }
  }else{
    imgString="";
  }

}


//-----------------recursive search for element----------------------------------

function findDiv(theEl){
  while (!theEl.name) {
  theEl=isNetscape ? theEl.parentNode: theEl.parentElement;
  }
  return theEl.id;
}

//-----------------create elements-------------------------------------------

function createMenuDiv (menuId, where){
  var menuDiv = document.createElement("DIV");
  menuDiv.style.position = "absolute";
  menuDiv.style.visibility = "hidden";
  menuDiv.id = menuId;
  menuDiv.name = menuId;
  menuDiv.style.width= menuWidth;
  menuDiv.style.zindex = menuZIndex;
  document.body.appendChild(menuDiv);
  actualMenu = menuDiv.id;
}

function createSubMenuDiv (subMenuId, relativeTo){
  subMenuDiv = document.createElement("DIV");
  subMenuDiv.style.position = "absolute";
  subMenuDiv.id = subMenuId;
  subMenuDiv.name = subMenuId;
  subMenuDiv.style.width= menuWidth;
  subMenuDiv.style.zindex = zMax;
  subMenuDiv.style.left= parseFloat(document.getElementById(relativeTo).offsetWidth+subMenuMarginLeft)+"px";
  subMenuDiv.style.marginTop= -(parseFloat(document.getElementById(relativeTo).offsetHeight+subMenuMarginTop))+"px";
  document.getElementById(relativeTo).appendChild(subMenuDiv);
  
    targetWidth= document.getElementById(relativeTo).offsetWidth;
    windowWidth = getWindowDimensionW();
    windowHeight = getWindowDimensionH();
    
    if (getAbsoluteLeft(subMenuDiv)>=(windowWidth-targetWidth) ){ // || submenuOnLeft
      subMenuDiv.style.left= parseFloat(-document.getElementById(relativeTo).offsetWidth-subMenuMarginLeft)+"px";
      submenuOnLeft=true;
    }
    if (submenuOnTop){
      subMenuDiv.style.marginTop= -(parseFloat(-document.getElementById(subMenuId).offsetHeight+document.getElementById(relativeTo).offsetHeight))+"px";
      submenuOnTop=true;
    }
    
    theEl= document.getElementById(relativeTo);
    theMenuToHide= document.getElementById(findDiv(theEl)).menuToHide;

    if (theMenuToHide){
     removeMenu(theMenuToHide);
    }
    document.getElementById(findDiv(theEl)).menuToHide=subMenuId;
}

// create the label of the menu

function createContentLineTitle (desc){
    contentLine = "<table width='100%' class='menuTitle' cellspacing='0' border='0'>\n<tr>\n<td nowrap >  "+desc+" </td>\n</tr>\n</table>";
  return contentLine;
}

// create the submenu line

function createSubmenuLine (desc, submenu,urlNodo, imgDef){

  createImg(imgDef);
  
  ContentLine="<div width='100%' id='"+desc+"_f' onMouseOver=\"showSubMenu("+submenu+",'"+submenu+"',this.id); document.getElementById('"+submenu+"_sml' ).className='subMenuOver' \" ><table style=\"cursor:pointer\" width='100%' id='"+submenu+"_sml' height='"+lineHeight+"' cellspacing='0' border='0' class='subMenu' onMouseOver=\"this.className='subMenuOver '\" onMouseDown=\"self.location.href='"+urlNodo+"'\"><tr>"+imgString+"<td name='"+desc+"_td' align='"+contentLineAlign+"' valign='middle' nowrap>  "+desc+" </td><td align='right' name='"+desc+"_td' ><img class='subMenuArrowww' src='"+imagesUrl+"blank.gif' class='img' border='0' width='16' height='16'></td></tr></table></div>";
  return ContentLine;
}

// create the menu line

function createContentLine (desc, lineUrl, target, contentTitle, script,imgDef){
  if (!target) target="document";
  if (target=="_parent") target="document.parent";
  if (target=="_self") target="document.self";
  
  createImg(imgDef);
  if(!script){
  if (contentTitle) contentTitle="title='"+contentTitle+"'"; else contentTitle=""
  ContentLine="<a href='#' onMouseDown=\""+target+".location.href='"+lineUrl+"'\""+contentTitle+"><table class='menu ' width='100%' height='"+lineHeight+"' cellspacing='0' border='0'  onMouseOver=\"hideSubMenu(this); this.className='menuOver '\" onMouseOut=\"this.className='menu '\"><tr>"+imgString+"<td nowrap align='"+contentLineAlign+"' valign='middle' nowrap>  "+desc+" </td></tr></table></a>";
  }else{
  ContentLine="<a href='#' onMouseDown=\" "+script+"\" "+contentTitle+"><table width='100%' height='"+lineHeight+"' cellspacing='0' border='0' class='menu' onMouseOver=\"hideSubMenu(this);this.className='menuOver '\" onMouseOut=\"this.className='menu '\"><tr>"+imgString+"<td nowrap align='"+contentLineAlign+"' valign='middle' nowrap>  "+desc+" </td></tr></table></a>";
  }
  return ContentLine;
}

// create the menu line disabled

function createContentLineDisabled (desc,imgDef){
  
  createImg(imgDef);
  ContentLine="<table class='menuDisabled  ' width='100%' cellspacing='0' border='0'><tr>"+imgString+"<td align='"+contentLineAlign+"' nowrap>  "+desc+" </td></tr></table>";
  return ContentLine;
}

// create the separator line

function createSeparator (){
  ContentLine="<table width='100%' cellspacing='0' cellpadding='0' border='0'><tr><td align='"+contentLineAlign+"' class='separator '><img src='../%22+imagesUrl+%22blank.gif' width='0' height='"+separatorHeight+"'></td></tr></table>";
  return ContentLine;
}

// create the menu

function createContent (wich, divId){
  var iframeHeight=0;
  var h=0;
    contentMenu="<div id='table1"+divId+"' class='menuContainer' style='position:absolute; width:"+menuWidth+"px; height:0 px; z-index:1000; border: 0px none'>";
 
  //if is explorer and Windows generate an iframe under the menu to prevent a bug in Explorer that put a form popup element over everything
  
  contentMenu+=(isExplorer && isWin ? "<iframe id ='menuframe' style='position:absolute; width:100%; height:100%; z-index:1;overflow:hidden; border: 0px solid' frameborder=0 framespacing=0 border=0 noresize scrolling='no'></iframe>":"");
  //
  contentMenu+="<div id='table"+divId+"' style='position:absolute; z-index:1000;'>\n";
  contentMenu+="<table width='100%' class='menuContainer' cellspacing='0' cellpadding='0' border='0' style='cursor:hand;'>";
    for (i=0;i<wich.length;i++){
      if (wich[i]){
        if (wich[i].indexOf('separator')>-1){
         h+=1;
        contentMenu +="<tr width='1'><td>";
        }else{
        contentMenu+="<tr><td valign='middle' align='"+contentLineAlign+"' class='cursorHand'>";
        }
        contentMenu+= wich[i];
        contentMenu+="</td></tr>";
      }
    }
  contentMenu+="</table>";
  contentMenu+="</div>\n";
  
  contentMenu+="</div>\n";

  return contentMenu;
}

//-----------------positioning------------------------------------------------------

// to determinate the real x position of the element where the menu has to be attached
function getAbsoluteLeft(el) {
  xPos = el.offsetLeft;
  tempEl = el.offsetParent;
  while (tempEl != null) {
    xPos += tempEl.offsetLeft;
    tempEl = tempEl.offsetParent;
  }
  return xPos;
}

function positionIt(where, targetId, shiftVert, shiftHoriz) {
  if(arguments.length<=3)
  shiftHoriz=0;
  if(arguments.length==2)
  shiftVert=0;
  var el = document.all ? document.all(where) : document.getElementById ? document.getElementById(where) : null;
  var target = document.all ? document.all(targetId) : document.getElementById ? document.getElementById(targetId) : null;
  if (el) {
    trueX = getAbsoluteLeft(el);
    trueX+=distanceX;
    trueY = getAbsoluteTop(el);
   
    var h = el.offsetHeight;
    elHeight=parseFloat(h);
    
    trueY+=parseFloat(elHeight)+distanceY;
    target.style.left=trueX+shiftHoriz;
    target.style.top=trueY+shiftVert;

   }
}
//END positioning

// change the content of the menu according with the call in the page
function changeContent (theMenu, menuId){
  content= createContent (theMenu, menuId);
  if(isExplorer){
    theLayer = document.all[menuId];
    theLayer.innerHTML=content;
  }
  if(isNetscape){
    theLayer = document.getElementById(menuId);
    theLayer.innerHTML =content;
 }
 //scriptDebugger(content);
}

function showMenu(theMenu, where, divId, isTxt){

  if (actualId!="") {
    hideMenu();
  }
  
  if (whichMenuBar && rollOverOnBar && menuBarType) document.getElementById(whichMenuBar).className=barMenuClass;
  whichMenuBar=where;
  menuBarType= isTxt;
  if (whichMenuBar && rollOverOnBar && menuBarType)  document.getElementById(whichMenuBar).className=barMenuOverClass;
  var el = document.getElementById(divId);
  if (!el && show==true){
  createMenuDiv (divId);
  actualId=divId;
  theMenuEl = document.getElementById(divId);
  theMenuEl.style.visibility="visible";
  positionIt(where, divId);
  changeContent (theMenu, divId);
  
  //----dimentioning iframe for Explorer bug collisioning with combo form element
    tableMenuEl= document.getElementById('table'+divId);
    table1MenuEl= document.getElementById('table1'+divId);
    topMenuEl= document.getElementById(where);
    if(isExplorer){
      table1MenuEl.style.width=tableMenuEl.offsetWidth;
      table1MenuEl.style.height=tableMenuEl.offsetHeight;
    }
  //----END dimentioning iframe
  
  // if the menu is out of the window then reposition it
    targetWidth= tableMenuEl.offsetWidth;
    targetHeight =tableMenuEl.offsetHeight;
    windowHeight=getWindowDimensionH()
    windowWidth = getWindowDimensionW();
    if (getAbsoluteLeft(theMenuEl)>=(windowWidth-targetWidth)){
      theMenuEl.style.left=(getAbsoluteLeft(theMenuEl)-(targetWidth)+topMenuEl.offsetWidth)+"px";
    submenuOnLeft=true;
    }
    if (getAbsoluteTop(theMenuEl)+targetHeight>=windowHeight){
      theMenuEl.style.top=(getAbsoluteTop(theMenuEl)-(windowHeight-targetHeight))+"px";
      submenuOnTop=true;
    }
    theMenuEl.style.zIndex=zMax++;
  }
}
  
function showSubMenu(theMenu, menuId, relativeTo) {
  if (!document.getElementById(menuId)) {
	createSubMenuDiv (menuId, relativeTo);
	theSubMenuEl = document.getElementById(menuId);
	changeContent (theMenu, menuId);
	theSubMenuEl.style.zIndex=10+zMax;
	zMax=zMax++;
	//----dimentioning iframe for Explorer bug collisioning with combo form element
		if(isExplorer){
			tableMenuEl= document.getElementById('table'+menuId);
			table1MenuEl= document.getElementById('table1'+menuId);
			table1MenuEl.style.width=tableMenuEl.offsetWidth;
			table1MenuEl.style.height=tableMenuEl.offsetHeight;
		}
	//----ENDdimentioning iframe
	}
}

// hide the menu & subMenu

function hideMenu(){
  if (whichMenuBar && rollOverOnBar && menuBarType) document.getElementById(whichMenuBar).className=barMenuClass;
  actualMenu="";
  actualSubMenu = "";
  whichmenuBar="";
  theMenuEl = document.getElementById(actualId);
  if (theMenuEl){
  removeMenu(actualId);
  actualId="";
  }
  submenuOnLeft=false;
  submenuOnTop=false;
}

function hideSubMenu(menuId){
  theMenuEl = document.getElementById(menuId);
  theMenuToHide= document.getElementById(findDiv(menuId)).menuToHide;
  theMenuEl= document.getElementById(theMenuToHide);
  if (theMenuEl){
  removeMenu(theMenuToHide);
  document.getElementById(findDiv(menuId)).menuToHide=null;
  }
}

function removeMenu (menuId){
  theMenu= document.getElementById(menuId);
  lineToRestore=document.getElementById(menuId+"_sml");
  if(theMenu) theMenu.parentNode.removeChild(theMenu);
  if (lineToRestore) lineToRestore.className='subMenu';
}

function showOnClick(){
  show=(menuOnClick)?true:show;
}
// END hide the menu & subMenu

// debugger ----------------------------------------------------------------------------
var str="";
function scriptDebugger(){
   str = str+"<br>";
    for (i=0;i<arguments.length;i++){
        str+= arguments[i]+"<br>-------------------------------------------------------------<br>";
    }
    var win = window.open("", "debugWin", "width=300,height=200"); // a window object
    win.document.open("text/html", "replace");
    win.document.write("<HTML><HEAD><TITLE>New Document</TITLE></HEAD><BODY>"+str+"</BODY></HTML>");
    win.document.close();
}
// END debugger ----------------------------------------------------------------------------

document.onclick = function () {
if (menuOnClick) setTimeout('if(actualMenu==\'\') show=false;',250);
}

document.onmousedown = hideMenu;