var mainmenu, submenu;

function menuCursorCheckPosition() {
	// cursor isn't on either menu, reset to active menu item's submenu
	if ((submenu.menuCursor == false) && (mainmenu.menuCursor == false)) {
		window.clearInterval(mainmenu.checkInterval);
		mainmenu.checkInterval = null;

		$('ul li a', mainmenu).removeClass('hover');
		menuSubHideAll();
		menuSubShow($('ul li', mainmenu),'a[class=active]');
	}
}

// hide all items in submenu
function menuSubHideAll() {
	$(submenu).children().hide();
}

// jObj is $('ul li', mainmenu)
// aType can be a variety of filters
function menuSubShow(jObj, aType) {
	if (aType == null) {
		aType = 'a';
	}

	// get the active main menu item
	var oMain = jObj.children(aType);
	if (!oMain.length) return;

	// get approprate submenu of main menu item
	var mainID = $.Util.id(oMain);	
	var oSub = $('#submenu-' + mainID.split('-')[1]);
	if (!oSub.length) return;
	var oSubEWidth = 0;

	// show designated submenu
	// must be displayed to compute centers
	oSub.show();

	// to hell with IE below 7.0
	// everything below tries to get the submenu items centered around the x midpoint of the main menu item
	if ($.browser.msie && ($.browser.version < 7)) return;

	// get the relative center of the active main menu item
	var oMainCenter = (oMain.get(0)).offsetLeft + (oMain.width() / 2); // point for submenu to center at
	// sum the widths of active submenu elements
	$('li a', oSub).each(function() {
		oSubEWidth += this.offsetWidth;
	});

	var oSubEWidthHalf = oSubEWidth / 2;
	// if the center of the entire submenu elements is larger than the main menu item's center
	// no need to continue as the submenu will have to be farther left, so off the layout
	if (oSubEWidthHalf >= oMainCenter) {
		return;
	}
	// right half of submenu will go over the overall width of the submenu
	else if (((oSub.get(0)).offsetWidth - oMainCenter - oSubEWidthHalf) < 0) {
		$('li:first a:first', oSub).css('margin-left', Math.ceil((oSub.get(0)).offsetWidth - oSubEWidth - 20) + 'px');
	}
	else {
		$('li:first a:first', oSub).css('margin-left', Math.ceil(oMainCenter - oSubEWidthHalf) + 'px');
	}
}

$(document).ready(function() {
	mainmenu = document.getElementById('mainmenu');
	mainmenu.checkInterval = null;
	mainmenu.menuCursor = false;

	submenu = document.getElementById('submenu');
	submenu.menuCursor = false;

	// hide submenus
	menuSubHideAll();

	// no need to run this if menu doesn't have any elements
	if (!$('ul li', mainmenu).length) return;

	// show active item's submenu
	menuSubShow($('ul li', mainmenu), 'a[class=active]');

	// attach hover event to show and hide submenus
	$('ul li', mainmenu).hover(
		function() {
			menuSubHideAll();
			menuSubShow($(this), 'a');
		},
		function() {
			// only start interval only if none already exists
			if (mainmenu.checkInterval == null) {
				// sees if cursor still in either main or sub menus
				// check every 500 milliseconds
				mainmenu.checkInterval = window.setInterval('menuCursorCheckPosition()', 500);
			}
		}
	);

	$('ul', mainmenu).hover(
		function() {
			mainmenu.menuCursor = true;
		},
		function() {
			mainmenu.menuCursor = false;
		}
	);

	$(submenu).hover(
		function() {
			submenu.menuCursor = true;

			// keep matching mainmenu item style at hover
			if ($('ul:visible', this).length) {
				var submenuID = $.Util.id($('ul:visible:first', this)[0]);
				$('#mainmenu ul li').children('#mainmenu-' + submenuID.split('-')[1]).addClass('hover');
			}
		},
		function() {
			submenu.menuCursor = false;
			$('#mainmenu ul li a').removeClass('hover');
		}
	);
});
