// preload image icon (for "international info" 
// that appears as secondary menu element under 
// Attractions > Park Map
var intl_info_icon = new Image(22,21); 
intl_info_icon.src="/images/vi_news/i-Logo-xsmall.gif"; 


// ** Sniff for the two major browser types **
	var ie  = (document.all)? true:false
	var moz = ((document.getElementById)&&(!ie))? true:false

// ** Sniff for browser version (used for IE 5 only) **
	var agent = navigator.userAgent.toLowerCase();
	var intAppVersion = parseInt(navigator.appVersion);
	var ie5_0 = (ie && (intAppVersion == 4) && (agent.indexOf("msie 5.0")!=-1) );
	var ie5_5 = (ie && (intAppVersion == 4) && (agent.indexOf("msie 5.5") !=-1));

// ** Sniff for Mac (used for IE Mac browsers) **
	var mac = (agent.indexOf("mac") > -1)? true:false

// ** Global variables **
	var layerNavMenu, layerNavSecMenu, layerMenuDiv, layerMenuSecDiv;
	var currentMenu, currentSecMenu, currentMenuWidth, currentSecMenuWidth;
	var memCurrentMenu = "";
	var memLayerMenuDiv = "";
	var memCurrentSecMenu = "";
	var memLayerMenuSecDiv = "";
	var isOutsideMenus = true;
	var isOutsideSecMenus = true;
	var constFullPageWidth = 800;
	var constTopNavWidth = 599;
	var PageLoaded = false;

// ** If these are set to 0 the code will automatically calculate and populate the values based on the widest menu entry. **
// ** Setting values here will bypass the calculations. **
// ***** WARNING!!!:  
// SETTING ANY OF THESE VALUES TO 0 (zero) 
// WILL BREAK THE DROP-DOWN NAV IN IE 7 !!!
	var navMenuWidth = new Array();
	navMenuWidth["Attractions"] = 150;
	navMenuWidth["VisitorInfo"] = 148;
	navMenuWidth["AnnualPass"] = 178;
	navMenuWidth["Tickets"] = 150;
	navMenuWidth["AttrParkOverview"] = 145;
	navMenuWidth["AttrStudioTour"] = 180;
	navMenuWidth["AttrRides"] = 275;
	navMenuWidth["AttrDining"] = 120;
	navMenuWidth["AttrShopping"] = 143;
	navMenuWidth["AttrParkMap"] = 225;
	navMenuWidth["VisTransport"] = 140;
	navMenuWidth["VisGuestServices"] = 165;
	navMenuWidth["VisParkmap"] = 170;
	navMenuWidth["VisRideRestric"] = 100;
	navMenuWidth["TicAll"] = 130;
	navMenuWidth["TicGroup"] = 250;
	navMenuWidth["TicFront"] = 220;
	
	navMenuWidth["JobDescrips"] = 155; 
	//navMenuWidth["AboutUs"] = 95;
	//navMenuWidth["AuditionRequirements"] = 150;
	//navMenuWidht["AuditionSchedule"] = 150;
	
	//navMenuWidth["MiniAttractions"] = 170; 
	navMenuWidth["MiniParkMaps"] = 170;
	//navMenuWidth["MiniDirections"] = 150;
	//navMenuWidht["MiniParkHours"] = 150;
	
	navMenuWidth["TicketQLinks"] = 180;

// ** Returns a DHTML reference to a layer.  It makes sure that if a layer doesn't exist **
// ** the function will return null instead of a JavaScript error. **
	function getLayerRef(layerName) {
		var layerRef;
		if (ie) {
			if (document.all[layerName]) {
				layerRef = document.all[layerName];
				HideText = "hidden";
			} else {
				layerRef = null;
			}
		} else {
		    // default to NS6+/Firefox
			if (document.getElementById(layerName)) {
				layerRef = document.getElementById(layerName);
				HideText = "hidden";
			} else {
				layerRef = null;
			}
		}

		return layerRef;
	}


