
if (window.jQuery)
jQuery(function() {
  // group by parents
  var p = {}, k;
  for (k in submenus) {
    var item = submenus[k], pid = item.p;
    if (!p[pid]) p[pid] = '';
    p[pid] += item.s;
  }
  // create blocks
  for (k in p)
    p[k] = $('<ul>').addClass('sub').attr('id', 'sub' + k).html(p[k])
    .appendTo(document.body);

  // hide function
  var showing = null, timeout = null, delay = 0.5;
  function hide() {
    if (!showing) return;
    items.removeClass('active');
    showing.hide();
    showing = null;
  }

  function hideDelayed() {
    timeout = setTimeout(hide, delay * 1000);
  }

  // show function
  var items = $('#layout-header .menu li');
  items.hover(function(ev) {
    var el = $(ev.target.parentNode),
        id = el[0].className.match(/\d+/)[0];
    hide();
    clear();
    if (!p[id]) return;
    showing = p[id];
    showing.css(el.offset()).show();
    el.addClass('active');
  }, hideDelayed);

  function clear() {
    timeout = clearTimeout(timeout);
  }

  // allow hovering the submenus
  for (k in p)
    p[k].hover(clear, hideDelayed);
});

