// JavaScript Document
<!-- Due to different browser naming of certain key global variables, we need to do three different tests to determine their values -->

// Determine how much the visitor had scrolled

var mouseX = 0, mouseY = 0, scrolledX = 0, scrolledY = 0;
var docW = 1024, docH = 768, bodyW = 1024;
var currentTip = "";
var fading = false;

var navOutId = "";
var navInId = "";
var navMode = "";

var nIn = 0;
var nOut = 0;

var ajax;
var ajaxName; 
var ajaxHeaderName = "";
var ajaxGrab = false; ajaxGrabX = 0; ajaxGrabY = 0;
var ajaxAutoClose = true;
var HTMLName;
var isContactForm = false;

var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;


function initTip() {
	
	document.onmousedown = ajaxMouseDown;
	document.onmouseup = ajaxMouseUp;
	document.onmousemove = getMouseXY; // calls getMouseXY whenever mouse is moved
	getDocSize();
	window.onload = checkMsg;
}

// get scrollX and scrollY for all browsers
function getScroll() {
	
	if (self.pageYOffset) {
		scrolledX = self.pageXOffset;
		scrolledY = self.pageYOffset; 
		} 
	else if (document.documentElement && document.documentElement.scrollTop) {
		scrolledX = document.documentElement.scrollLeft;
		scrolledY = document.documentElement.scrollTop; 
		} 
	else if(document.body) {
		scrolledX = document.body.scrollLeft;
		scrolledY = document.body.scrollTop;
	}
}


function getDocSize() {

	if (self.innerHeight) {
		docW = self.innerWidth;
		docH = self.innerHeight;
		} 
	else if (document.documentElement && document.documentElement.clientHeight) {
		docW = document.documentElement.clientWidth;
		docH = document.documentElement.clientHeight;
		} 
	else if (document.body) {
		docW = document.body.clientWidth;
		docH = document.body.clientHeight;
		}
	bodyW = document.getElementsByTagName('body')[0].clientWidth - 15;
}


function getMouseXY(e) // works on IE6,FF,Moz,Opera7
{ 
	if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)

	if (e) {
		if (e.pageX || e.pageY) { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
			mouseX = e.pageX;
			mouseY = e.pageY; }
		else if (e.clientX || e.clientY) { // works on IE6,FF,Moz,Opera7
			mouseX = e.clientX + scrolledX;
		  	mouseY = e.clientY + scrolledY;
    	}  
		// update any tips
		if (currentTip != "") {
			document.getElementById(currentTip).style.left = (mouseX + 1) + "px"; // must add 'px' to make it work with ff
			document.getElementById(currentTip).style.top = (mouseY + 1) + "px"; }
	
		if (ajaxGrab) ajaxMouseMove();
		if (ajaxName != "") ajaxCheckCursor();
	}
	
}

// works with all browsers n = 0..100%
function setOpacity(name, n) {

	document.getElementById(name).style.opacity = n / 100;
	document.getElementById(name).style.filter = 'alpha(opacity=' + n + ')'; // ie
}


function fadeFalse() { fading = false; }


function fadeInTip(name) {

	fading = true;
	for (var i = 0; i <= 50; i++)
		setTimeout('setOpacity("' + name + '", ' + i*2 + ');', 6 * i);
	setTimeout('fadeFalse();', 310);
}


function hideTip(name) {

	// make sure we don't fade out in the middle of fading in
	if (!fading) {
		fading = true;
		for (var i = 50; i >= 0; i--)
			setTimeout('setOpacity("' + name + '", ' + i*2 + ');', 300 - 6 * i); 
		setTimeout('closeTip("' + name+ '")', 310); 
		setTimeout('fadeFalse();', 310); }
	else closeTip(name);
}


function showTip(name) {
	
	currentTip = name;
	getScroll();
	setOpacity(name, 0);
	document.getElementById(name).style.display = "block";
	document.getElementById(name).style.left = (mouseX + 1) + "px"; // must add 'px' to make it work with ff
	document.getElementById(name).style.top = (mouseY + 1) + "px";
	fadeInTip(name);
}


function closeTip(name) {

	document.getElementById(name).style.display = "none";
	currentTip = "";
}


