// Hover div related code...

var canGetComputed = false;		// Whether or not we can use DOM style getComputedStyle...
var mouseOffset = [16,16];		// x,y offsets from cursor position in pixels. Enter 0,0 for no offset
var mouseX = 0;
var mouseY = 0;
var followMouse = true;			// Set this to false and don't call hideHover to not follow...
var docWidth = 0;
var docHeight = 0;
var divWidth = 0;
var divHeight = 0;
var hoverDelayRef = null;
var hoverDelay = 250;			// Quarter second delay...
var canShow = false;

if (window.getComputedStyle) canGetComputed = true;
if (document.getElementById || document.all) document.write('<div id="hoverdiv" style="visibility:hidden;"></div>');

// Used to fetch the hover div's content via Ajax...
function initHover(filename) {
	debug('initHover \''+filename+'\' called...');
	sendRequest('initHover', filename);						// Fire Ajax request... Could use '/cms/.../'+encodeURIComponent(filename);
	canShow = true;
}

function prepHover() {

	debug('prepHover called...');
	
	ourDiv = document.getElementById('hovercontent');

	if(canGetComputed) {
		// Grab the computed width/height and strip out the 'px'...
		tempWidth = new String(document.defaultView.getComputedStyle(ourDiv, null).getPropertyValue('width'));
		tempHeight = new String(document.defaultView.getComputedStyle(ourDiv, null).getPropertyValue('height'));
		widthChunks = tempWidth.split('px');
		heightChunks = tempHeight.split('px');
		divWidth = Number(widthChunks[0]) + 18;
		divHeight = Number(heightChunks[0]) + 18;
	}
	else {
		divWidth = ourDiv.offsetWidth;
		divHeight = ourDiv.offsetHeight;
	}

	if(canShow) { hoverDelayRef = setInterval('showHover()', hoverDelay); }
	document.onmousemove=followmouse;
}


function showHover() { 

	debug('showHover called...');

	if(mouseX != 0 && mouseY != 0) {
		canShow = true;
		document.getElementById('hoverdiv').style.visibility = "visible";
		clearInterval(hoverDelayRef);
	}
}


function hideHover() {

	debug('hideHover called...');

	canShow = false;
	document.onmousemove = "";
	if(hoverDelayRef) clearInterval(hoverDelayRef);
	if(document.getElementById('hoverdiv')) {
		ourDiv = document.getElementById('hoverdiv');
		ourDiv.style.width="0px";
		ourDiv.style.height="0px";
		ourDiv.style.visibility="hidden";
		ourDiv.style.left="-1000px";
		ourDiv.style.top="-1000px";
	}
}


function followmouse(e) {
	
	getMouseLocation(e);
	
	var hoverY = new Number;
	var hoverX = new Number;
	
	ourDiv = document.getElementById('hoverdiv');
	
	// Do we need to place the hover div to the left of the mouse pointer...
	if(docWidth <= (mouseX + mouseOffset[0] + divWidth)) hoverX = (mouseX - (divWidth + mouseOffset[0]));
	else hoverX = mouseX + mouseOffset[0];
	
	// Do we need to place the hover div above the mouse pointer...	
	if(docHeight <= (mouseY + mouseOffset[1] + divHeight)) hoverY = (mouseY - (divHeight + mouseOffset[1]));
	else hoverY = mouseY + mouseOffset[1]
	
	ourDiv.style.left = hoverX + "px";
	ourDiv.style.top = hoverY + "px";

	//window.status = 'docWidth: '+docWidth+', docHeight: '+docHeight+', mouseX: '+mouseX+', mouseY: '+mouseY+', canGetComputed: '+canGetComputed+', divWidth: '+divWidth+', divHeight: '+divHeight+'...';
	//window.status = 'docWidth: '+docWidth+' ('+mouseX + mouseOffset[0] + divWidth+'), docHeight: '+docHeight+' ('+mouseY + mouseOffset[1] + divHeight+')...';

	if(!followMouse) document.onmousemove="";
}

function getMouseX(ourEvent) {
	if (!ourEvent) ourEvent = window.event;
		if (ourEvent.pageX) return ourEvent.pageX;
		else if (ourEvent.clientX )return ourEvent.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft);
	else return 0;
}

function getMouseY(ourEvent) {
	if (!ourEvent) ourEvent = window.event;
		if (ourEvent.pageY) return ourEvent.pageY;
		else if (ourEvent.clientY) return ourEvent.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	else return 0;
}


function getMouseLocation(ourEvent) {

	// Update the doc width and height...
	docWidth = document.all ? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth;
	docHeight = document.all ? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(document.body.offsetHeight, window.innerHeight);

	mouseX = getMouseX(ourEvent);
	mouseY = getMouseY(ourEvent);
}

function truebody() {
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat") ? document.documentElement : document.body
}
