/* MENU */
function stretchMenu(){
   if(document.getElementById("temas")) document.getElementById("temas").className = "";

   var topmenu = document.getElementById('topmenu');
   if(!topmenu) return;
   var children = topmenu.children;
   for(var i = 0; i < children.length; i++){
      children[i].className = "";
   }

   var available = topmenu.offsetWidth + 1;

   var usedwidth = 0;
   for(var i = 0; i < children.length; i++){
      children[i].style.width = "auto";
      children[i].style.paddingLeft = "0px";
      children[i].style.paddingRight = "0px";
      usedwidth += children[i].offsetWidth-1;
   }

   available -= usedwidth;
   for(var i = 0; i < children.length; i++){
      var w = Math.floor(available/(children.length-i));
      children[i].style.width = children[i].clientWidth + w + "px";
      available -= w;
   }
   for(var i = 0; i < children.length; i++){
      children[i].className = "fix";
   }

   if(document.getElementById("temas")) document.getElementById("temas").className = "fix";

}
window.onload = stretchMenu;
window.onresize = stretchMenu;

/* SCROLL TEMAS */
var scrollInterval;
var scrollContainer;
var scrollLeft;
var scrollRight;
var scrollDirection;
function initScroll(){
   scrollContainer = document.getElementById('temas-scroll');
   scrollLeft = document.getElementById('scroll-left');
   scrollRight = document.getElementById('scroll-right');

   scrollContainer.scrollLeft = 0;
   scrollDirection = 1;
   startScroll();
  
   scrollContainer.onmouseover = pauseScroll;
   scrollContainer.onmouseout = resumeScroll;
   scrollLeft.onmouseover = ffScroll;
   scrollLeft.onmouseout = resumeScroll;
   scrollRight.onmouseover = rwdScroll;
   scrollRight.onmouseout = resumeScroll;
}
function startScroll(mul){
   if(!mul) mul = 1;
   if(scrollInterval) window.clearInterval( scrollInterval );
   scrollInterval = window.setInterval(doScroll, 50*mul);
}
function doScroll(){
   if(scrollDirection){
      scrollContainer.scrollLeft += 1;
      if(scrollContainer.children[1].offsetLeft <= scrollContainer.scrollLeft){
         scrollContainer.scrollLeft = 0;
         scrollContainer.insertBefore( scrollContainer.children[0], null );
      }
   } else {
      scrollContainer.scrollLeft -= 1;
      if(scrollContainer.scrollLeft == 0){
         scrollContainer.insertBefore( scrollContainer.children[scrollContainer.children.length-1], scrollContainer.children[0] );
         scrollContainer.scrollLeft = scrollContainer.children[1].offsetLeft;
      }
   }
}
function pauseScroll(){
   window.clearInterval(scrollInterval);
}
function resumeScroll(){
   scrollDirection = 1;
   startScroll();
}
function ffScroll(){
   scrollDirection = 1;
   startScroll(0.3);
}
function rwdScroll(){
   scrollDirection = 0;
   startScroll(0.3);
}