function centreTip(name) {

	var win = document.getElementById(name);

	getScroll();
	getDocSize();
	setOpacity(name, 0);
	win.style.display = "block";

	var leftOffset = scrolledX + (docW - win.offsetWidth) / 2;
	var topOffset = scrolledY + (docH - win.offsetHeight) / 2;

	if (leftOffset < scrolledX) leftOffset = scrolledX;
	if (topOffset < scrolledY) topOffset = scrolledY;

	win.style.top = topOffset + "px";
	win.style.left = leftOffset + "px";
	fadeInTip(name);
}


function maxHeight(name) {

	getDocSize();
	document.getElementById(name).style.height = (docH - 150) + "px";
}


function initAjax() {

	try {
	  	// Firefox, Opera 8.0+, Safari
		ajax=new XMLHttpRequest();
		}
	catch (e) {
		// Internet Explorer
  		try {
    		ajax=new ActiveXObject("Msxml2.XMLHTTP");
			}
  		catch (e) {
    		try {
      			ajax=new ActiveXObject("Microsoft.XMLHTTP"); 
				}
    		catch (e) {
      			alert("Your browser does not support AJAX!");
      			return false;
			}
    	}
	}
}


// source = sourcefile, header = window, dest = destination document (within window), 
// maxH = maximize the height (boolean)
// autoClose = close automatically if you click outside the window
function loadAjax(source, header, dest, maxH, autoClose) {
	

	var winH = document.getElementById(header);
	var winD = document.getElementById(dest);	

	// set referring url if it is the contact form
	if (source == "/Contact.html") { isContactForm = true; }

	initAjax();
	if (maxH) { maxHeight(header); }
	ajaxName = dest;
	ajaxHeaderName = header;
	ajax.onreadystatechange = doneAjax; // this won't work in IE if you put it after the "send" function
	ajax.open("GET", source, true);
	ajax.send(null);
	centreTip(header);
	
	winD.style.height = document.getElementById(header).offsetHeight - 37 + "px";
	winD.style.left = "0px";
	winD.style.top = "30px";
	if (!IE6) {
		winH.style.position = "fixed";
		winH.style.top = (docH - winH.offsetHeight) / 2 + "px";
		winH.style.left = (docW - winH.offsetWidth) / 2 + "px";
		if (winH.offsetTop < 0) winH.style.top = "0px";
	}
	
	document.getElementById("lightbox").style.height = document.body.offsetHeight + "px";
	document.getElementById("lightbox").style.display = "block";
	
	ajaxAutoClose = autoClose;	
}

// loads ajax directly into website (very simple, no windows)
function loadHTML(source, dest)

{
	initAjax();
	HTMLName = dest;
	ajax.onreadystatechange = doneHTML;
	ajax.open("GET", source, true);
	ajax.send(null);
}


function doneAjax() {

	if (ajax.readyState==4) {
		document.getElementById(ajaxName).innerHTML = ajax.responseText;
		if (isContactForm) {
			document.getElementById('myurl').value = location.href.replace(/\?.*/,'');
			isContactForm = false; }
	}
}

function doneHTML()

{
	if (ajax.readyState==4) {
		document.getElementById(HTMLName).innerHTML = ajax.responseText;
	}
}


function hideAjax() {
	
	if (ajaxHeaderName != "") {
		document.getElementById("lightbox").style.display = "none";
		hideTip(ajaxHeaderName);
		ajaxHeaderName = ""; 
		ajaxGrab = false;
		document.body.style.cursor = "default";
		}
}

// handles mouse down routines such as
// moving the ajax window or fading it out
// if you click outside the window
function ajaxMouseDown(e) {

	var w, x, y, mx, my;

	if (!fading && ajaxHeaderName != "") { 

		w = document.getElementById(ajaxHeaderName);
		x = w.offsetLeft; y = w.offsetTop;
		mx = mouseX; my = mouseY;
		getDocSize();

		if (w.style.position == "fixed") {
			getScroll();
			mx -= scrolledX;
			my -= scrolledY; }

		if (ajaxAutoClose && mx < bodyW && 
			(mx < x ||
		    my < y || 
			mx > x + w.offsetWidth || 
			my > y + w.offsetHeight)) {
					hideAjax(); } 
		}
		if (!IE6 && !ajaxGrab && 
			mx >= x && mx <= x + w.offsetWidth &&
			my >= y && my <= y + 30) {
				ajaxGrabX = mx;
				ajaxGrabY = my;
				ajaxGrab = true;
				getDocSize();
			}
}

// stops moving the ajax window
function ajaxMouseUp(e) {

	ajaxGrab = false;
}

