function hover(who, baseClass, what) {
	var current;
	
	eval('current = who.className.match(/(Sel)?'+baseClass+'(Hover)?/);');
	if(current) {
		current = current[0];
		
		if(what && current && current.search(/Hover$/) == -1) {
			eval('who.className = who.className.replace(/'+current+'/, "'+current+'Hover");');
		} else if(!what && current && current.search(/Hover$/) != -1) {
			eval('who.className = who.className.replace(/'+current+'/, "'+current.replace(/Hover$/, '')+'");');
		}
	}
}

function select(who, baseClass, what) {
	var current;
	
	eval('current = who.className.match(/(Sel)?'+baseClass+'(Hover)?/);');
	if(current) {
		current = current[0];
		
		if(what && current && current.search(/^Sel/) == -1) {
			eval('who.className = who.className.replace(/'+current+'/, "Sel'+current+'");');
		} else if(!what && current && current.search(/^Sel/) != -1) {
			eval('who.className = who.className.replace(/'+current+'/, "'+current.replace(/^Sel/, '')+'");');
		}
	}
}

function parseStr(str) {
	var first = str.split('&');
	var data = new Array();
	
	for(var i = 0; i < first.length; i++) {
		var last = first[i].split('=');
		
		data[last[0]] = last[1];
	}
	
	return data;
}

function fixPNG(who) {
	var isIE = (navigator.appName == "Microsoft Internet Explorer" && navigator.userAgent.indexOf('Opera') == -1);
	if(isIE) {
		// filter for MSIE 6.x and 5.5
		var ie6xFilter = /^.*MSIE [6]\.[0-9].*$/;
		var ie55Filter = /^.*MSIE [5].*$/;
		
		if(ie6xFilter.test(navigator.userAgent) || ie55Filter.test(navigator.userAgent)) {
			who.onload = function(){}
			who.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src='+who.src+')';
			who.src = 'images/pixel.gif';
		}
	}
}

function preload(imagePathArray) {
	var imageArray = new Array();
	for (var i = 0; i < imagePathArray.length; i++) {
		var nextImage = new Image();
		nextImage.src = imagePathArray[i];
		imageArray.push(nextImage);
	}
	return imageArray;
}

function evalScripts(data) {
	var index = data.indexOf('<script');
	var endIndex;
	
	while(index != -1) {
		index = data.indexOf('>', index) + 1;
		endIndex = data.indexOf('</script>', index);
		
		eval(data.substring(index, endIndex));
		
		index = data.indexOf('<script', endIndex);
	}
}

function processLoadPage() {
	if(document.cancelSwap) {
		document.cancelSwap();
	}

	var detailSlip = document.getElementById('detailSlipContainer');
	if(detailSlip && detailSlip.style.display != 'none') {
		if(detailSlip.getAttribute('keepShown') == 'yes') {
			detailSlip.removeAttribute('keepShown');
		} else {
			hideSlip('detail');
		}
	}

	var el = document.getElementById('pageLoad');
	var title = httpReq.responseText.match(/!--title:(.+)-->/);

	if(title) {
		document.title = title[1];
	}
	if(httpReq.responseText.indexOf('--skipInnerHtml-->') == -1) {
		el.innerHTML = httpReq.responseText;
	}
	if(el.style.display != 'block') {
		el.style.display = 'block';
	}
	
	window.scrollTo(0,0);
	
	evalScripts(httpReq.responseText);
}

function processSlip(slip, skipShowSlip, slideTo) {
	if(!slip) {
		slip = 'detail';
	}

	if(httpReq.responseText.indexOf('--skipInnerHtml-->') == -1) {
		if(slideTo && httpReq.responseText.indexOf('--skipSlide-->') == -1) {
			slideSlip(slip, slideTo, httpReq.responseText);
		} else {
			var el = document.getElementById(slip+'SlipContent');
			
			el.innerHTML = httpReq.responseText;
		}
	}
	
	evalScripts(httpReq.responseText);
	
	if(!skipShowSlip) {
		showSlip(slip);
	}
}

function processEval() {
	eval(httpReq.responseText);
}

function scrollFix(el, event) {
	if(window.event) {
		el.scrollTop -= (window.event.wheelDelta / 120 * 60);
		return false;
	} else if(this.scrollTop == 0 && event.wheelDelta > 0) {
		return false;
	} else if((el.scrollHeight - this.scrollTop) == el.offsetHeight && event.wheelDelta < 0) {
		return false;
	}
}

function submitForm(theForm, queryString, proccessFunction) {
	var data = '';
	
	if(theForm) {
		theForm.jt_submitted.value = 'isValid';
		var formElements = theForm.elements;
	
		for(i = 0; i < formElements.length; i++) {
			if(formElements[i].disabled || ((formElements[i].type == 'radio' || formElements[i].type == 'checkbox') && !formElements[i].checked))
				continue;
				
			if(i)
				data += "&";
			
			if(formElements[i].options) {
				var valueAppend = '';
			
				for(k = 0; k < formElements[i].length; k++) {
					if(formElements[i].options[k].selected) {
						if(valueAppend != "") {
							valueAppend += ",";
						}
						
						valueAppend += formElements[i].options[k].value;
					}
				}
				
				data += escape(formElements[i].name) + '=' + escape(valueAppend);
			} else {
				data += escape(formElements[i].name) + '=' + escape(formElements[i].value);
			}
		}
	}
	
	sendRequest('view_controller.php', queryString, data, (proccessFunction ? proccessFunction : 'processSlip("detail", true);'));
}

function setLoadStatus(on) {
	var el = document.getElementById('loadStatus');
	
	if(el) {
		if(on) {
			var fixedNotSupported = el.currentStyle && el.currentStyle['position'] == 'absolute';

			var currentWidth = window.innerWidth ? window.innerWidth : (document.documentElement ? document.documentElement.clientWidth : document.body.clientWidth);
			var currentHeight = window.innerHeight ? window.innerHeight : (document.documentElement ? document.documentElement.clientHeight : document.body.clientHeight);

			var verticalPosition = fixedNotSupported ? (window.scrollY ? window.scrollY : document.documentElement.scrollTop) : 0;

			verticalPosition = (currentHeight/2)-150+verticalPosition;
			if(verticalPosition < 0) {
				verticalPosition = 0;
			}
			
			el.style.top = verticalPosition+'px';
			el.style.left = (currentWidth/2 - 37)+'px';
		
			el.style.display = 'block';
		} else {
			el.style.display = 'none';
		}
	}
}

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};
}

