(function($) {
	$(function() {
		var menus = $('#nav li:has(.submenu)'),
			timeouts = {};
		menus.live('mouseover', function(e) {
			var menu = $(this).addClass('hover'),
				index = menus.index(this),
				timeout = timeouts[index];

			menu.find('a:first').addClass('active');

			if(timeouts[index]) {
				clearTimeout(timeout);
				timeouts[index] = null;
			}
			var submenus = menus.filter(function(i) {
				return i != index;
			});
			submenus.find('a.active').removeClass('active');
			submenus.find('.submenu').hide();
			menu.find('.submenu').fadeIn(100, function() {
				if(this.style.filter !== undefined && this.style.removeAttribute) {
					this.style.removeAttribute('filter');
				}
			});
		}).live('mouseout', function(e) {
			var menu = $(this);
			timeouts[menus.index(this)] = setTimeout(function() {
				if(menu.hasClass('hover')) {
					return;
				}
				menu.find('a:first').removeClass('active');
				menu.find('.submenu').fadeOut(100);
			}, 500);
			menu.removeClass('hover');
		});

		// Fix IE 6 display bug where odd number of submenu items leaves a 1px empty space
		if(typeof document.body.style.maxHeight == 'undefined') {
			menus.each(function(i, menu) {
				menu = $(menu);
				var subItems = menu.find('li');
				if(subItems.length % 2 != 0) {
					$(subItems[subItems.length - 2]).css({
						marginBottom: '-1px'
					});
				}
			});
		}
	});

	// Social media
	$('#social-media a').hover(
		function(e) {
			$(this).find('span').css({ display: 'block' });
		},
		function(e) {
			$(this).find('span').css({ display: '' });
		}
	);
})(jQuery);