if (document.captureEvents) {
	document.captureEvents(Event.MOUSEMOVE);
	document.onmousemove = getCoords;
} else if (document.all) {
   document.onmousemove=getCoords;
}

var nMouseX = nMouseX = 0;
// var nMouseX = '';
// var nMouseY = '';
var movingDiv = false; // this will hold 
var yOffset = 0; // position the div position against the mouse position
var xOffset = 0;
var useGrid = false;
var gridX = 30;
var gridY = 30;

function getCoords(e) { // monitor mouse movements and record them... Please don't adjust this bit
	if (!e) e = window.event;
	if (document.all)
	{ // for IE
		// document.documentElement... is Standards Compliance mode for IE... clientY is document-relative so has to be offset with scrollTop and scrollLeft
		nMouseY = (e.clientY + (document.documentElement ? document.documentElement.scrollTop : document.body.scrollTop)); 
		nMouseX = (e.clientX + (document.documentElement ? document.documentElement.scrollLeft : document.body.scrollLeft));
	} else { // for Gecko-based browsers... no offset necessary as the coords are screen-relative (slightly odd)
		nMouseY = (e.pageY); 
		nMouseX = (e.pageX);
	}
	if (movingDiv !== false)	moveMe();
	return false;
}

function moveMe() { // attempt to move the parent of the div clicked (which is the title)
	var x = nMouseX;
	var y = nMouseY;
	var moveX = true; var moveY = true;
	
	y -= yOffset;
	x -= xOffset;
	
	if (moveX)	{ movingDiv.style.left = (useGrid ? (x - (x%gridX)) : x) + 'px'; window.status = 'X-Mod: ' + (x - (x%gridX)); }
	if (moveY)	movingDiv.style.top = (useGrid ? (y - (y%gridY)) : y) + 'px';
	return false;
}

function startMoving(theDiv)
{ // set up the global object as the parent of the clicked div
	movingDiv = theDiv;
	var x = nMouseX;
	var y = nMouseY;
	yOffset = y - movingDiv.offsetTop;
	xOffset = x - movingDiv.offsetLeft;
}

function stopMoving()
{ // set the global div object to false to stop the div from moving
	movingDiv = false;
	createCookie('x', nMouseX -= xOffset, '1');
	createCookie('y', nMouseY -= yOffset, '1');
}

function initClock() {
	// pointless function really to display real time
	var d = new Date();
	var h = d.getHours();
	var m = d.getMinutes();
	var s = d.getSeconds();

	document.getElementById('jsClock').innerHTML = (h < 10 ? '0' + h : h) + ':' + (m < 10 ? '0' + m : m) + ':' + (s < 10 ? '0' + s : s);
	setTimeout('initClock()', 1000);
}

function initHelp() {
	// find all images on the page and attach a help popup to any with a class of snipHelp applied to the outer A tag
	// actual function called by this initialiser is in objxhr.js
	var as = document.getElementsByTagName('A');
	var addDiv = false;

	for(var i=0; as[i]; i++) {
		if(as[i].className == 'snipHelp') {
			// var t = new Function ("xhr.showHelp('" + as[i].id + "', true);"); // var t = as[i].id; // scope resolution 
			addDiv = true;
			as[i].onmouseover = function () { xhr.showHelp(this.id, true); }
			as[i].onmouseout = function () { xhr.hideHelp(); }
		}
	}

	// create the help div (if there are any valid links)
	if (addDiv === true) {
		var mainDiv = document.getElementById('bodyHolder');
		var holder = document.createElement('DIV');
		holder.id = 'snipHelpHolder';
		var titleDiv = document.createElement('DIV');
		titleDiv.id = 'snipHelpTitle';
		titleDiv.appendChild(document.createTextNode('Please wait'));
		holder.appendChild(titleDiv);
		var contentDiv = document.createElement('DIV');
		contentDiv.id = 'snipHelpContent';
		holder.appendChild(contentDiv);
		mainDiv.appendChild(holder);
	}
}


