$(document).ready(function() {
  //SUBMENU
  $(".nivel1").mouseover(function () {
     $('.nivel1').children('.submenu').hide();
     $('.nivel1').removeClass('ativo');
     $(this).addClass('ativo');

     browserWidth = document.body.clientWidth;
     var alturaMenu = $(".menu").height();
     var larguraMenu = $(".menu").outerWidth() -12;
     var offset = $(".menu").offset();
     var diferencaRight = browserWidth - offset.left;
     var right = diferencaRight - larguraMenu;
     var top = offset.top;

     $(this).children('.submenu').css({'right':right-12+'px', 'top':top+alturaMenu+1 +'px', 'width':larguraMenu-2 +'px'});
     $(this).children('.submenu').show();

     controle = true;
     return false;
  });

  $("body").mouseover(function () {
     controle = false;
  });

  $(".nivel1").mouseout(function () {
     if(timeout_controle!=undefined){
        clearTimeout(timeout_controle);
     }
     timeout_controle = setTimeout("sumirMenu();" ,600);
  });

  $(".submenu").each(function () {
     $(this).children("li:last").css("background","none");
  });

  //ALTURA DA PÁGINA
  var browserHeight = document.documentElement.clientHeight;
  var PageRodape = $("#page").height() + $("#rodape").height();
  var diferenca = browserHeight - ($("#topo").height() + $("#rodape").height());
  if(PageRodape < browserHeight){
     $("#conteudo").height(diferenca);
  }
  else{
     $("#conteudo").css("min-height","450px");
  }
  $('#pagina-atual').mouseover();

});

function myOnComplete() { return true; }

var controle = false;
var timeout_controle = undefined;
function sumirMenu(){
  if(!controle){
     $('.nivel1').children('.submenu').hide();
     $('.nivel1').removeClass('ativo');
     $('#pagina-atual').children('.submenu').show();
     $('#pagina-atual').addClass('ativo');
  }
}

