var timer = null;
var current = null;

function prepare(el)
{
  clearTimeout(timer);
  timer = setTimeout(function(){HideMenu(el)}, 300);
}

function retain()
{
  clearTimeout(timer);
}

function ShowMenu(el)
{
  var ob = $('ul', el); 

  if (ob != current)
  {
    HideMenu(current);
    retain();
  }

  ob.show();
  current = ob;
}

function HideMenu(el)
{
  if (!el)
    return; 

  el.hide();
}

$(document).ready(function()
{
  $('.topmenu > li').mouseenter(function(){ShowMenu(this)});
  $('.topmenu > li > ul').mouseenter(function(){retain()});
  $('.topmenu > li').mouseleave(function(){prepare($('ul', this)); });
});


