﻿var timerId = 0;
var subMenu = null;
var maximumWidth = 960;

function PrepareDropMenus( ParentObj, maxWidth ) {
	maximumWidth = maxWidth;

	// Setup submenus
	//
	ParentObj.mouseover(function() {
		ShowMenu($(this).children('ul:first'));
	}).mouseout(function() {
		timerId = setTimeout(HideMenu, 100);
	});
}

// show menu
//
function ShowMenu(oLayer) {

	// Kill existing timer if other menu is waiting to hide
	//
	if (timerId != 0)
		clearTimeout(timerId);
	timerId = 0;

	// If another menu is displayed, get rid of it. Requires RenderItemId="true" on CMSListMenu
	//
	if ((subMenu != null) && (subMenu.attr('id') != oLayer.attr('id')))
		HideMenu();
	subMenu = oLayer;

	// Submenu needs to be at least as wide as parent, otherwise looks odd
	//
	if (oLayer.parent().width() - 23 > oLayer.width())
		oLayer.width(oLayer.parent().width() - 23);
	oLayer.show();

	if (subMenu != null) {
		// Make sure drop down stays within the right boundary of the design to make sure it does not go off screen
		//
		if (oLayer.offset().left + oLayer.width() > maximumWidth)
			oLayer.css('left', oLayer.position().left + (maximumWidth - oLayer.offset().left - oLayer.width()));

		// show IFRAME under drop down to prevent elements on page from interrupting the mouseout
		//
		$('#iframe').width(oLayer.width()).height(oLayer.height()).css('left', oLayer.offset().left).css('top', oLayer.offset().top).show();
	}
}

function HideMenu() {
	// hide IFRAME
	//
	$('#iframe').hide();

	// hide menu
	//
	subMenu.hide();
	subMenu = null;
}