// ** Functions to determine the browser window width and height **
	function getInsideWindowWidth() {
		if (ie) {
			return document.body.scrollWidth
		} else {
			return document.width
		}
	}

	function getInsideWindowHeight() {
		if (ie) {
			return document.body.scrollHeight
		} else {
			return document.height
		}
	}


// ** Calculate the position of the rolled over item from the mouse event. **
	function getEventXOffset(evt) {
		if (ie5_0 || ie5_5) {
			return document.body.scrollLeft - document.body.clientLeft + evt.clientX - evt.offsetX;
		} else if (ie) {
			return document.documentElement.scrollLeft - document.documentElement.clientLeft + evt.clientX - evt.offsetX;
		} else {
			return evt.pageX - evt.layerX
		}
	}

	function getEventYOffset(evt) {
		if (ie5_0 || ie5_5) {
			return document.body.scrollTop - document.body.clientTop + evt.clientY - evt.offsetY;
		} else	if (ie) {
			return document.documentElement.scrollTop - document.documentElement.clientTop + evt.clientY - evt.offsetY;
		} else {
			return evt.pageY - evt.layerY
		}
	}

// ** returns the width of a layer **
	function getLayerWidth(layer) {
		if (ie) {
			return layer.scrollWidth;
		} else {
			return layer.offsetWidth;
		}
	}



// ** Initialize the top nav layers and sets PageLoaded to true. **
	function initMenus() {
		layerNavMenu = getLayerRef("navMenuContainer");
		layerNavSecMenu = getLayerRef("navSecMenuContainer");
		PageLoaded = true;
	}


//**********************************
//** Top Nav DHTML Menu Functions **
//**********************************

// ** Calculates max width of layer (if not already set), and sets the outer div to that size. **
	function calcAndSetWidth(menuName, isSecondary) {
		var i, menuToCalc;
		menuToCalc = isSecondary ? layerNavSecMenu : layerNavMenu;
		if (navMenuWidth[menuName] <= 0) {
		// ** Calculate max width of inner DIVs **
			for (i=0; i<menuToCalc.childNodes.length; i++) {
				if (menuToCalc.childNodes[i].offsetWidth > navMenuWidth[menuName]) {
					navMenuWidth[menuName] = menuToCalc.childNodes[i].offsetWidth;
				}
			}
		}
		
		if (isSecondary) {
			currentSecMenuWidth  = navMenuWidth[menuName];
		} else {
			currentMenuWidth = navMenuWidth[menuName];
		}

		menuToCalc.style.width = navMenuWidth[menuName] + "px";
	}


// ** Menu item rollover functions **
	function menuItemRollOver(layer, isSecondary) {
		if (PageLoaded) {
			layer.style.backgroundColor = "#f1c00f";			
			showDropDownMenu(isSecondary);
		}
	}

	function menuItemRollOff(layer, isSecondary) {
		if (PageLoaded) {
			layer.style.backgroundColor = "#1a6092";
			first = layer.href.lastIndexOf('/') + 1;
			last = layer.href.lastIndexOf('.');
			imgName = layer.href.substring(first, last);
			if (isSecondary) {
				hideSecDropDownMenu(true);
			} else {
				hideDropDownMenu(true);
			}
		}
	}


