var newsItemsHeadings = {};
var newsItemsContents = {};
var newsItemsIds = {};

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'};
  });

  ////////////////////////////////////////////////
  ////////COMPORTAMIENTO NOTICIAS ENSENANZA///////
  ///////////////////////////////////////////////////
  
  /*setMainNewsBehaviors();*/


}

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();
}

function setMainNewsBehaviors()
{
  //le quitamos la barra de scroll que se muestra por defecto
  $('da1').style.overflow = 'visible';

  //escondemos las noticias que no son la primera
  var newsItemsCount = 0;
  $$('#da1 div.newsItemBox').each(function(item){
     newsItemsCount++;
     if(newsItemsCount>1)
     {
       item.style.display = 'none';
     }

     newsItemsHeadings[newsItemsCount] = item.firstDescendant().innerHTML;
     newsItemsIds[newsItemsCount] = item.id;
  });

  //generamos un div con los controles para cambiar la noticia

  //dar formato a los controles
  $('da1').style.position = 'relative';
  $('da1').style.zIndex = '50';
  $('newsControls').style.height = '20px';
  $('newsControls').style.width = '335px';
  $('newsControls').style.padding = '0px 0px 0px 5px';
  $('newsControls').style.background = 'url(/css/portal/images/newsControlsFondoGris.gif) top left no-repeat';
  $('newsControls').style.position = 'absolute';
  $('newsControls').style.left = '25px';
  $('newsControls').style.top = '287px';
  $('newsControls').style.zIndex = '140';

  for(x=1; x<=newsItemsCount; x++)
  {
    var itemClass = (x==1)?'activeNewsItemControl':'newsItemControl';
    var itemTag = (x==1)?'span':'a href="http://www.udelar.com"';
    var onclickOption = (x!=1)?'onclick="showItem('+x+');return false"':'';
    $('newsControls').innerHTML+= '<'+itemTag+' title="'+newsItemsHeadings[x]+'" class="'+itemClass+'" tabindex="0" '+onclickOption+'>'+x+'</'+itemTag+'> ';
  }
//  $('newsControls').innerHTML+= newsItemsHeadings[1];
}

function setNewsItemsPositions(){
  var itemsCount = 0;
  $$('#da2 .newsCategoryTitle').each(function(item){
     itemsCount++;
     if(itemsCount>1)
     {
       item.next().style.display = 'none';
       item.style.background = 'url(/css/portal/images/da2_center.jpg) top left no-repeat #1B5A8D';
       


     }
     else{
       item.style.background = 'url(/css/portal/images/da2_top.jpg) top left no-repeat #1B5A8D';
     }
     item.onclick = function(){ toggleNewsItemsVisibility(item.id)};

  });
}


function showItem(targetItemNumber)
{
  var newsItemsCount = 0;
  var newControlsString = '';
  var itemClass = '';
  var onclickOption = '';
  $$('#da1 div.newsItemBox').each(function(item){
     newsItemsCount++;
     if(newsItemsCount!=targetItemNumber){
       item.style.display = 'none';
       itemClass = 'newsItemControl';
       onclickOption = 'onclick="showItem('+newsItemsCount+')")';
     }
     else{
       item.style.display = 'block';
       itemClass = 'activeNewsItemControl';
       onclickOption = '';
     }
     newControlsString += '<span title="'+newsItemsHeadings[newsItemsCount]+'" class="'+itemClass+'" tabindex="0" '+onclickOption+'>'+newsItemsCount+'</span> ';

  });
//  newControlsString += newsItemsHeadings[targetItemNumber];
  $('newsControls').innerHTML = newControlsString;
}

function toggleNewsItemsVisibility(itemId)
{
  var newsItemsCount = 0;

  $$('#da2 .newsCategoryTitle').each(function(item){
     newsItemsCount++;
     if(item.id!=itemId)
     {
       
//       if(newsItemsCount>1){
//         item.next().hide();
//       }else{
//         item.next().slideUp("slow");
//       }
      item.next().style.display = 'none';
     }
     else{
       
      //   item.next().slideDown("slow");
       
       
       item.next().style.display = 'block';
     }

     
  });
}

