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


window.onload=setBehaviors;
function setBehaviors(){
  /////////////////////////////////////////
  //COMPORTAMIENTO MENU PRINCIPAL//////////
  ///////////////////////////////////////
  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';
        };
        item.onmouseout = function(){
            item.firstDescendant().next().style.visibility='hidden'
            item.firstDescendant().style.backgroundPosition = 'top center';
        };
      }
  });

  /////////////////////////////////
  ////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'};
      item.onfocus = function(){item.style.backgroundPosition='bottom left'};
      item.onblur = function(){item.style.backgroundPosition='top left'};
  });

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

  /////////////////////////////////////////////////////////////
  ////////////COMPORTAMIENTO NOTICIAS NO DESTACADAS////////////
  /////////////////////////////////////////////////////////////
  setNewsItemsPositions();




}

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 = '20';
  $('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;
  $$('#da3 .newsCategoryTitle').each(function(item){
     itemsCount++;
     if(itemsCount>1)
     {
       item.next().style.display = 'none';

     }
     item.onclick = function(){ toggleNewsItemsVisibility(item.id)};
     
  });
}


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

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

function toggleNewsItemsVisibility(itemId)
{
  var newsItemsCount = 0;

  $$('#da3 .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';
      item.firstDescendant().style.backgroundPosition = '0px -3px';
     }
     else{
       
      //   item.next().slideDown("slow");
       
       
       item.next().style.display = 'block';
       item.firstDescendant().style.backgroundPosition = '0px -23px';
     }

     
  });
}