// ** Top Nav Dropdown functions **
// ** Check PageLoaded on these just in case to avoid javascript errors **
	function showDropDownMenu(isSecondary) {
		var menuToShow;
		if (PageLoaded) {
			menuToShow = isSecondary ? layerNavSecMenu : layerNavMenu;
			if (isSecondary) {
				isOutsideSecMenus = false;
				isOutsideMenus = false;
			} else {
				isOutsideMenus = false;
			}
			menuToShow.style.visibility = "inherit";
			if (!isSecondary) {
				layerMenuDiv.childNodes[0].style.visibility = "inherit";
			}
		}
	}

	function hideDropDownMenu(FirstTry) {
		if (PageLoaded) {
			if (FirstTry) {
				isOutsideMenus = true;
				memLayerMenuDiv = layerMenuDiv;
				memCurrentMenu = currentMenu;
				setTimeout("hideDropDownMenu(false)", 50);
			} else if (isOutsideMenus) {
				layerNavMenu.style.visibility = "hidden";
				//** Must split these two if statement, because the second one will fail if the first is true. **
				if (memLayerMenuDiv) {
					if (memLayerMenuDiv != "") {
						memLayerMenuDiv.childNodes[0].style.visibility = "hidden";
					}
				}
				memLayerMenuDiv = "";
				memCurrentMenu = "";
			}
		}
	}

	function hideSecDropDownMenu(FirstTry) {
		if (PageLoaded) {
			if (FirstTry) {
				isOutsideSecMenus = true;
				isOutsideMenus = true;
				memLayerMenuSecDiv = layerMenuSecDiv;
				memCurrentSecMenu = currentSecMenu;
				setTimeout("hideSecDropDownMenu(false)", 50);
				setTimeout("hideDropDownMenu(false)", 150);
			} else if (isOutsideSecMenus) {
				layerNavSecMenu.style.visibility = "hidden";
				memLayerMenuSecDiv = "";
				memCurrentSecMenu = "";
			}
		}
	}

	function openDropDownMenu(menuName, topDiv, evt, isSecondary) {
		var MenuEdge, menuXPosition, menuYPosition, thisMenu;
		
		/* 	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 	*/
		/* 	"ENTERTAINMENT JOBS: MiniSite" || TopNav's "Job Descriptions" dropdown menu
			This sets a flag to test whether this file was called via 
				the "JobDescrips" link of the "Entertainment Jobs" section's top nav bar.
		*/
		var isEntJobs = false;
		if (menuName == "JobDescrips")
		{
			isEntJobs = true;
		}
		
		isTicketQL = false;
		if (menuName == "TicketQLinks")
		{
			isTicketQL = true;
		}
		/* END CODE to set flag
			(see below for related code)
			-- Brian Janko:  May 26, 2006
		/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
				
		if (!evt) evt = event;
		
		if (PageLoaded) {
			thisMenu = isSecondary ? layerNavSecMenu : layerNavMenu;

		// ** Hide menu and move it off the page.  This resolves menu flicker. **
			if (isSecondary) {
				isOutsideSecMenus = true;
				hideSecDropDownMenu(false);
			} else {
				isOutsideMenus = true;
				hideDropDownMenu(false);
			}
			
			thisMenu.style.left = -500;
			thisMenu.style.top = -500;

			//alert(getLayerRef('nav' + menuName).innerHTML);
		// ** Copy the contents of the current menu div at the bottom into the menu container div **
			thisMenu.innerHTML = getLayerRef('nav' + menuName).innerHTML;

			if (ie) {
				thisMenu.style.width = "10px";
			} else {
				thisMenu.style.width = "auto";
			}

			calcAndSetWidth(menuName, isSecondary);

	// ** Calculate right edge of menu.  If it's past the edge of the top nav, align it so it is not past it. **
	// ** For secondary menus, move the menu to the left of the primary menu when this happens **

			menuXPosition = getEventXOffset(evt);
			menuYPosition = getEventYOffset(evt);

			LeftPageStart = (getInsideWindowWidth()-constFullPageWidth)/2;
			MenuEdge = menuXPosition + getLayerWidth(thisMenu) - LeftPageStart;

			if (MenuEdge > constTopNavWidth) {
	// ** The right edge of the menu is beyond the designed-for constTopNavWidth.  Adjust menuXPosition appropriately.
				if (isSecondary) {
					menuXPosition -= currentSecMenuWidth;
					menuXPosition -= currentMenuWidth;
					menuXPosition += 5;
					if (ie) {
						menuXPosition -= 3;
					}
				} else {
					menuXPosition -= (MenuEdge-constTopNavWidth) - 3;
				}

				if (moz) {
					menuXPosition -= 3;
				} else if (ie && mac)  {
					menuXPosition -= 2;
				}

				if (ie5_5) {
					menuXPosition -= 2;
				}
			}

			menuYPosition +=1;

		// ** Put in IE Mac position fixes here if we need them (not tested). **				
			if (ie && mac) {
				menuXPosition -= 23;
			}
			
			if (ie) {
				//menuXPosition -= 2;
				menuYPosition -= 1;
			}
			
			if (ie5_0 || ie5_5) {
				menuXPosition += 2;
			}
			
			if (isSecondary) {
				menuXPosition += currentMenuWidth;
				if (topDiv.className == "firstli")
				{
					if (!ie) {
						menuYPosition -= 2;
					}
				}
				if (ie) {
					menuXPosition -= 2;
					menuYPosition -= 31;
				} else {
					menuXPosition -= 1;
					menuYPosition -= 29;
				}
			}
			
			
		/* 	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 	*/
		/* 	"ENTERTAINMENT JOBS: MiniSite" || TopNav's "Job Descriptions" dropdown menu
			   If the isEntJobs flag above has been set to true, then the EntJobs topnav is in use.  
			   So, now, IN ADDITION TO ALL THE PREVIOUS CROSS-PLATFORM/BROWSER ADJUSTMENTS, 
			   this further increments or decrementeds the X and Y positions of the dropdown.
		*/
 			if (isEntJobs == true)
			{
				if (ie5_5)
					menuXPosition += -2; 
				if (moz)
					menuXPosition += 0; 
				else
					menuXPosition += 1; 
					
				menuYPosition += 0; 
			}
		/* END CODE to adjust the X and Y positions of the dropdown on the Entertainment Jobs section topnav
			(see above for related code)
			-- Brian Janko:  May 26, 2006 (modified July 7, 2006)
		/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
			
		
		// ** Set left & top positions. **
			thisMenu.style.left = menuXPosition + "px";
			
			if (isTicketQL) {
				layerMenuDiv = topDiv;
				currentMenu = menuName;
				menuYPosition = menuYPosition + 23;
			} else if (isSecondary) {
				layerMenuSecDiv = topDiv;
				currentSecMenu = menuName;
				menuYPosition = menuYPosition + 30;
			} else {
				layerMenuDiv = topDiv;
				currentMenu = menuName;
				menuYPosition = menuYPosition + 47;
			}
			thisMenu.style.top = menuYPosition + "px";

			showDropDownMenu(isSecondary);
		}
	}
	
	function rollOverNoMenu(topDiv) {
		if (PageLoaded) {
			topDiv.childNodes[0].style.visibility = "visible";
		}
	}
	
	function rollOutNoMenu(topDiv) {
		if (PageLoaded) {
			topDiv.childNodes[0].style.visibility = "hidden";
		}
	}


// ** Generic Function Library **

	function divLink(url) {
		document.location.href = url;
	}
	
	


// Image Rollovers
// An alternate to using the recommended CSS functionality
// when the latter is not a possibility for some reason.

// The JavaScript remembers the original image URL, and thus a file
// does not need to be passed unless you're rolling out to an image
// that is different from the one originally loaded.

var oldImages = new Array();
var imgPath   = 'images/topnav/'; // a path to the images if all the same (else blank)

function imageSwap(imgName, newImage, setPath) {

  if( newImage != undefined && newImage != null ) {
  // Store the old image for when it needs to be reverted
  var oldImageParts  = new Array();

    oldImageURL        = document.getElementsByName(imgName)[0].src;
    oldImageParts      = oldImageURL.split('/');
    oldImages[imgName] = oldImageParts[oldImageParts.length - 1];

  } else {
  // Set the image to be loaded to the old (original) value
    if( oldImages[imgName] != undefined ) {
      newImage = oldImages[imgName];
    } else {
      // Not enough data to work with
      return false;
    }
  }

  // Swap the path if requested
  imgPath = (setPath != undefined) ? setPath : imgPath;

  // Swap the image with another
  document.getElementsByName(imgName)[0].src = imgPath + newImage;

} // ! imageSwap function
