function toggleMenu(which, on) {
	var curMenu = document.getElementById(which+'Menu');
	
	if(curMenu) {
		if(on) {
			turnMenusOff(which);
			if(curMenu.style.display != 'block') {
				curMenu.style.display = 'block';
			}
		} else {
			if(curMenu.style.display != 'none') {
				curMenu.style.display = 'none';
			}
		}
	}
}

function toggleSubmenu(which, on) {	
	if(on) {
		if(document.lastSubmenu && document.lastSubmenu != which) {
			toggleSubmenu(document.lastSubmenu, false);
		}
		
		document.lastSubmenu = which;
		if(which) {
			var curMenu = document.getElementById(which+'Menu');
			if(curMenu && curMenu.style.display != 'block') {
				curMenu.style.display = 'block';
			}
		}
	} else {
		if(which) {
			if(document.lastSubmenu == which) {
				document.lastSubmenu = null;
			}
			
			var curMenu = document.getElementById(which+'Menu');
			
			if(curMenu && curMenu.style.display != 'none') {
				curMenu.style.display = 'none';
			}
		}
	}
}

function turnMenusOff(skipMenu) {
	var menuPoints = ['passengerTerminalGuide', 'visitorInformation', 'generalAviationAirCargo', 'about'];
	
	for(var i = 0; i < menuPoints.length; i++) {
		if(skipMenu != menuPoints[i]) {
			toggleMenu(menuPoints[i], false);
		}
	}
}

var flightReq;
function updateFlightInfo() {
	flightReq = window.ActiveXObject ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
	
	flightReq.onreadystatechange = processUpdateFlightInfo;
	flightReq.open('GET', 'view_controller.php?what=getUpdateFlightInfo&ajax=yes', true);
	flightReq.send(null);
}

function processUpdateFlightInfo() {
	if(flightReq.readyState == 4 && flightReq.status == 200) {
		eval(flightReq.responseText);
	}
}

function toggleJTechComments(event) {

	var jtechComments = document.getElementById('jtechCommentsContainer');
	if (jtechComments) {
		// get the current display style
		var currentDisplayStyle = jtechComments.style.display;

		// if we're about to show it - make sure it's positioned appropriately
		if (currentDisplayStyle == 'none') {

			// get the current mouse position during this click
			var mouseCoords = getMouseCoordinates(event);
			// and now get some information about the screen
			var windowDetails = getWindowDetails();

			// sort of show the comment slip, so we can get a reading on the height
			jtechComments.style.visibility = 'hidden';
			jtechComments.style.display = 'block';
			var commentHeight = jtechComments.offsetHeight;
			jtechComments.style.display = 'none';
			jtechComments.style.visibility = 'visible';

			// we only care about the height - see if the comments slip would fall below the currently viewable area
			if ((mouseCoords.y+commentHeight) > (windowDetails.viewHeight+windowDetails.scrollPositionX)) {
				jtechComments.style.top = -(commentHeight)+'px';
			}
			else {
				jtechComments.style.top = '17px';
			}
		}

		// toggle its display status from what it currently is		
		jtechComments.style.display = (currentDisplayStyle == 'none' ? 'block' : 'none');
	}

	// and return false, to prevent it from bubbling up to any link
	return false;
}

function getWindowDetails() {
	// the "viewable" height and width
	var viewWidth = window.innerWidth ? window.innerWidth : (document.documentElement ? document.documentElement.clientWidth : document.body.clientWidth);
	var viewHeight = window.innerHeight ? window.innerHeight : (document.documentElement ? document.documentElement.clientHeight : document.body.clientHeight);

	// where we're currently scrolled to
	var scrollPositionY = window.scrollY ? window.scrollY : document.documentElement.scrollTop;
	var scrollPositionX = window.scrollX ? window.scrollX : document.documentElement.scrollLeft;
	
	// get the total height and width of the content (which includes the area you have to scroll to)...
	var totalHeight = (document.documentElement.scrollHeight > document.body.scrollHeight) ? document.documentElement.scrollHeight : document.body.scrollHeight;	
	var totalWidth = (document.documentElement.scrollWidth > document.body.scrollWidth) ? document.documentElement.scrollWidth : document.body.scrollWidth;

	var centerY = scrollPositionY+(viewHeight/2);
	var centerX = scrollPositionX+(viewWidth/2);

	return {
		viewWidth: viewWidth,
		viewHeight: viewHeight,
		scrollPositionX: scrollPositionX,
		scrollPositionY: scrollPositionY,
		totalWidth: totalWidth,
		totalHeight: totalHeight,
		centerY: centerY,
		centerX: centerX,
		toString: function() {
			var output = "";
			output += 'viewWidth: '+viewWidth+"\n";
			output += 'viewHeight: '+viewHeight+"\n";
			output += 'scrollPositionX: '+scrollPositionX+"\n";
			output += 'scrollPositionY: '+scrollPositionY+"\n";
			output += 'totalWidth: '+totalWidth+"\n";
			output += 'totalHeight: '+totalHeight+"\n";
			output += 'centerY: '+centerY+"\n";
			output += 'centerX: '+centerX+"\n";		

			return output;
		}
	};
}

function getMouseCoordinates(e) {
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}

	// posx and posy contain the mouse position relative to the document
	return {x: posx, y:posy};
}


function showDestinationAirlines(city, destinationAirlines, xPos, yPos) {
	var destinationAirlinesDiv = document.getElementById('destinationAirlines');
	if (destinationAirlinesDiv) {
		var output = '<h3>'+city+'</h3>';
		output += '<div style="margin-bottom: 5px;">Non-stop destinations served by:</div>';
		output += '<ul style="padding-left: 10px;">';
		for (var i = 0; i < destinationAirlines.length; i++) {
			output += '<li style="padding-bottom: 0px;">'+destinationAirlines[i]+'</li>';
		}
		output += '</ul>';
		
		// set the content
		destinationAirlinesDiv.innerHTML = output;

		// position it appropriately
		destinationAirlinesDiv.style.left = xPos+"px";
		destinationAirlinesDiv.style.top = yPos+"px";
		// and show it
		destinationAirlinesDiv.style.display = "block";		
	}
}

function hideDestinationAirlines() {
	var destinationAirlinesDiv = document.getElementById('destinationAirlines');
	if (destinationAirlinesDiv) {
		// hide it
		destinationAirlinesDiv.style.display = "none";
		// and clear it out
		destinationAirlinesDiv.innerHTML = '';
		destinationAirlinesDiv.style.left = "0px";
		destinationAirlinesDiv.style.top = "0px";
	}
}