
/* PriceSmart.com Javascript code for Navigation Menus */

/* VARIABLES */

// Hold the list of menus that are currently visible so we can hide them when needed
var g_aVisibleMenus = new Array();
var g_iMenuTimer = Number(0);

/* FUNCTIONS */

// Position a menu relative to it's parent and then show it immediately
function ShowChildMenu( strMenuID, strParentID, strParentParentID ) {

	// Relocate the child
	if ( strParentID != "" ) {
		var objPage = DocumentObject( "page", false );
		var objParent = DocumentObject( strParentID, false );
		var objChild = DocumentObject( strMenuID, true );
		
		var aParentLocation = DocumentObjectPosition( objParent );
		var aPageLocation = DocumentObjectPosition( objPage );
		
		//alert( aParentLocation[0] + ', ' + aParentLocation[1] );
		
		objChild.top = String( Number( aParentLocation[1] ) - Number( aPageLocation[1] ) ) + 'px';
		objChild.left = String( Number( aParentLocation[0] ) - Number( aPageLocation[0] ) + objParent.offsetWidth + 5 ) + 'px';
	}		

	// Make the menu visible
	ShowMenu( strMenuID, true, strParentParentID );
}

// Wait until later and then show menu
function ShowMenuLater(strMenuID, bHideOthers, strExceptHide) {
    // Clear the timeout, if any
    clearTimeout(g_iMenuTimer);
    // Set a new timer
    g_iMenuTimer = setTimeout(function () { ShowMenu(strMenuID, bHideOthers, strExceptHide); }, 1000);
}

// Immediatly show the given menu and optionally hide any other visible menus
function ShowMenu( strMenuID, bHideOthers, strExceptHide ) {

	// Hide all currently visible menus, if needed
	if ( bHideOthers ) {
		HideMenusNow(strExceptHide);
	}

	// Make the menu object visible
	DocumentObject( strMenuID, true ).display = 'block';
	
	// Add this menu to the list of visible menus, if it's not already there
	bExists = false;
	for ( iMenu=0; iMenu<g_aVisibleMenus.length; iMenu++ ) {
		if ( g_aVisibleMenus[iMenu] == strMenuID ) {
			bExists = true;
		}
	}
	if ( ! bExists ) {
		g_aVisibleMenus[g_aVisibleMenus.length] = strMenuID;
	}
}

// Wait until later and then hide any visible menus
function HideMenusLater( ) {
	// Clear the timeout, if any
	clearTimeout( g_iMenuTimer );
	// Set a new timer
	g_iMenuTimer = setTimeout( 'HideMenusNow("");', 1000 );
}

// Immediately hide any visible menus
function HideMenusNow( strExcept ) {

	strExcept = String( strExcept );
	
	// Clear the menu timer, if any
	clearTimeout( g_iMenuTimer );
	g_iMenuTimer = 0;

	// Loop through and hide each item in the menu array
	for( iMenu=0; iMenu<g_aVisibleMenus.length; iMenu++ ) {
		var strMenu = g_aVisibleMenus[iMenu];
		if ( strMenu != ""  &&  strMenu != strExcept  &&  strMenu != "undefined" ) {
			DocumentObject( strMenu, true ).display = 'none';
		}
	}
	
	// Remove all of the elements from the array
	g_aVisibleMenus.splice(0,g_aVisibleMenus.length);
	
	// Add back the exception
	if ( strExcept != ""  &&  strExcept != "undefined" ) {
		g_aVisibleMenus[g_aVisibleMenus.length] = strExcept;
	}
}

// Stops any visible menus from automatically hiding
function PauseHideMenus( ) {
	// Clear the menu timer, if any
	clearTimeout( g_iMenuTimer );
	g_iMenuTimer = 0;
}

// Handle the click on one of the ecom tabs
function EcomTabClick( strSelectedTab ) {
	
	// Show/hide the correct tab
	DocumentObject('tab-shop-online-nav-off',true).display = ( strSelectedTab == "shop-online" ) ? 'none' : 'block';
	DocumentObject('tab-shop-online-nav-on',true).display = ( strSelectedTab == "shop-online" ) ? 'block' : 'none';
	DocumentObject('tab-in-the-clubs-nav-off',true).display = ( strSelectedTab == "in-the-clubs" ) ? 'none' : 'block';
	DocumentObject('tab-in-the-clubs-nav-on',true).display = ( strSelectedTab == "in-the-clubs" ) ? 'block' : 'none';
	DocumentObject('tab-business-services-nav-off',true).display = ( strSelectedTab == "business-services" ) ? 'none' : 'block';
	DocumentObject('tab-business-services-nav-on',true).display = ( strSelectedTab == "business-services" ) ? 'block' : 'none';

	
	// Show/hide the correct contents
	DocumentObject('tab-shop-online-contents',true).display = ( strSelectedTab == "shop-online" ) ? 'block' : 'none';
	DocumentObject('tab-in-the-clubs-contents',true).display = ( strSelectedTab == "in-the-clubs" ) ? 'block' : 'none';
	DocumentObject('tab-business-services-contents',true).display = ( strSelectedTab == "business-services" ) ? 'block' : 'none';

}
