var oteraMenuNode = null;
function none(){
  return false;
}

function registerOteraMenuNode(node) {
  oteraMenuNode = node;
}

function onWindowOnload() {
  // Close main menu.
    if(oteraMenuNode) {
      oteraMenuNode.parent().find('a').removeClass('hover');
      oteraMenuNode.parent().find("ul.submenu").slideUp('fast');
    }
}

$(document).ready(function(){

  /* Menu */
  $("#menuPrincipal ul a").hover(function(){ //When trigger is hover...
      registerOteraMenuNode($(this))
      //Following events are applied to the subnav itself (moving subnav up and down)
      $(this).addClass('hover');
      $(this).parent().find("ul.submenu").stop(false, true).slideDown('fast').show(); //Drop down the subnav on click
      $(this).parent().hover(function(){}, function(){
        $(this).parent().find('a').removeClass('hover');
        $(this).parent().find("ul.submenu").stop(false, true).slideUp('fast'); //When the mouse hovers out of the subnav, move it back up
      });
  }, function(){$(this).addClass('hover');});

  /* Search form Submit event. */
  $('#cse-search-box').submit(function(){
    if($.trim($('#q').val()) != ''){
      return true;
    } else {
      return false;
    }
  });

  /* Search Button Click event */
  $('#searchlabel').click(function(){
    $('#searchlabel').hide();
    $('#searchbox').show();
    $('#q').focus();
    return false;
  });

  /* Query input Blur event */
  $('#q').blur(function(){
    if($.trim($('#q').val()) == ''){
      $('#searchlabel').show();
      $('#searchbox').hide();
    }
  });

  /* Check if the query box is empty string */
  if($.trim($('#q').val()) != ''){
      $('#searchlabel').hide();
      $('#searchbox').show();
  } else {
      $('#q').val('');
  }
  
  /* Publications */
  $('#publications-ecotera > ul > li > a').click(function(event) {
	  event.preventDefault();
	  if ($(this).parent().hasClass('open'))
		  $(this).parent().removeClass('open');
	  else
		  $(this).parent().addClass('open');
  });
  
  
});

window.onunload = onWindowOnload;