function getObj(name)
{ // with much thanks and kudos to PPK for this code snippet... Find it at http://www.quirksmode.org/js/dhtmloptions.html
	if (document.getElementById) {
		this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
	} else if (document.all) {
		this.obj = document.all[name];
		this.style = document.all[name].style;
	} else if (document.layers) {
   		this.obj = document.layers[name];
   		this.style = document.layers[name];
	}
}


function coverPlate() { // coverPlate prototype pseudo-class definitions
	var additional;
	
	this.additional = new Array();
}

coverPlate.prototype.showCoverPlate = function (strTitle, strText, showClose) { // show the JS alert full screen cover thingy
	var coverDiv = document.createElement('div');
	var noteDiv = document.createElement('div');
	var titleDiv = document.createElement('div');
	
	coverDiv.id = 'coverPlate';
	coverDiv.className = 'coverPlate';
	coverDiv.style.opacity = '.65';
	coverDiv.style.filter = 'alpha(opacity=65)';
	// get a handle to the BODY of the document
	if (document.childNodes[0].childNodes[1]) { // disparity between browsers on DOM implementation... not IE's fault this time
		var objBody = document.childNodes[0].childNodes[1]; // This one is for Opera
	} else {
		var objBody = document.childNodes[1].childNodes[1]; // Every other browser
	}
	
	objBody.appendChild(coverDiv);
	noteDiv.id = 'coverNote';
	titleDiv.id = 'coverTitle';
	titleDiv.appendChild(document.createTextNode(strTitle));
	noteDiv.appendChild(titleDiv);
	var hSpan = document.createElement('span');
	hSpan.innerHTML = strText;
	noteDiv.appendChild(hSpan);
		
	if (showClose === true) { // this is an alert box so show the close option
		var closeText = document.createElement('div');
		var _this = this; // scope resolution
		closeText.id = 'coverCloseText';
		closeText.onclick = function () { _this.closeCoverPlate(closeText.parentNode) };
		closeText.appendChild(document.createTextNode('[ Close ]'));
		noteDiv.appendChild(closeText);
	}
	
	objBody.appendChild(noteDiv);
}

coverPlate.prototype.closeCoverPlate = function (objNode) { // remove all of the child nodes of the cover plate
	nodeRemover(objNode.previousSibling);
	nodeRemover(objNode);
}

function nodeRemover(objRefNode) { // recursively remove all of the child nodes from the referenced object parent node
	while (objRefNode.childNodes.length > 0) {
		if (objRefNode.childNodes[0].childNodes.length > 0) {
			nodeRemover(objRefNode.childNodes[0]);
		} else {
			objRefNode.removeChild(objRefNode.childNodes[0]);
		}
	}
	objRefNode.parentNode.removeChild(objRefNode);
}

coverPlate.prototype.getSize = function () {
	// get the height of the viewport and return it to the calling function
	// so the coverplate can be correctly sized
	// I can't remember where I got this code snippet so apologies for no link or credit... It has, though, been modified a bit
	var viewportwidth;
	var viewportheight;
	var docHeight;

	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerWidth;
		viewportheight = window.innerHeight;
	} else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
		// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
		viewportwidth = document.documentElement.clientWidth;
		viewportheight = document.documentElement.clientHeight;
	} else {
		// older versions of IE
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
		viewportheight = document.getElementsByTagName('body')[0].clientHeight;
	}

	docHeight = document.body.clientHeight;
	
	var size = new Array();
	size['height'] = (viewportheight > docHeight ? viewportheight : docHeight + 100);
	size['width'] = viewportwidth;
	return size;
}

var cp = new coverPlate; // instantiate the coverPlate class

/* Credit for the following code goes to PPK... The page can be found here http://www.quirksmode.org/js/cookies.html
I've used this because there's no point writing it again myself :) */
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
