(window.attachEvent)?document.attachEvent('onclick',initNav):window.addEventListener('click',initNav,false);

//global state variables:
var currentMenu3,currentMenu4,opened3=false,opened4=false,selected2nd=1,max2ndLinks=1;
//variables to hold values of closing timers for 3rd and 4th levels:
var iClosingTimer3,aClosingTimer3=[],iClosingTimer4,aClosingTimer4=[];

function initNav(){
  (window.attachEvent)?document.attachEvent('onclick',possiblyCloseNav):window.addEventListener('click',possiblyCloseNav,false);
  //stop bubble for all existing 3rd level menus:
  var i=1,oObj=$('subTab'+i),oObj2=null;
  while(oObj){
    oObj.onclick=stopBubble;
    oObj2=$('tabThirdNavigation'+i);if(oObj2)oObj2.onclick=stopBubble;
    i++;oObj=$('subTab'+i); //attempt next object
  } //while
} //initNav()

  function highlightTab(oMenu){oMenu.className="hover";}
  function resetTab(oMenu){oMenu.className="tab";}
  function resetTabSelected(oMenu){oMenu.className="tabCurrentWithSubNav";}

  function highlightSubTab(oMenu){if(!opened3 || opened3 && oMenu.className!='tabCurrentMoreSelected')oMenu.className="hover";}
  function resetSubTab(oMenu){if(!opened3 || opened3 && oMenu.className!='tabCurrentMoreSelected')oMenu.className="tab";}
  function resetSubTabSelected(oMenu){if(!opened3 || opened3 && oMenu.className!='tabCurrentMoreSelected')oMenu.className="tabCurrent";}

  function highlightThirdTab(oMenu,pos){cl3();oMenu.className=pos?"thirdNav hover "+pos:"thirdNav hover";setImage3(oMenu.id)}
  function resetThirdTab(oMenu,pos){
    oMenu.className=pos?"thirdNav "+pos:"thirdNav";resetImage3(oMenu.id);
	if(opened4)closeMenu4Click(currentMenu4);
  }
  function setImage3(menuId){var oImg=$(menuId+'imgHover');if(oImg){oImg.style.display="block";$(menuId+'img').style.display="none";}}
  function resetImage3(menuId){var oImg=$(menuId+'img');if(oImg){oImg.style.display="block";$(menuId+'imgHover').style.display="none";}}

  function highlightFourthTab(oMenu,pos){cl4();oMenu.className=pos?"fourthNav hover "+pos:"fourthNav hover"}
  function resetFourthTab(oMenu,pos){oMenu.className=pos?"fourthNav "+pos:"fourthNav"}

  //close navigation in two situations: 1)whenever user clicks anywhere outside navigation (immediately)
  //2)whenever user rolls out of navigation (with time delay)
  function possiblyCloseNav(oEvent){
    if(opened3 || opened4){
      switch(oEvent.type){
        case 'click':
          if(opened4)closeMenu4Click(currentMenu4);
          if(opened3)closeMenu3Click('',currentMenu3);
          break;
        case 'mousemove':
          if(opened4)closeMenu4Wrap(currentMenu4);
          if(opened3)closeMenu3Wrap('',currentMenu3);
          break;
      } //switch
    } //if
  } //possiblyCloseNav()

  //toggle 3rd and 4th level menus:
  function toggleMenu(menuLevel,menuID,sMore){
    switch(menuLevel){
      case 3: //toggle current menu, close other opened
        if(opened4)closeMenu4Click(currentMenu4);
        oMenu=$('tabThirdNavigation'+menuID);
        if(!currentMenu3)currentMenu3=oMenu; //initialize to avoid null object
        if(currentMenu3==oMenu){(opened3)?closeMenu3Click(menuID,oMenu):openMenu3Wrap(menuID,oMenu,sMore);}
        else {closeMenu3Click('',currentMenu3);openMenu3Wrap(menuID,oMenu);}
        break;
      case 4:
        oMenu=$('tabFourthNavigation'+menuID);
        if(!currentMenu4)currentMenu4=oMenu; //initialize to avoid null object
        if(currentMenu4==oMenu){(opened4)?closeMenu4Click(oMenu):openMenu4Wrap(oMenu);}
        else {closeMenu4Click(currentMenu4);openMenu4Wrap(oMenu);}
        break;
    } //switch
  } //toggleMenu()

  //wrapper to close 3rd menu on click:
  function closeMenu3Click(menuID,oMenu){
    if(!menuID)menuID=oMenu.id.replace(/\D/g,''); //if clicked somewhere outside nav, there is no menuID
    var sClass=(menuID==selected2nd)?'subNavCurrent':'subNav';
    $('subTab'+menuID).className=sClass; //reset 2nd level
    $('subTabSpan'+menuID).className=sClass; //set 2nd level span value //added by mehul on 28-Jan-2011
    closeMenu3(oMenu); //close 3rd level
    //remove the mousemove event handler from the now closed menu:
    (window.detachEvent)?document.detachEvent('onmousemove',possiblyCloseNav):window.removeEventListener('mousemove',possiblyCloseNav,false);
  } //closeMenu3Click()

  //wrapper to close 3rd menu (with time delay):
  function closeMenu3Wrap(menuID,oMenu){
    iClosingTimer3=setTimeout(function(){
      if(!menuID)menuID=oMenu.id.replace(/\D/g,''); //if clicked somewhere outside nav, there is no menuID
      var sClass=(menuID==selected2nd)?'subNavCurrent':'subNav';
      $('subTab'+menuID).className=sClass; //reset 2nd level
      $('subTabSpan'+menuID).className=sClass; //set 2nd level span value //added by mehul on 28-Jan-2011
      closeMenu3(oMenu); //close 3rd level
      //remove the mousemove event handler from the now closed menu:
      (window.detachEvent)?document.detachEvent('onmousemove',possiblyCloseNav):window.removeEventListener('mousemove',possiblyCloseNav,false);
    },1000); //setTimeOut{}
    //moving the mouse will generate several move events, need to save them all in order to reset them
    //  when user reenters the menu entry right away
    aClosingTimer3.push(iClosingTimer3); //save all closing timers for level 3 menu
  } //closeMenu3Wrap()
  function closeMenu3(oMenu){if(oMenu){oMenu.style.display="none";opened3=false;}}

  function closeMenu4Click(oMenu){if(oMenu){oMenu.style.display="none";opened4=false;}}
  function closeMenu4Wrap(oMenu){
    iClosingTimer4=setTimeout(function(){
      if(oMenu){oMenu.style.display="none";opened4=false;}
    },2000); //setTimeOut{}
    aClosingTimer4.push(iClosingTimer4); //save all closing timers for level 4 menu
  } //closeMenu4Wrap()

  //wrapper to open 3rd level:
  function openMenu3Wrap(menuID,oMenu,sMenu){
    var oObj;
		if(!sMenu)sMenu='';
    $('subTab'+menuID).className='subNavCurrentSelected'+sMenu; //set 2nd level to selected state
    $('subTabSpan'+menuID).className='subNavCurrentSelected'+sMenu; //set 2nd level span value //added by mehul on 28-Jan-2011
    openMenu3(oMenu); //open 3rd level
    //3rd and 4th level menus are supposed to close themselves when user moves her mouse outside of them
    //this can be done by checking mousemove event and stopping event bubble on the opened menus:
    (window.attachEvent)?document.attachEvent('onmousemove',possiblyCloseNav):window.addEventListener('mousemove',possiblyCloseNav,false);
    oObj=$('tabThirdNavigation'+menuID);if(oObj)oObj.onmousemove=stopBubble;
    oObj=$('subTab'+menuID);if(oObj)oObj.onmousemove=stopBubble;
  } //openMenu3Wrap()
  function openMenu3(oMenu){if(oMenu){oMenu.style.display="";currentMenu3=oMenu;opened3=true;}}

  //wrapper to open 4th level:
  function openMenu4Wrap(oMenu){
    openMenu4(oMenu); //open 4th level
    //if user opens 4th level menu, but stays within 3rd menu, stopped bubble will prevent auto closing
    //need to invoke the auto closing directly to cover this case:
    closeMenu4Wrap(currentMenu4);
  } //openMenu4Wrap()
  function openMenu4(oMenu){if(oMenu){oMenu.style.display="";currentMenu4=oMenu;opened4=true;}}

	function setMoreLinkOption(menuID,destID){
		hideAllExtras(); //hide existing extras (if any) from destination menu
		showAllExtras(menuID); //show all extras within More Options menu
		$('subTabLi'+destID).style.display=''; //display extra into destination menu...
		$('thirdTab'+menuID).style.display='none'; //...and hide current entry
	} //setMoreLinkOption()
	
	//hide all extra 2nd levels (entries over max2ndLinks except More Options) from destination menu:
	function hideAllExtras(){
		var i=max2ndLinks+1;
	  var oObj=$('subTabLi'+i);
		while(oObj){
      oObj.style.display='none';
      i++;oObj=$('subTabLi'+i); //attempt next object
    } //while
	} //hideAllExtras()
	
	//show all extra 2nd level within More Options menu:
	function showAllExtras(menuID){
	  var iParentID=menuID.split('_')[0],i=1;
		var oObj=$('thirdTab'+iParentID+'_'+i);
		while(oObj){
      oObj.style.display='';
      i++;oObj=$('thirdTab'+iParentID+'_'+i); //attempt next object
    } //while
	} //showAllExtras()
