/***********************************************************************************
	Disney's Cross-Browser JavaScript DHTML Menu (1.1)
	Website: Walt Disney World Youth Programs
	Created by: Brian Kueck
	Author: Agency Services
	Date: Oct. 2007+
	Supports: .png transparencies, drop shadows & fast CSS/JS image switching.
	Works in: IE 6, IE 7, FF 1.5, FF 2, FF 3.0a8, Safari 3, Opera 9.24, Netscape 9
	Version History: 
		1.0 - 2007 - Initial Build.
		1.1 - 2008 - Added IFrame for IE 5.5 & IE 6.0 to avoid select box bleed through.
					 Corrected the program logo glitch for IE 6.0. Added Trace Divs.
					 Stabilization fix to keep menu from closing when mousedover.
					 Added program logo pre-caching and faster image swapping.
	(c) Disney. All rights reserved.
***********************************************************************************/

/* INITIALIZATION */

	var menu = {};
		menu.debug = 0;
		menu.visibility = 0; // Keep us from hitting the DOM every time the menu is flipped. JS = faster! DOM hits are slow.
		menu.timeout = {};
		menu.lastImgID = null;
		menu.traceDiv = null;
		menu.clearTraceDiv = null;
		menu.isBadIE = false;
		menu.lastFunctionName = '';

	var browser = {};
		browser.URI = window.location.href;
		browser.protocol = browser.URI;
		browser.protocol = browser.protocol.substring(0,browser.protocol.indexOf(':'));
		browser.hostName = browser.URI;
		browser.hostName = browser.hostName.replace(/https:\/\//,'');
		browser.hostName = browser.hostName.replace(/http:\/\//,'');
		browser.hostName = browser.hostName.substring(0,browser.hostName.indexOf('/'));
		/* Don't use these! They won't pre-cache, but will cause the site to constantly download images!
		browser.hostName = browser.hostName.replace(/disneyyouthgroups/,'adisneyworld');
		browser.hostName = browser.hostName.replace(/secure-wdyp/,'adisneyworld'); */
		//browser.hostName = browser.hostName.replace(/disneyyouthgroups/,'adisneyyouthgroups');
		//alert(browser.protocol + '|' + browser.hostName);

	var imageList = {}; // Program Logos
		imageList.URL = browser.protocol + '://' + browser.hostName + '/media/wdyp/media/en_US/global/menu/menu_logo_';
		//alert(imageList.URL);
		imageList.ext = '.gif';
		imageList.PA = new Image(); // Program Assistant
		imageList.PA.src = imageList.URL + 'PA'.toLowerCase() + imageList.ext;
		imageList.MMD = new Image();
		//imageList.MMD.src = imageList.URL + 'MMD'.toLowerCase() + imageList.ext;
		imageList.MMD.src = imageList.URL + 'MMD'.toLowerCase() + '.jpg';
		imageList.FD = new Image();
		imageList.FD.src = imageList.URL + 'FD'.toLowerCase() + imageList.ext;
		//imageList.FD.src = imageList.URL + 'FD'.toLowerCase() + '.jpg';
		imageList.DH = new Image();
		imageList.DH.src = imageList.URL + 'DH'.toLowerCase() + imageList.ext;
		imageList.PAW = new Image();
		imageList.PAW.src = imageList.URL + 'PAW'.toLowerCase() + imageList.ext;
		imageList.DSN = new Image();
		imageList.DSN.src = imageList.URL + 'DSN'.toLowerCase() + imageList.ext;
		imageList.JC = new Image();
		imageList.JC.src = imageList.URL + 'JC'.toLowerCase() + imageList.ext;
		imageList.GN = new Image();
		imageList.GN.src = imageList.URL + 'GN'.toLowerCase() + imageList.ext;
		imageList.D8GGJ = new Image();
		imageList.D8GGJ.src = imageList.URL + 'D8GGJ'.toLowerCase() + imageList.ext;
		imageList.NOJ = new Image();
		//imageList.NOJ.src = imageList.URL + 'NOJ'.toLowerCase() + imageList.ext;
		imageList.NOJ.src = imageList.URL + 'NOJ'.toLowerCase() + '.jpg';
		imageList.TPA = new Image();
		imageList.TPA.src = imageList.URL + 'TPA'.toLowerCase() + imageList.ext;
		imageList.YES = new Image();
		imageList.YES.src = imageList.URL + 'YES'.toLowerCase() + imageList.ext;
		imageList.HSD = new Image();
		imageList.HSD.src = imageList.URL + 'HSD'.toLowerCase() + imageList.ext;
		imageList.YES_OE = new Image();
		//imageList.OpenEnrollment.src = imageList.URL + 'OE'.toLowerCase() + imageList.ext;
		imageList.YES_OE.src = imageList.URL + 'OE'.toLowerCase() + '.jpg';
		//alert(imageList.OpenEnrollment.src);

	var iframe = {};
		iframe.object = null;
		iframe.visibility = 0;

/* BEGIN TRACE DIV SECTION */

	menu.trace = function(strDebugInfo) {
		/*if (menu.debug) {
			menu.lastFunctionName = 'menu.trace';
			//menu.trace(menu.lastFunctionName);
		}*/
		var obj = menu.traceDiv;
		if ((menu.debug) && (obj)) {
			if ((strDebugInfo.indexOf('iframe.') > -1) || (strDebugInfo.indexOf('menu.') > -1)) {
				strDebugInfo = '<b>' + strDebugInfo + '</b> (' + menu.visibility + '/' + iframe.visibility + ')';
			}
			if ((strDebugInfo.indexOf('<BR/>') > -1) || (strDebugInfo.indexOf('<HR/>') > -1)) {
				obj.innerHTML += strDebugInfo + '\n';
			} else {
				obj.innerHTML += strDebugInfo + '<BR/>\n';
			}
			
			//if ((obj.innerHTML.indexOf('<!--BEGIN-->') > 0) && (obj.innerHTML.indexOf('<!--END-->') > 0)) {
				//var divStyle = ' style="background-color:#AAFFAA; border: 1px solid black; margin-top: 3px; padding: 0px 5px 0px 5px; overflow: hidden; text-overflow: ellipsis;"';
				//var divStyle = '';
				//obj.innerHTML = obj.innerHTML.replace('<!--BEGIN-->','<div'+divStyle+'>');
				//obj.innerHTML = obj.innerHTML.replace('<!--END-->','</div>');
			//}
			
			menu.coords();
		}
		/*if (menu.debug) {
			//menu.trace('<HR/>');  
		}*/
	}

	menu.coords = function() {
		/*if (menu.debug) {
			menu.lastFunctionName = 'menu.coords';
			//menu.trace(menu.lastFunctionName);
		}*/
		var obj = menu.coordsDiv;
		if (obj) {
			obj.innerHTML = '<b>Coords:</b><HR/>\n';
			obj.innerHTML += 'menu.Visibility: ' + menu.visibility + '<BR/>\n';
			obj.innerHTML += 'iframe.Visibility: ' + iframe.visibility + '<BR/>\n';
			var objMenu = document.getElementById('ProgramsMenu');
			obj.innerHTML += 'typeof: ' + typeof(menu.timeout[objMenu.id]) + '<BR/>\n';
			obj.innerHTML += 'b4: ' + menu.timeout[objMenu.id] + '<BR/>\n';
			menu.timeout[objMenu.id] = null;
			menu.timeout[objMenu.id] = undefined;
			obj.innerHTML += 'after: ' + menu.timeout[objMenu.id] + '<BR/>\n';
		}
		/*if (menu.debug) {
			//menu.trace('<HR/>');  
		}*/
	}

	menu.clearDiv = function(divName,strDefault) {
		var obj = menu[divName];
		if (obj) {
			obj.innerHTML = strDefault;
		}
		if (menu.debug) {
			menu.lastFunctionName = 'menu.clearTrace';
			//menu.trace(menu.lastFunctionName);
			//menu.trace('<HR/>'); 
		}
	}

	menu.buildDiv = function(divName,divHeight,divWidth,divLeft,divTop,backgroundColor) {
		/*if (menu.debug) {
			menu.lastFunctionName = 'menu.buildDiv';
			//menu.trace(menu.lastFunctionName);
		}*/
		// The (!menu.traceDiv) check will prevent this from being recreated every time that the browser is resized.
		if (!menu[divName]) {
			var div = document.createElement('DIV');
			div.setAttribute('id',divName);
			div.style.backgroundColor = backgroundColor;
			div.style.border = '1px solid black';
			div.style.left = divLeft + 'px';
			div.style.height = divHeight + 'px';
			div.style.padding = '0px 5px 2px 5px';
			div.style.position = 'absolute';
			div.style.overflow = 'auto';
			div.style.textAlign = 'left';
			div.style.top = divTop + 'px';
			div.style.width = divWidth + 'px';
			div.style.zIndex = '10000';
			menu[divName] = div;
			document.body.appendChild(menu[divName]);
			menu.setOpacity(divName,7);
		}
		/*if (menu.debug) {
			//menu.trace('<HR/>');  
		}*/
	}

	menu.buildDivs = function() {
		if (menu.debug) { 
			menu.lastFunctionName = 'menu.buildDivs';
			//menu.trace(menu.lastFunctionName);
		}
		// Build Clear Trace Div
		menu.buildDiv('clearTraceDiv',15,100,0,130,'#FFAAAA');
		var strLink = '<a href="javascript:void(0);" onClick="if ((typeof(menu) != ' + "\'undefined\'" + ') && (typeof(menu.clearDiv) != ' + "\'undefined\'" + ')) menu.clearDiv(' + "\'traceDiv\',\'<b>Trace Div:</b><HR/>\'" + ');">Clear Trace</a>';
		menu.clearDiv('clearTraceDiv',strLink);

		// Build Trace Div
		menu.buildDiv('traceDiv',550,175,0,153,'#AAAAFF');
		menu.clearDiv('traceDiv','<b>Trace Div:</b><HR/>\n');

		// Build Coords Div
		menu.buildDiv('coordsDiv',110,175,0,710,'#AAFFAA');
		if (menu.debug) {
			//menu.trace('<HR/>');  
		}
	}

/* BEGIN SECTION - MAIN ROUTINES */

	menu.isIE = function() {
		if (menu.debug) {
			menu.lastFunctionName = 'menu.isIE';
			//menu.trace(menu.lastFunctionName);
		}
		if (navigator.appName == 'Microsoft Internet Explorer') {
			//alert(navigator.appName + '|' + navigator.appVersion);
			var objVersion = navigator.appVersion;
			var strVersion = objVersion.substr(objVersion.indexOf('MSIE ')+5,objVersion.length);
			strVersion = strVersion.substr(0,strVersion.indexOf(';'));
			if (strVersion < 7.0) {
				menu.isBadIE = true;
			}
		}
		if (menu.debug) {
			//menu.trace('<HR/>');
		}
	}
	menu.isIE();

	menu.setOpacity = function(divName,opacity) {
		if (menu.debug) {
			menu.lastFunctionName = 'menu.setOpacity';
			//menu.trace(menu.lastFunctionName);
		}
		var obj = document.getElementById(divName);
		//if (obj.style.-moz-opacity) obj.style.-moz-opacity = opacity;
		obj.style.filter = 'alpha(opacity=' + opacity*10 + ')';
		obj.style.opacity = opacity/10;
		if (menu.debug) {
			//menu.trace('<HR/>');  
		}
	}

	menu.findPixelCount = function(coordDir,obj) {
		if (menu.debug) {
			menu.lastFunctionName = 'menu.findPixelCount';
			//menu.trace(menu.lastFunctionName);
		}
		var pixelCount = 0;
		if (obj.offsetParent) {
			while(1) {
				if (coordDir == 'x')
					pixelCount += obj.offsetLeft;
				else if (coordDir == 'y')
					pixelCount += obj.offsetTop;
				if (!obj.offsetParent) break;
				else obj = obj.offsetParent;
			}
		} else if (obj[coordDir]) {
			pixelCount = obj[coordDir];
		}
		if (menu.debug) {
			//menu.trace('<HR/>');  
		}
		return pixelCount;
	}

	menu.resize = function() {
		if (menu.debug) {
			menu.buildDivs();
			// Begin Function
			menu.lastFunctionName = 'menu.resize';
			//menu.trace(menu.lastFunctionName);
			//menu.trace('<!--BEGIN-->');
		}
		if ((document.getElementById('ProgramsButton')) && (document.getElementById('ProgramsMenu'))) {
			var menuImg = document.getElementById('ProgramsButton');
			var newSizeX = menu.findPixelCount('x',menuImg);
			var newSizeY = menu.findPixelCount('y',menuImg);
			var objMenu = document.getElementById('ProgramsMenu');
			if (navigator.userAgent.indexOf('MSIE') > -1) {
				objMenu.style.left = newSizeX + 8 + 'px';
				objMenu.style.top = newSizeY + 23 + 'px';
			} else {
				objMenu.style.left = newSizeX + 8 + 'px';
				objMenu.style.top = newSizeY + 23 + 'px';
			}

			// IE 5, IE 6 fix for select boxes appearing above dynamic divs.
			if (menu.isBadIE) {
				// The (!iframe.object) check will prevent this from being recreated every time that the browser is resized.
				if (!iframe.object) {
					objMenu.style.zIndex = 2;
					
					iframe.object = document.createElement('IFRAME');
					iframe.object.setAttribute('src',imageList.URL + 'PA'.toLowerCase() + imageList.ext);
					iframe.object.setAttribute('frameborder','0');
					iframe.object.setAttribute('marginwidth','0px');
					iframe.object.setAttribute('marginheight','0px');
					iframe.object.setAttribute('scrolling','no');
					iframe.object.setAttribute('id','MSIEIFrame');
					var events = "if ((typeof(menu) != 'undefined') && (typeof(menu.clearTimer) != 'undefined')) menu.clearTimer('+objMenu+')";
					iframe.object.setAttribute('onMouseOver',events);
					iframe.object.style.border = '0px' //'1px solid red';
					iframe.object.style.position = 'absolute';
					iframe.object.style.visibility = 'visible';
					iframe.object.style.zIndex = 1;
					document.body.appendChild(iframe.object);
					if (menu.debug) {
						menu.setOpacity('MSIEIFrame',10);
					} else {
						menu.setOpacity('MSIEIFrame',0);
					}
					iframe.toggleVisibility('iframe','MSIEIFrame','hidden');
				}
				// Resize & reposition after creating iFrame & also when browser resizes.
				var objIFrame = document.getElementById('MSIEIFrame');
				objIFrame.style.left = newSizeX + 0 + 'px';
				objIFrame.style.top = newSizeY + 23 + 'px';
				objIFrame.style.height = 390 + 'px';
				objIFrame.style.width = 184 + 'px';
			}
			/* End IE fix */
		}
		if (menu.debug) {
			//menu.trace('<!--END-->');
			//menu.trace('<HR/>');  
		}
	}

	menu.cleanString = function(str) {
		if (menu.debug) {
			menu.lastFunctionName = 'menu.cleanString';
			//menu.trace(menu.lastFunctionName);
		}
		str = str.replace('2','');
		str = str.replace('LABEL','');
		if (menu.debug) {
			//menu.trace('<HR/>');  
		}
		return str;
	}

	menu.imgSwap = function(obj) {
		if (menu.debug) {
			menu.lastFunctionName = 'menu.imgSwap';
			//menu.trace(menu.lastFunctionName);
		}
		var objMenu = document.getElementById('ProgramsMenu');
		//menu.trace('obj ID: ' + objMenu.id);
		menu.clearTimer(objMenu);
		// Change Logos:
		if (document.getElementById('menuLogos')) {
			//var strSrc = 'http://disneyworld.disney.go.com/media/wdyp/media/en_US/global/menu/menu_logo_' + obj.id.toLowerCase() + '.gif';
			try {
				menu.trace('IMAGE LIST: ' + obj.id.toUpperCase() + ' <font color="green"><b>PASS</b></font>');
			} catch(e) {
				menu.trace('IMAGE LIST: ' + obj.id.toUpperCase() + ' <font color="red"><b>FAIL</b></font>');
			}
			var strSrc = imageList[menu.cleanString(obj.id.toUpperCase())].src;
			document.getElementById('menuLogos').src = strSrc;
		}

		if (navigator.userAgent.indexOf('Firefox') > -1) { // IE 6/7 already has "a:hover" effect which works nicely. Skip so IE runs faster.
			var strImgID = obj.id+'OrangeMedia';
			strImgID = menu.cleanString(strImgID);
			if (strImgID != menu.lastImgID) {
				//menu.switchStyleDisplay(menu.lastImgID,'none');
				iframe.toggleVisibility('img',menu.lastImgID,'hidden');
			}

			if (document.getElementById(strImgID)) {
				//menu.switchStyleDisplay(strImgID,'block');
				iframe.toggleVisibility('img',strImgID,'visible');
				menu.lastImgID = strImgID;
			}
		}
		if (menu.debug) {
			//menu.trace('<HR/>');  
		}
	}

	menu.getID = function(obj) {
		if (menu.debug) {
			//menu.lastFunctionName = 'menu.getID';
			//menu.trace(menu.lastFunctionName);
		}
		if ((obj) && (obj.id)) {
			if (obj.id.indexOf('Menu') < 1) return obj.id+'Menu';
			else return obj.id;
		} else {
			return obj;
		}
		if (menu.debug) {
			//menu.trace('<HR/>');  
		}
	}

	menu.setTimeoutValue = function(obj) {
		if (menu.debug) {
			menu.lastFunctionName = '<font color="maroon">menu.setTimeoutValue</font>';
			//menu.trace(menu.lastFunctionName);
		}
		//menu.trace('obj: ' + obj);
		var menuID = menu.getID(obj);
		menu.obj = menuID;
		//try {
				//menu.trace('<font color="gold"><b>timerID: ' + menuID + '</b></font>');
				menu.timeout[menuID] = setTimeout('menu.closeMenus()', 250);
				//menu.trace('<font color="gold"><b>timer: ' + menu.timeout[menuID] + '</b></font>');
		//} catch(e) {
		//}
		if (menu.debug) {
			//menu.trace('<HR/>');  
		}
	}

/* BEGIN SECTION - DIV SWITCHING */

	// Used from nav button rollover, where the menu object name is known. It would toggle only the current menu open/closed.
	menu.toggle = function(obj) {
		if (menu.debug) {
			menu.lastFunctionName = 'menu.toggle';
			//menu.trace(menu.lastFunctionName);
		}
		menu.resize();
		var menuID = menu.getID(obj);
		if (menu.visibility == 1) {
			/* SET TIMER TO CLOSE. IF MENU OPTIONS ARE MOUSED OVER, CLEAR TIMER. */
			//menu.trace('setting close timer');
			//menu.trace('timerID: ' + menuID);
			if (!menu.timeout[menuID]) {
				//menu.trace('<b><font color="navy">close timer status: OK</font></b>');
				menu.setTimeoutValue(menuID);
				//menu.trace('timeout: ' + menu.timeout[menuID]);
			} else if (menu.timeout[menuID]) { // Has to be a number.
				//menu.trace('<b><font color="navy">close timer status: ALREADY SET</font></b>');
				//menu.trace('timeout: ' + menu.timeout[menuID]);
			} else {
				//menu.trace('<b><font color="navy">close timer status: FAIL</font></b>');
				//menu.trace('timeout: ' + menu.timeout[menuID]);
			}
		} else {
			//menu.trace('opening menus');
			/* OPEN MENU + SHOW IFRAME */
			//menu.switchStyleDisplay(menuID,'inline');
			//menu.trace('menuID: ' + menuID);
			iframe.toggleVisibility('menu',menuID,'visible');
			if (menu.isBadIE) {
				iframe.toggleVisibility('iframe','MSIEIFrame','visible');
			}
		}
		if (menu.debug) {
			//menu.trace('<HR/>');
		}
	}

	// Used from body.onClick, where the menu object names are not known. That would close all open menus.
	// Also called from the timeout, when the menu is moused out.
	menu.closeMenus = function() {
		if (menu.debug) {
			menu.lastFunctionName = 'menu.closeMenus';
			//menu.trace('<font color="white">' + menu.lastFunctionName + '</font>');
			//menu.trace('closing menus');
		}
		var menuID = menu.getID('ProgramsMenu');
		//menu.trace('<font color="red">1: ' + menuID);
		//menu.trace('2: ' + menu.timeout[menuID]);
		//menu.trace('3: ' + typeof(menu.timeout[menuID]));
		//menu.trace('4: ' + menu.visibility);
		//menu.trace('5: ' + iframe.visibility + '</font>');
		//if ((menu.timeout[menuID]) && (typeof(menu.timeout[menuID]) != 'undefined')) {
		if (menu.visibility == 1) {
			/* CLOSE MENU + HIDE IFRAME */
			var obj = document.getElementById(menuID);
			//menu.toggle(obj);
			iframe.toggleVisibility('menu','ProgramsMenu','hidden');
			if (menu.isBadIE) {
				iframe.toggleVisibility('iframe','MSIEIFrame','hidden');
			}
			menu.clearTimer(obj);
		}
		/*if ((obj.style) && (obj.style.display != 'none')) {
			menu.switchStyleDisplay('ProgramsMenu','none');
		}*/
		if (menu.debug) {
			//menu.trace('<HR/>');
		}
	}

	menu.clearTimer = function(obj) {
		if (menu.debug) {
			menu.lastFunctionName = '<font color="green">menu.clearTimer</font>';
			//menu.trace(menu.lastFunctionName);
		}
		var menuID = menu.getID(obj);
		//menu.trace('<font color="purple"><b>timerID: ' + menuID + '</b></font>');
		//menu.trace('<font color="purple"><b>timer: ' + menu.timeout[menuID] + '</b></font>');
		clearTimeout(menu.timeout[menuID]);
		if (menu.debug) menu.trace(menu.lastFunctionName);
		/* These won't work here:
			menu.timeout[obj.id] = null;
			menu.timeout[obj.id] = undefined;
		*/
		menu.timeout[menuID] = undefined; // Has to be a number.
		//menu.trace('<strike>timer: ' + menu.timeout[menuID] + '</strike>');
		//menu.toggle(obj);
		if (menu.debug) {
			//menu.trace('<HR/>');
		}
	}

	/* menu.switchStyleDisplay = function(objID,styleInfo) {
		if (menu.debug) {
			menu.lastFunctionName = 'menu.switchStyleDisplay';
			//menu.trace(menu.lastFunctionName);
		}
		var obj = document.getElementById(objID);
		if (obj) {
			if (styleInfo == 'none') {
				menu.visibility = 0;
			} else {
				menu.visibility = 1;
			}
			obj.style.display = styleInfo;
		}
		if (menu.debug) {
			//menu.trace('<HR/>');
		}
	} */

	iframe.toggleVisibility = function(objKey,objID,visibility) {
		if (menu.debug) {
			menu.lastFunctionName = 'iframe.toggleVisibility';
		}
		var obj = document.getElementById(objID);
		if (obj) {
			if (visibility == 'visible') {
				if (objKey == 'menu') {
					menu.visibility = 1;
				} else if ((objKey == 'iframe') && (menu.isBadIE)) {
					iframe.visibility = 1;
				}
			} else {
				if (objKey == 'menu') {
					menu.visibility = 0;
				} else if ((objKey == 'iframe') && (menu.isBadIE)) {
					iframe.visibility = 0;
				}
			}
			//menu.trace(objKey + 'Visibility is: ' + visibility);
			obj.style.visibility = visibility;
		}
		if (menu.debug) {
			//menu.trace(menu.lastFunctionName);
			//menu.trace('<HR/>');
		}
	}