/*
 *  Any javascript code necessary to power the top_nav_include file, goes here
 *
 */
var M_objGearbagQuantity;
var M_objGearbagTotalPrice;
var M_objHiddenHolster;
var M_objSignIn;
var M_objGearbag;
var M_objToggleContainer;
var M_objMyGear;
var M_objHolster;
var M_objTray;
var myHeight;
var myHeight2;
var M_objCartWrapper;
var M_ToggleOnImgSrc;
var M_ToggleOffImgSrc;
var M_CheckoutBar;
var M_GearbagItems;
var M_SavedItems;
var M_bIsIframeLoaded = false;
var M_objNavWrapper;
var M_bShowProductOptions = false;
var M_bMenuItemShow = false;
var M_resultingPageNoServlet
var M_haveResultingPageNoServlet = false;



var M_arrTopMenu = new Array( 
		new Array ( "dvNavClothing", "imgClothingOff", false, ""),
		new Array ( "dvNavSports", "imgSportsOff", false, ""),
		new Array ( "dvNavActivism", "imgActivismOff", false, ""),
		new Array ( "dvNavCampfire", "imgCampfireOff", false, "")
		);

var M_intArrImgOff						= 0;
var M_intArrImgOn						= 1;
var M_bIsOver							= 2;
var M_intArrTogglePosition				= 3;
var M_intArrImgOffPosition				= 4;


var M_bIsMInitialized 					= false;
var M_pntShowDropdowns					= function(){};
var M_pntHideDropdowns					= function(){};
var M_pntHolsterShowDropdowns			= function(){};
var M_pntHolsterHideDropdowns			= function(){};
var M_pntPageLevelFunctions				= function(){};
var M_objGearBagAjax;
var M_strTNPageName						= "TopNav.js";
var M_bSetGearSavedImages         = false;
var M_bIsGearbagRefreshed					= false;
var M_bIsProdLinkWinOpen					= false;
var M_pntDoHolster								= function(){};

window.onresize = ReloadWindow;

/* This code runs if a user resizes their window. */
function ReloadWindow() {
	try {
		if( M_bIsMInitialized ) {
			// If not overridden in a page, this does nothing.  Else, it will fire page events we want to happen on window resize
			M_pntPageLevelFunctions();
		}
	}
	catch( e ) {
		HandleError( "ReloadWindow", e, M_strTNPageName );
	}
}


/* This method iterates through the items in our TopMenu
   array, instantiates 'toggler' objects, retracts them and adds them to our array*/
function InitializeTopNav(resultingPage) {
	try {
		// Initialize the top nav menu items
		M_arrTopMenu.each(function(node) {
				var dvToggle = new fx.Height(node[M_intArrImgOff], {duration: 400});
				dvToggle.hide();
			
				node[M_intArrTogglePosition] = dvToggle;
		});

		
//		InitializeHolster(resultingPage);
		
		M_bIsMInitialized = true;
	}
	catch( e ) {
		HandleError( "InitializeTopNav", e, M_strTNPageName );
	}
}

/* Gets the position of the image we are overlaying, closes anyother open menu windows and displays the one the user is mousing over*/
function ShowMenuItem( iIndex ) {
	try {
		// If we're not initialized - leave
		if( !M_bIsMInitialized ) return;
		M_arrTopMenu[iIndex][M_bIsOver] = true;
		Element.setStyle(M_arrTopMenu[iIndex][M_intArrImgOff],{display:'block'});
		M_arrTopMenu[iIndex][M_intArrTogglePosition].toggle();
		M_bMenuItemShow = true;
		
		// If there are any dropdowns to be hidden on the page, this needs to be overridden at the page level
		SetTimeout( 'M_pntHideDropdowns();', .5 );
		
	}
	catch( e ) {
		HandleError( "ShowMenuItem", e, M_strTNPageName );
	}
}

function CheckEm( intIndex, imgOff, imgOn ) {
	try {
		if( !M_bIsMInitialized ) {
			M_arrTopMenu[intIndex][M_intArrImgOffPosition] = imgOff;
			return;
		}
		
		//MM_swapImage('imgClothingOff','','/images/structure/en_US/top_nav/btn_clothing_gear_over.gif',1); 
		if( M_arrTopMenu[intIndex][M_intArrTogglePosition].isOpen() ) {
			KeepMenuItemOpen(intIndex);
		}
		else {
			M_arrTopMenu[intIndex][M_intArrImgOffPosition] = imgOff;
			
			MM_swapImage(M_arrTopMenu[intIndex][M_intArrImgOn],'',imgOn,1);
			ShowMenuItem(intIndex);
		}
	}
	catch( e ) {
		HandleError( "CheckEm", e, M_strTNPageName );
	}
}

/* Tries to hide a menu item that has been mousedoff.  Runs a few checks before it does anything.
Although the timer is set to 0, it causes enough of a delay to make this work smoothly. */
function HideMenuItem( iIndex ) {
	try {
		// If we're not initialized - leave
		if( !M_bIsMInitialized ) { return; }


		/* If we are still off the window and its not in transition, hide it.  If window is in transition, make recursive calls to ourselves
		   until layer is fully toggled */
		SetTimeout( "if(!M_arrTopMenu["+iIndex +"]["+M_bIsOver+"]){ CloseMenuItem("+iIndex+"); }", 0 );
	}
	catch( e ) {
		HandleError( "HideMenuItem", e, M_strTNPageName );
	}
}

/* Skip the checks, just close the layer*/
function CloseMenuItem( iIndex ) {
	try {
		if( !M_bIsMInitialized ) return;
		
		M_arrTopMenu[iIndex][M_intArrTogglePosition].clearTimer();
		M_arrTopMenu[iIndex][M_intArrTogglePosition].hide();

		// Safari fix - set display to none
		Element.setStyle(M_arrTopMenu[iIndex][M_intArrImgOff],{display: 'none'});
		//$(M_arrTopMenu[iIndex][M_intArrImgOn]).src = M_arrTopMenu[iIndex][M_intArrImgOffPosition];
		MM_swapImage(M_arrTopMenu[iIndex][M_intArrImgOn],'',M_arrTopMenu[iIndex][M_intArrImgOffPosition],1);
		
		M_pntShowDropdowns(M_arrTopMenu[iIndex][M_intArrImgOff]);
	}
	catch( e ) {
		HandleError( "CloseMenuItem", e, M_strTNPageName );
	}
}

/* To get around mouseoff events firing within our div tag, we turn this flag on and off to let us know if we are really off the div tag*/
function KeepMenuItemOpen( intIndex ) {
	try {
		M_bMenuItemShow = true;
		M_arrTopMenu[intIndex][M_bIsOver] = true;
	}
	catch( e ) {
		HandleError( "KeepMenuItemOpen", e, M_strTNPageName );
	}
}


/* Function gets called when you mouse off the options layer. */
function LetMenuItemClose( intIndex ) {
	try {
		M_bMenuItemShow = false;
		M_arrTopMenu[intIndex][M_bIsOver] = false;
		HideMenuItem( intIndex );
	}
	catch( e ) {
		HandleError( "LetMenuItemClose", e, M_strTNPageName );
	}
}

