(function () {
   var $ = jQuery;
   var console = { log: function () {} }

$(document).ready(setupMenu);

function setupMenu() {
   // Implement hovers
   $('.mainmenu_item > a').each(function () {
      this.subMenu = $('.submenu', this.parentNode);

      $(this).hover(
         MenuDropDown.displayMenu,
         function () {
            console.log('()');
            console.log('<- unhover mainmenu');
            MenuDropDown.hideTimer = setTimeout(MenuDropDown.hideAll, 500);
            console.log('------------------------------');
         }
      );

      this.subMenu.hover(
         function () {
            console.log('()');
            console.log('-> hover submenu');
            console.log('clear timer', MenuDropDown.hideTimer);
            clearTimeout(MenuDropDown.hideTimer);
            console.log('------------------------------');
         },
         function () {
            console.log('()');
            console.log('<- unhover submenu');
            MenuDropDown.hideTimer = setTimeout(MenuDropDown.hideAll, 500);
            console.log('set hideTimer', MenuDropDown.hideTimer);
            console.log('------------------------------');
         }
      )
   });

   // flurp the submenus to cover the entire page width
   $('.submenu').each(function () {
      $menu = $(this);

      $menu.width( $('body').width() );
      $menu.css('left', -Math.ceil($menu.parent().position().left) - 1);
   });
}

var MenuDropDown = new Object();

MenuDropDown.hideTimer = null;

MenuDropDown.displayMenu = function () {
   console.log('# displayMenu()');
   var menuitem = this;

   console.log('-> hover mainmenu!');
   console.log('clear hidetimer', MenuDropDown.hideTimer);
   clearTimeout(MenuDropDown.hideTimer);
   MenuDropDown.hideAll();
   console.log('show matching submenu()');
   menuitem.subMenu.fadeIn(200);

   console.log('------------------------------');
}

MenuDropDown.hideMenu = function () {
}

MenuDropDown.hideAll = function () {
   console.log('# hideAll()');
   console.log('current hidetimer ', MenuDropDown.hideTimer);
   $('.submenu').hide();
   console.log('------------------------------');
}


})();
