var pe = null;
var menuHasFocus = false;
var currentlyOpenedList = null;

window.onload=setBehaviors;
function setBehaviors(){

  /////////////////////////////////////////
  //COMPORTAMIENTO MENU PRINCIPAL//////////
  ///////////////////////////////////////
  setMainMenuKeyboardNavigation();

 
  /////////////////////////////////
  ////COMPORTAMIENTO PILARES///////
  /////////////////////////////////
  $$('#nav2 .nav_li_link_level_1').each(function(item) {
      item.onmouseover = function(){item.style.backgroundPosition='bottom left'};
      item.onmouseout = function(){item.style.backgroundPosition='top left'};
  });

}

function setMainMenuKeyboardNavigation()
{
  var newCount = 300;

  $$('#nav1 .nav_li_level_1').each(function(item) {
      newCount++;
      if(item.firstDescendant().next()!=null)
      {
        item.style.zIndex = newCount;
        
        item.onmouseover = function(){
            item.firstDescendant().next().style.visibility='visible';
            item.firstDescendant().style.backgroundPosition = 'bottom center';
             menuHasFocus = true;
        };
        item.onmouseout = function(){
            item.firstDescendant().next().style.visibility='hidden'
            item.firstDescendant().style.backgroundPosition = 'top center';
            menuHasFocus = false;
        };
      }
  });



  $$('#nav1 .nav_li_link_level_1').each(function(item) {
      newCount++;
          item.style.zIndex = newCount;
        
        item.onmouseover = function(){
           // item.firstDescendant().next().style.visibility='visible';
            item.firstDescendant().next().style.visibility = 'visible';
            item.firstDescendant().style.backgroundPosition = 'bottom center';
             menuHasFocus = true;
             currentlyOpenedList = item.next();
        };

 
       
               item.onfocus = function(){
              //hide currently opened list if it exists 
              if(currentlyOpenedList!=null)
              {
                 currentlyOpenedList.style.visibility = 'hidden';
              }
              menuHasFocus = true;
              item.next().style.visibility = 'visible';
              currentlyOpenedList = item.next();
              item.firstDescendant().next().style.visibility='visible';
        };
        item.onblur = function(){
              pe = new PeriodicalExecuter(checkOpenedMenu, 1); 
              menuHasFocus = false;
              //hide list if focus is not within current list
        };

  });



  $$('#nav1 .nav_li_link_level_2').each(function(item) {
       item.onblur = function(){
         pe = new PeriodicalExecuter(checkOpenedMenu, 1); 
         menuHasFocus = false;
       };
       item.onfocus = function(){
         menuHasFocus = true;
         currentlyOpenedList.style.visibility = 'visible';
       };

       item.onmouseover = function(){
         menuHasFocus = true;
         //currentlyOpenedList.style.visibility = 'visible';
       };
   
 
  });
  
  $$('#nav1 .nav_li_link_level_3').each(function(item) {
       item.onblur = function(){
         pe = new PeriodicalExecuter(checkOpenedMenu, 1); 
         menuHasFocus = false;
       };
       item.onfocus = function(){
         menuHasFocus = true;
         currentlyOpenedList.style.visibility = 'visible';
       };
  });

}


function checkOpenedMenu()
{
  if(!menuHasFocus)
  {
    if(currentlyOpenedList!=null)
    {
      currentlyOpenedList.style.visibility = 'hidden';
    }
  }
  pe.stop();
}