/*
  //when user clicks on an entry, move that entry to placeholder area then close this menu:
  function setMoreLinkOption(menuID,sURL){
    var iParentID=menuID.split('_')[0];  //menuID is in "A_B" syntax, need to extract "A"
    var oMenu=$('tabThirdNavigation'+iParentID);
    var iDest=iParentID-1; //destination index is always 1 less than current
    populatePlaceholder(iDest,menuID,sURL);
    displayAllMoreOptions(iParentID); //make sure all entries are displayed first...
    $('thirdTab'+menuID).style.display='none'; //...before hiding current entry
    closeMenu3Click(iParentID,oMenu); //mission accomplished, close this menu
  } //setMoreLinkOption()

  //populate placeholder menu from selected More Options entry:
  function populatePlaceholder(iDest,menuID,sURL){
    var oDest=$('subTabText'+iDest);
    var sText=$("navText"+menuID).innerHTML;
    oDest.className=(sText.indexOf('<br')==-1)?'subTabText':'subTabText2Lines';
    oDest.innerHTML='<a href="'+sURL+'">'+sText+'</a>';
  } //populatePlaceholder()
*/
  //display (unhide) all More Options entries:
  function displayAllMoreOptions(menuID){
    var i=1,oObj=$('thirdTab'+menuID+'_'+i);
    while(oObj){
      oObj.style.display='';
      i++;oObj=$('thirdTab'+menuID+'_'+i); //attempt next object
    } //while
  } //displayAllMoreOptions()

  //clear closing timers, for example when user left then reentered opened menu:
  function cl3(){while(aClosingTimer3.length>0){clearTimeout(aClosingTimer3.shift());}}
  function cl4(){while(aClosingTimer4.length>0){clearTimeout(aClosingTimer4.shift());}}

  function stopBubble(oEvent){(typeof oEvent=="undefined")?event.cancelBubble=true:oEvent.stopPropagation();}
  function $(obj){return document.getElementById(obj);}
