(function($) {
  $(function() {
    var show = function($arrow) { $('ul.labs', $(this)).show('fast', function() { $arrow.addClass('downarrow'); }); },
      hide = function($arrow) { $('ul.labs', $(this)).hide('fast', function() { $arrow.removeClass('downarrow'); }); };
    $('#research ul.labs').hide();
    $('#research > div').each(function() {
      var $this = $(this),
        $h4 = $('h4', $this).parent(),
        $arrow = $('div.arrow', $this),
        evtHandle = null,
        shown = null,
        cancel = function() { if(evtHandle) clearTimeout(evtHandle); evtHandle = null; };
      if($arrow.size() == 0) {
        return;
      }
      $arrow.click(function() {
        if(!shown) {
          cancel();
          $.proxy(show, $h4)($arrow);
          shown = true;
        } else {
          cancel();
          $.proxy(hide, $h4)($arrow);
          shown = false;
        }
      });
      $h4.hover(function() {
          if(!shown) {
            cancel();
            evtHandle = setTimeout(function() { $.proxy(show, $h4)($arrow); }, 400);
          }
        }, function(e) {
          if(!shown) {
            cancel();
            $.proxy(hide, $h4)($arrow);
          }
      });
    });
  });
}(window.jQuery));
