function MegaMenu(selector) {

  var menu = {
    elem: $(selector)
  };
  
  function hover() {
    var hoverIntent_config = {
      interval: 200,
      timeout: 800,
      over: function() {
        setDefault();
        $(this).find('div.megamenu').css({"z-index": "1001"});
        $(this).find('div.megamenu').fadeIn();
      },
      out: function() {
        $(this).find('div.megamenu').fadeOut();
      }
    },
    hover_config = {
      over: function() {
        $(this).addClass('hover');
      },
      out: function() {
        $(this).find('div.megamenu').css({"z-index": "100"});
        $(this).removeClass('hover');
      }
    };
    
    menu.elem.find('>li')
             .hoverIntent(hoverIntent_config)
             .hover_with_hash(hover_config);
  };
  
  function subHover() {
    var hoverIntent_config = {
      interval: 100,
      timeout: 500,
      sensitivity: 3,
      over: function() {
        menu.elem.find('ul.submenu>li').removeClass('hover');

        $(this).addClass('hover');
        if(!$(this).find('div.details').is(':visible')) {
          menu.elem.find('ul.submenu>li').find('div.details, div.features').fadeOut();
          $(this).find('div.details, div.features').fadeIn();
        }
      },
      out: function() {
      }
    };
    
    menu.elem.find('ul.submenu>li')
             .hoverIntent(hoverIntent_config);
    
  };
  
  function setDefault() {
      menu.elem.find('ul.submenu>li').removeClass('hover');
      menu.elem.find('ul.submenu>li').find('div.details, div.features').hide();
      menu.elem.find('ul.submenu>li:first-child').addClass('hover').find('div.details, div.features').show();
  };
  
  function loadJS(script) {
    $('<script></script>', {
      type: 'text/javascript',
      src: '/javascripts/' + script
    }).appendTo('head');
  };
  
  if(typeof($.fn.hoverIntent) == 'undefined')
    loadJS('jquery.hoverIntent.min.js');
  
  hover();
  subHover();
  
};

$(function(){
  var menu = new MegaMenu('div#navigation ul.primary');
});

Array.prototype.has = function(value) {
  for(var i = 0, loopCnt = this.length; i < loopCnt; i++) {
    if(this === value) {
      return true;
    }
  }
  return false;
};
