//-------------------------------------------------------------------------//
//-- horizontal cascading menu v2 -----------------------------------------//
//----- anthony mastoroudes (anthony@binaryfusion.co.za) ------------------// 
//-- IE6.0+ | FF1.0+ | NS7.2+ ---------------------------------------------// 
//-------------------------------------------------------------------------//
//-- 2005-06-18 - optimized event handling to be less resource intensive
//--              fixed random menu close issue when still on sub menu group
//--              optimized LLItem functions
//--              removed auto menu close feature
//--              added menu close on document/item click
//--              added menu debug console
//--              optimized activateMenu func onload pos once & win.onresize
//--              cursor hand feature in mozilla
//--              status bar text
//--              alternative text
//--              menu opacity setting
//-- TO DO LIST!!!
//--            - obfusticated version
//--            - sub menu positioning when menu reaches limits of screen
//--            - menu item width and height issues
//-------------------------------------------------------------------------//
  
//-- menu system variables --------------------------------------------------
//browser forking
var docType       = (document.doctype);
var bodyRef       = docType ? document.documentElement : document.body;
var ns6           = document.getElementById && !document.all;
//menu status & positioning
var menuActive    = false;
var activeMenu    = '';
var activeCell    = '';   //only used by main menu
var debugCounter  = 0;    //only used by debug function
var cX = 0; var cY = 0;  
//main menu
var mmenu = new Array();  
var mmT   = document.createElement("TABLE"); //mm... = main menu
var mmB   = document.createElement("TBODY")  
var mmR   = document.createElement("TR");
var mmC   = document.createElement("TD");   
//sub menu
var smenu = new Array();  
var smT   = document.createElement("TABLE"); //sm... = sub menu
var smB   = document.createElement("TBODY")  
var smR   = document.createElement("TR");
var smC   = document.createElement("TD");
//---------------------------------------------------------------------------
//close menu when click on document
document.onclick = function documentOnClick(e) {
  var evt = ns6? e.target : event.srcElement;
  actionOnClick('', '', evt.id);
};
//re-position menu on window.resize
window.onresize = function documentOnResize() {
  activateMenu();
};
//-- main menu functions ----------------------------------------------------
//creates main menu table group
function mmBegin(name) {
	this.addItem    = mmItem;
	this.items      = new Array();
	this.endGroup   = mmEnd;
  	
  mmT                = document.createElement("TABLE");
  mmT.id             = 'main' + name;
  mmT.style.display  = 'none';    
  //mmT.style.left     = x;
  //mmT.style.top      = y;
  mmT.style.position = position;    
  mmT.style.border   = border;
  mmT.style.height   = mm_height;
  mmT.style.backgroundColor  = mm_bgColor;
	if (ns6) {
	  mmT.style.MozOpacity    = opacity/100;
	} else {
	  mmT.style.filter        = 'alpha(opacity=' + opacity + ')';
	}

  mmB             = document.createElement("TBODY")  
  mmR             = document.createElement("TR"); 
}
//inserts a main menu item
function mmItem(content, subMenuID, cursorHand, icon, URL, jscript) {
  mmC         = document.createElement("TD"); 
	var rndNum  = Math.floor((Math.random() * 100000))
  if (subMenuID != 0) {
    var pref = 'nod_';
  } else {
    var pref = 'itm_';
  }  	
  mmC.id = pref + rndNum;
  //sizes
  mmC.style.height   = mi_height; 
	mmC.style.width    = rmi_width; 	
	mmC.style.padding  = mi_padding;
  //colors
  //mmC.style.backgroundColor  = mi_bgOut; 
  mmC.style.border       = borderSize + 'px ' + borderStyle + ' ' + mi_bgOut;
  //fonts 
  mmC.style.color            = fontColorOut;
  if (content != '') {     
    mmC.style.textAlign      = textAlign;
    mmC.style.verticalAlign  = 'middle'; 
    mmC.style.fontFamily     = fontFace;
    mmC.style.color          = fontColorOut;
    mmC.style.fontSize       = fontSize + "px"; 
    if (fontStyle.indexOf("bold") != -1)
	  	mmC.style.fontWeight     = "bold";
	  else if (fontStyle.indexOf("italic") != -1)
	  	mmC.style.fontStyle      = "italic";
	  else if (fontStyle.indexOf("underline") != -1)
	  	mmC.style.textDecoration = "underline";  
	  var fontTag       = document.createElement("font");
	  fontTag.id        = pref + 'txt_' + rndNum;
    fontTag.innerHTML = content;
  }
  mmC.title = content;
  //icons
	if (icon != '') {
	  var icn        = document.createElement("img");
	  icn.id         = pref + 'icn_' + rndNum;
  	  
	  icn.src        = icon;
	  icn.align      = 'absmiddle';
	  icn.width      = iconWidth;
	  icn.height     = iconHeight;
	  icn.hspace     = 0;
	  icn.vspace     = 0;

	  mmC.appendChild(icn);
	}
  if ((icon != '') || (content != '')) {
   	var icn_spc    = document.createElement("img");
   	icn_spc.id     = pref + 'spc_' + rndNum;

	  icn_spc.src    = iconSpacer;
	  icn_spc.align  = 'absmiddle';
	  if (icon != '') {
	    icn_spc.width  = iconSpace;
	  } else {
	    icn_spc.width  = iconSpace;
	  }
    icn_spc.height = iconHeight;
	  icn_spc.hspace = 0;
	  icn_spc.vspace = 0;
  	  	
	  mmC.appendChild(icn_spc);	  
  }
	if (content != '') {
	  mmC.appendChild(fontTag);
	}
	//other
  if (cursorHand) {
    if (ns6) {
      mmC.style.cursor       = 'pointer';
    } else {
      mmC.style.cursor         = 'hand';
    }
  }	
  //item type specific properties
  if (subMenuID != 0) {
    //node menu item
    mmC.style.backgroundImage     = 'url(' + arrowRight + ')';
    mmC.style.backgroundRepeat    = 'no-repeat';
    mmC.style.backgroundPosition  = 'right center';
      
    mmC.onmouseover = function doActionZ1(e) {
            var evt = ns6? e.target : event.srcElement;
            //alert(evt.id + ' -- ' + activeCell);
            //alow event if: e-source is it's own
            if ((evt.id.indexOf('nod_icn_',0) == 0) || ((evt.parentNode.parentNode.parentNode.id.indexOf('main',0) == 0))) {  
              // && (evt.id != activeCell)
              //alert('dddd');
              window.status = content;
              
              LLItems(activeCell.replace('icn_', ''));
              HLItems(evt.id.replace('icn_', ''));
            
              menuActive = true;
              activeMenu = evt.parentNode.parentNode.parentNode.id;
              activeCell = evt.id.replace('icn_', '');

              hideAll();

              document.getElementById('menu' + subMenuID).style.top     = findPosY(document.getElementById('menuPlacer')) + mm_height + 3; //this.parentNode.parentNode.parentNode.offsetTop + mm_height + 3;
              document.getElementById('menu' + subMenuID).style.left    = this.offsetLeft + (borderSize*2) + (mi_padding*2) + findPosX(document.getElementById('menuPlacer'));
              document.getElementById('menu' + subMenuID).style.display = 'block';
            }
    };
  } else {
    //item menu item
    mmC.onmouseover = function doActionC(e) { 
            var evt = ns6? e.target : event.srcElement;
            //alert(evt.id + ' -- ' + activeCell);
            if ((evt.id.indexOf('itm_icn_',0) == 0) || ((evt.parentNode.parentNode.parentNode.id.indexOf('main',0) == 0))) {
              // && (evt.id != activeCell)
              //alert('dddd');
              window.status = content;
              
              LLItems(activeCell.replace('icn_', ''));
              HLItems(evt.id.replace('icn_', ''));
                                
              menuActive = true;
              activeMenu = evt.parentNode.parentNode.parentNode.id;
              activeCell = evt.id.replace('icn_', '');

              hideAll();    
            }           
    };
    mmC.onclick = function disableMenu(e)  { 
            var evt = ns6? e.target : event.srcElement;
            actionOnClick(jscript, URL, evt.id);
    };      
  }
    
  mmR.appendChild(mmC);
}  
function mmEnd() {
    mmB.appendChild(mmR);  
    mmT.appendChild(mmB);
    bodyRef.appendChild(mmT);  
}  
//-- sub menu functions -----------------------------------------------------
function smBegin(name) {
	this.addItem    = smItem;
	this.items      = new Array();
	this.endGroup   = smEnd;
  	
  smT    = document.createElement("TABLE");
  smT.id = 'menu' + name;
  smB   = document.createElement("TBODY")  
    
  smT.style.width           = rsm_width+(borderSize*3);
	smT.style.border          = border;
	smT.style.padding         = mi_padding;
	if (ns6) {
	  smT.style.MozOpacity    = opacity/100;
	} else {
	  smT.style.filter          = 'alpha(opacity=' + opacity + ')';
	}
	//smT.style.height        = mm_height-4; //mozilla requires total menu group height
	smT.style.backgroundColor = mm_bgColor;
	smT.style.position        = 'absolute';
	smT.style.visibility      = 'visible';	    
  smT.style.display = 'none';
}
function smItem(content, subMenuID, cursorHand, icon, URL, jscript) {
  smR   = document.createElement("TR");
  smC   = document.createElement("TD");    
	var rndNum  = Math.floor((Math.random() * 100000))
  if (subMenuID != 0) {
    var pref = 'nod_';
  } else {
    var pref = 'itm_';
  }  	
  smC.id = pref + rndNum;
  //sizes
  smC.style.height   = mi_height; 
	smC.style.width    = rsm_width-10; 	
	smC.style.padding  = mi_padding;
  //colors
  smC.style.backgroundColor  = mi_bgOut; 
  smC.style.border           = borderSize + 'px ' + borderStyle + ' ' + mi_bgOut;
  //fonts     
  if (content != '') {    
    smC.style.color            = fontColorOut;
    smC.style.textAlign        = textAlign; 
    smC.style.verticalAlign    = 'middle'; 
    smC.style.fontFamily       = fontFace;
    smC.style.color            = fontColorOut;
    smC.style.fontSize         = fontSize + "px"; 
    if (fontStyle.indexOf("bold") != -1)
	  	smC.style.fontWeight     = "bold";
	  else if (fontStyle.indexOf("italic") != -1)
	  	smC.style.fontStyle      = "italic";
	  else if (fontStyle.indexOf("underline") != -1)
	  	smC.style.textDecoration = "underline";  
	  var fontTag       = document.createElement("font");
	  fontTag.id        = pref + 'txt_' + rndNum;
    fontTag.innerHTML = content;
  }
  smC.title = content;
  //icons
	if (icon != '') {
	  var icn        = document.createElement("img");
	  icn.id         = pref + 'icn_' + rndNum;
	  icn.src        = icon;
	  icn.align      = 'right';
	  icn.width      = iconWidth;
	  icn.height     = iconHeight;
	  icn.hspace     = 0;
	  icn.vspace     = 0;
	  smC.appendChild(icn);
	}
  if ((icon != '') || (content != '')) {
   	var icn_spc    = document.createElement("img");
    icn_spc.id     = pref + 'spc_' + rndNum;
	  icn_spc.src    = iconSpacer;
	  icn_spc.align  = 'absmiddle';
	  if (icon != '') {
	    icn_spc.width  = iconSpace;
	  } else {
	    icn_spc.width  = iconSpace;
	  }
    icn_spc.height = iconHeight;
	  icn_spc.hspace = 0;
	  icn_spc.vspace = 0;
	  smC.appendChild(icn_spc);	  
  }
	if (content != '') {
	  smC.appendChild(fontTag);
	}
	//other
  if (cursorHand) {
    if (ns6) {
      smC.style.cursor       = 'pointer';
    } else {
      smC.style.cursor       = 'hand';
    }
  }	
  smC.style.display         = 'block';		
  //item type specific properties
  if (subMenuID != 0) {
    //node menu item
    smC.style.backgroundImage     = 'url(' + arrowRight + ')';
    smC.style.backgroundRepeat    = 'no-repeat';
    smC.style.backgroundPosition  = 'right center';
      
    smC.onmouseover = function doActionA(e) {
            var evt = ns6? e.target : event.srcElement;
            //alow event if: e-source is it's own
            if ((evt.id.indexOf('nod_icn_',0) == 0) || ((evt.parentNode.parentNode.parentNode.id.indexOf('menu',0) == 0) && (evt.id != activeMenu))) {
              window.status = content;
              
              LLItems(evt.id.replace('icn_', ''));
              HLItems(evt.id.replace('icn_', ''));
                                
              menuActive = true;
              activeMenu = evt.parentNode.parentNode.parentNode.id;
              hideSubMenus(); 

              showMenu(subMenuID, this.id);
            }
    };
  } else {
    //item menu item
    smC.onmouseover = function doActionC(e) { 
            var evt = ns6? e.target : event.srcElement;
            //alow event if: e-source is it's own
            if ((evt.id.indexOf('itm_icn_',0) == 0) || ((evt.parentNode.parentNode.parentNode.id.indexOf('menu',0) == 0) && (evt.id != activeMenu))) {
              window.status = content;
              
              LLItems(evt.id.replace('icn_', ''));
              HLItems(evt.id.replace('icn_', ''));
                              
              menuActive = true;
              activeMenu = evt.parentNode.parentNode.parentNode.id;

              hideSubMenus();  
            }             
    };
    smC.onclick = function disableMenu(e)  { 
            var evt = ns6? e.target : event.srcElement;
            actionOnClick(jscript, URL, evt.id);
    };
  }
    
  smR.appendChild(smC);
  smB.appendChild(smR);
}
function smEnd() {
  smT.appendChild(smB);
  bodyRef.appendChild(smT);  
}
//---------------------------------------------------------------------------
//lowlight item(s)
function LLItems(itemID) {
  var node = document; var x = 0; var y = 0; var smCnt = 0;
  if (itemID != 'product') {
    //indentify cell parent menu
    if (document.getElementById(itemID)) {
      var pID = document.getElementById(itemID).parentNode.parentNode.parentNode;
      //s-menu: low-light all cells in menu group
      //  ...cannot just deactivate previous cell as menu HL tracking is required
      if (pID.id.indexOf('menu',0) == 0) {
        node = pID;
        for (y=0; y < node.rows.length; y++) {
          //do nothing to seperator items
          for (x=0; x < node.rows[y].cells.length; x++) {
            if (node.rows[y].cells[x].id != '') {
              var cell = node.rows[y].cells[x].style;
              LLCell(cell);
            }
          }
        }
      }
      //m-menu: low-light previous cell
      if (pID.id.indexOf('main',0) == 0) {
        var cell = document.getElementById(itemID).style;
        LLCell(cell);
      } 
    }     
  } else {
    //reset all menu items
    menus = document.getElementsByTagName('table');
    for (smCnt=0; smCnt<menus.length; smCnt++) {
      node = menus[smCnt];
      for (y=0; y < node.rows.length; y++) {
        //do nothing to seperator items
        for (x=0; x < node.rows[y].cells.length; x++) {
          if (node.rows[y].cells[x].id != '') {
            var cell = node.rows[y].cells[x].style;
            LLCell(cell);
          }
        }
      } 
    }     
  }
}
//lowlight cell (used by LLItems)
function LLCell(cellObj) {
  cellObj.color           = fontColorOut;
  cellObj.backgroundColor = mi_bgOut; 
  cellObj.border          = borderSize + "px solid " + mi_bgOut;
} 
//highlight item
function HLItems(itemID) {
  if (itemID != '') {
    if (document.getElementById(itemID)) {
      document.getElementById(itemID).style.color           = fontColorOver;
      document.getElementById(itemID).style.backgroundColor = mi_bgOver; 
      document.getElementById(itemID).style.border          = border;
    }
  }
}
//hide all sub menus
function hideSubMenus() { 
  var curMenuId = activeMenu;
  var divItm    = document.getElementsByTagName('table');
  var divCnt    = divItm.length;
  var i         = 0;
  if (curMenuId != '') {
    for (i=0; i<divCnt; i++) { 
      //loops through all div's
      if (divItm[i].id.indexOf('menu',0) == 0) { 
        //if div is menu container
        if (divItm[i].id != curMenuId) { 
          //if menu container is not active
          if (parseInt(document.getElementById(curMenuId).style.left) < parseInt(divItm[i].style.left)) {
            //find menu containers to the right of the active menu container. 
            if (divItm[i].style.display == 'block') {
              //debug('hide : ' + divItm[i].id + '  -  cur menu : ' + curMenuId);
              divItm[i].style.display = 'none';
            }
          }    
        }
      }
    }
  }
}
//hides all menus except main menu
function hideAll() {
  var divItm    = document.getElementsByTagName('table');
  var divCnt    = divItm.length;
  var i         = 0;
    
  for (i=0; i<divCnt; i++) { 
    //loops through all div's
    if (divItm[i].id.indexOf('menu',0) == 0) { 
      //if div is menu container
      if (divItm[i].id != 'main1') {
        //if not root menu
        if (divItm[i].style.display == 'block') {         
          divItm[i].style.display = 'none';
        }
      }
    }
  }
}
//show menu
function showMenu(subMenuID, eventSource) {
  if (eventSource) {
    //if ('menu'+subMenuID != activeMenu) {
      //debug('show : menu'+subMenuID);
      var obj = document.getElementById(eventSource).parentNode.parentNode.parentNode;
      //parent X,Y
      var pY = parseInt(obj.style.top);
      var pX = parseInt(obj.style.left);
      var pW = rsm_width;
      //current X, Y placement
      var cX = pW + pX + (borderSize*2) + 2;
      var cY = document.getElementById(eventSource).offsetTop + pY - 3;

	    var bH = 0, bW = 0;
	    var sX = 0, sY = 0;
  	    
	    if (docType) {
	    	bH = document.getElementsByTagName("HTML")[0].offsetHeight;
	    	bW = document.getElementsByTagName("HTML")[0].offsetWidth;
	    	sX = document.getElementsByTagName("HTML")[0].scrollLeft;
	    	sY = document.getElementsByTagName("HTML")[0].scrollTop;
	    } else {
	    	bH = document.body.clientHeight;
	    	bW = document.body.clientWidth;
	    	sX = document.body.scrollLeft;
	    	sY = document.body.scrollTop;
	    }
      //fix x : if menu container off screen
	    //if (cX + width > bW + sX) {
	    //	cX = pX - width;
	    //}
	    //position and display menu container
      if (document.getElementById('menu'+subMenuID)) {
        document.getElementById('menu'+subMenuID).style.top     = cY;
        document.getElementById('menu'+subMenuID).style.left    = cX;
        document.getElementById('menu'+subMenuID).style.display = 'block';
      }
    //}
  }
}
//write debug text to 'debugConsole' div container
function debug(dbText) {
  if (document.getElementById('debugConsole')) { 
    var oDB = document.getElementById('debugConsole');
    var sDB = oDB.innerHTML;
    debugCounter++;
    oDB.innerHTML = '<BR><b>[' + debugCounter + ']</b>&nbsp;' + dbText + sDB;
  }
} 
//execute menu onclick actions
function actionOnClick(js, url, eSrcID) {
  if (menuActive) {
    hideAll(); 
    LLItems('');    
      
    if (eSrcID.indexOf('itm_',0) == 0) {
      window.status = '';
        
      menuActive = false;
      activeMenu = '';  
      activeCell = '';

      if (js != '') {
        eval(js);
      } else {
        if (url != '') {
          window.location = url;
        }
      }
    }
  }
}  
//position and show main menu
function activateMenu() {
  if (menuPlacer) { 
    if (document.getElementById('menuPlacer')) {
      //alert(document.getElementById('menuPlacer').offsetTop);
      cY = findPosY(document.getElementById('menuPlacer')); //parseInt(document.getElementById('menuPlacer').offsetTop) + (mm_height/2);
      cX = findPosX(document.getElementById('menuPlacer')); //parseInt(document.getElementById('menuPlacer').offsetLeft);
      //debug(parseInt(document.getElementById('menuPlacer').offsetLeft));
    //} else {
    //  cY = y;
    //  cX = x;
    //}
  //} else {
  //  cY = y;
  //  cX = x;    
  }
	//position and display menu container
  if (document.getElementById('main1')) {
    document.getElementById('main1').style.top     = cY;
    document.getElementById('main1').style.left    = cX;  
    document.getElementById('main1').style.display = 'block';
  }
  //if (!document.getElementById('menuPlacer')) {
    setTimeout('activateMenu()',50);
  }
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//padding, color, size, opacity properties
var opacity       = 100;                   // integer indicating menu opacity 
var mi_padding    = 1;                     // menu item inner px padding
var mi_width      = 70;                   // menu item px width
var mi_bgOut      = '#B99E2B';             // hex / named color onmouseout
var mi_bgOver     = '#B99E2B';             // hex / named color onmouseover
var mm_bgColor    = '#B99E2B';             // hex / named color main menu bg
//positioning porperties
var menuPlacer    = true;                  // wrap to menuPlace img   
var x             = 0;                     // x px if menuPlace = false
var y             = 0;                     // y px if menuPlace = false                     
//font properties
var fontColorOut  = '#000000';             // hex / named color onmouseout  
var fontColorOver = '#999999';             // hex / named color onmouseover           
var fontFace      = 'arial';               // any font
var fontStyle     = 'normal';              // normal/underline/italic/bold
var fontSize      = 10;                    // px size
//icon properties
var iconWidth     = 10;                    // icon px width                    
var iconHeight    = 10;                    // icon px height
var iconSpace     = 0;                     // px space between icon & text
//general properties
var arrowRight    = './images/arrow-right.gif';          // rel path of sub menu icon
var iconSpacer    = './images/cas_menu_v1_2_blank.gif';
//border properties
//  ...only affects menu group borders
var borderStyle   = 'solid';               // CSS border style
var borderColor   = '';             // hex / named color
var borderSize    = 0;                     // px size
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//DO NOT ALTER THESE VARIABLES!!!
var border        = borderSize + 'px ' + borderStyle + ' ' + borderColor;
var textAlign     = 'left';
var position      = 'absolute';
var mi_height     = 19//(mi_padding*2)+(borderSize*2)+iconHeight;
var mm_height     = (mi_height)+(borderSize*2)+8; 
var rmi_width     = 95;//(mi_padding*2)+(borderSize*2)+mi_width; 
var rsm_width     = rmi_width*2//rmi_width+(borderSize*2)+4;



function init_menu() {
	//padding, color, size, opacity properties
	opacity       = 100;                   // integer indicating menu opacity 
	mi_padding    = 2;                     // menu item inner px padding
	mi_width      = 30;                   // menu item px width
	mi_bgOut      = '#FFAD31';             // hex / named color onmouseout
	mi_bgOver     = '#FFAD31';             // hex / named color onmouseover
	mm_bgColor    = '#FFAD31';             // hex / named color main menu bg
	//positioning porperties
	menuPlacer    = true;                  // wrap to menuPlace img   
	x             = 0;                     // x px if menuPlace = false
	y             = 0;                     // y px if menuPlace = false                     
	//font properties
	fontColorOut  = 'white';             // hex / named color onmouseout  
	fontColorOver = '#545454';             // hex / named color onmouseover           
	fontFace      = 'arial';               // any font
	fontStyle     = 'bold';              // normal/underline/italic/bold
	fontSize      = 11;                    // px size
	//icon properties
	iconWidth     = 16;                    // icon px width                    
	iconHeight    = 16;                    // icon px height
	iconSpace     = 0;                     // px space between icon & text
	//general properties
	
	//border properties
	//  ...only affects menu group borders
	borderStyle   = 'solid';               // CSS border style
	borderColor   = '';             // hex / named color
	borderSize    = 0;                     // px size
	//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
	document.write('<script src="./_includes/cas_menu_v2_struc.asp" language="javascript1.1" type="text/javascript"></script>');
  activateMenu();
}
init_menu();
//-------------------------------------------------------------------------//