// moves the ajaxWindow
function ajaxMouseMove() {

	var w = document.getElementById(ajaxHeaderName);
	var mx = mouseX;	
	var my = mouseY;
	var newx, newy;
	
	if (w.style.position == "fixed") {
		getScroll();
		mx -= scrolledX;
		my -= scrolledY; }
		
	newx = w.offsetLeft + (mx - ajaxGrabX);
	newy = w.offsetTop + (my - ajaxGrabY);
	if (newx + w.offsetWidth > docW) newx = docW - w.offsetWidth;
	if (newy + w.offsetHeight > docH && newy > 0) {
		newy = docH - w.offsetHeight;
		if (newy < 0) newy = 0; }
	if (newx < 0) newx = 0;
	if (newy < -20) newy = -20;
	
	w.style.left = newx + "px";
	w.style.top = newy + "px";
	
	ajaxGrabX = mx;
	ajaxGrabY = my;
}

// changes the mouse cursor if it is over a window
function ajaxCheckCursor() {

	var w, x, y, mx, my;

	if (!fading && ajaxHeaderName != "") { 

		w = document.getElementById(ajaxHeaderName);
		x = w.offsetLeft; y = w.offsetTop;
		mx = mouseX; my = mouseY;
		getDocSize();

		if (w.style.position == "fixed") {
			getScroll();
			mx -= scrolledX;
			my -= scrolledY; }

		if (!IE6 && !ajaxGrab) {
			if (mx >= x && mx <= x + w.offsetWidth &&
				my >= y && my <= y + 30) {
					document.body.style.cursor = "move"; }
			else {
				document.body.style.cursor = "default";
			} 
		}
	}
	
}

//displays a message into element "alertAjax" which is in the "alertHeader" window
function myAlert(msg) {
	
	var winH = document.getElementById("alertHeader");
	var winD = document.getElementById("alertAjax");	

	initAjax();
	ajaxName = "alertAjax";
	ajaxHeaderName = "alertHeader";
	centreTip("alertHeader");
	
	winD.style.height = document.getElementById("alertHeader").offsetHeight - 37 + "px";
	winD.style.left = "0px";
	winD.style.top = "30px";
	if (!IE6) {
		winH.style.position = "fixed";
		winH.style.top = (docH - winH.offsetHeight) / 2 + "px";
		winH.style.left = (docW - winH.offsetWidth) / 2 + "px";
		if (winH.offsetTop < 0) winH.style.top = "0px";
	}
	
	document.getElementById("lightbox").style.height = document.body.offsetHeight + "px";
	document.getElementById("lightbox").style.display = "block";

	document.getElementById("alertAjax").innerHTML = 
		'<table width="100%" height="100%" cellpadding="0" cellpadding="0">' + 		
		'<tr><td class="three" cellpadding="0" cellspacing="0"><br><br><center>' +
	 	msg + '<br><br><br>' +
		'<input type="button" value="OK" onclick="hideAjax()" /><br></center></td></tr></table>';
	
	ajaxAutoClose = false;
}

//upon loading the website, checks to see if it should display a message
function checkMsg() {
	
	var s, left, right;

	if (document.location.search) {
		s = document.location.search;
		s = s.replace(/%20/g, " ");
		s = s.replace(/%2C/g, ",");
		left = s.indexOf("=") + 1;
		right = s.length;
		myAlert(s.substr(left, right)); }
}


//sets display to none when given id, if no other nav is selected
function hideNav(id) {
	
	navOutId = id;
	navMode = "out";
	setTimeout('checkHideId();', 310);
}


// hides the nav bar if none is selected after 1/3 of a second
function checkHideId() {

	if (navMode == "out") {		
		document.getElementById(navOutId).style.display = "none";
		navInId = navOutId = "";
	}
}


//sets display to absolute when given id
function showNav(id) {

	var n = document.getElementById(id).style;

	if (navInId == id) { navMode = "in"; return; }

	setOpacity(id, 0);
	n.position = "absolute";
	n.display = "block";
	n.backgroundColor = "#000";
	n.padding = "5px";
	
	for (var i = 0; i <= 37; i++)
		setTimeout('setOpacity("' + id + '", ' + i*2 + ');', 8 * i);

	navInId = id;
	navMode = "in";
	if (navInId != navOutId) {
		document.getElementById(navOutId).style.display = "none";
	}
}


initTip();
