var offsetfrommouse = [15,20]; //x,y offsets from cursor
var displayduration = 0; //seconds info should remain visible. 0 for always.

// Create DIV for our info
if (document.getElementById || document.all) {
	document.write('<div id="trailBox"></div>');
}

// Shortcut to get the DIV object
function getTrailObj(){
	if (document.getElementById)
		return document.getElementById("trailBox");
	else if (document.all)
		return document.all.trailBox;
}

//Gets the "body" element depending on browser
function truebody(){
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}


// Add info to DIV and make visible
function showTrail(image,text) {
	if (image != '' && text != '') //image and text
		text = '<img src="' + image + '"/><br/>' + text;
	else if (image != '' && text == '') // just image
		text = '<img src="' + image + '"/>';


	getTrailObj().innerHTML = text;
	getTrailObj().style.visibility = "visible";
}

// Hide the DIV
function hideTrail(){
	getTrailObj().style.visibility = "hidden";
	//document.onmousemove = "";
}

//Get the viewable browser width
function getWinWidth () {
	var width = 0;
	if (window.innerWidth)
		width = window.innerWidth - offsetfrommouse[1];
	else if (document.documentElement && document.documentElement.clientWidth)
		width = document.documentElement.clientWidth;
	else if (document.body && document.body.clientWidth)
		width = document.body.clientWidth;
	return width;
}

//Get the viewable browser height
function getWinHeight () {
	var height = 0;
	if (window.innerHeight)
		height = window.innerHeight - offsetfrommouse[1];
	else if (document.documentElement && document.documentElement.clientHeight)
		height = document.documentElement.clientHeight;
	else if (document.body && document.body.clientHeight)
		height = document.body.clientHeight;
	return height;
}

//Get the scrollbar X position
function getScrollX () {
	var scrollX = 0;
	if (typeof window.pageXOffset == "number")
		scrollX = window.pageXOffset;
	else if (document.documentElement && document.documentElement.scrollLeft)
		scrollX = document.documentElement.scrollLeft;
	else if (document.body && document.body.scrollLeft)
		scrollX = document.body.scrollLeft;
	else if (window.scrollX)
		scrollX = window.scrollX;

	return scrollX;
}

//Get the scrollbar Y position
function getScrollY () {
	var y = 0;
	if (typeof window.pageYOffset == "number")
		y = window.pageYOffset;
	else if (document.documentElement && document.documentElement.scrollTop)
		y = document.documentElement.scrollTop;
	else if (document.body && document.body.scrollTop)
		y = document.body.scrollTop;
	else if (window.scrollY)
		y = window.scrollY;
	return y;
}

//Determine where our DIV should be as the mouse moves
function followMouse(e) {
	var xcoord = 0;
	var ycoord = 0;
	if (typeof e != "undefined"){
		xcoord += e.pageX;
		ycoord += e.pageY;
	}
	else if (typeof window.event != "undefined"){
		xcoord += truebody().scrollLeft + event.clientX;
		ycoord += truebody().scrollTop + event.clientY;
	}

	//Set X Coordinate for Info Box (keep from going off screen)
	var x = xcoord; // + getScrollX();
	if(x + getTrailObj().offsetWidth + offsetfrommouse[0] > getWinWidth() + getScrollX()){
		x = x - getTrailObj().offsetWidth - offsetfrommouse[0];
		if(x < 0)
			x = 0;
	}
	else x = x + offsetfrommouse[0];

	//Set Y Coordinate for Info Box (keep from going off screen)
	var y = ycoord; //+ getScrollY();
	if(y + getTrailObj().offsetHeight + offsetfrommouse[1] > getWinHeight() + getScrollY()){
		y = y - getTrailObj().offsetHeight - offsetfrommouse[1];
		if( y < getScrollY()) {
			y = getWinHeight() + getScrollY() - getTrailObj().offsetHeight;
		}
	}
	else y = y + offsetfrommouse[1];

	getTrailObj().style.left = x + "px";
	getTrailObj().style.top = y + "px";
	//getTrailObj().innerHTML = 'offset: ' + getTrailObj().offsetHeight + '<br/>winheight: ' + getWinHeight() + '<br/>scrollY: ' + getScrollY() + '<br/>y: ' + y + '<br/>ycoord: ' + ycoord;
}

// Image preloader
var imageHandler = {
	path:"", // path to images
	imgs:[], preload:function() {
		for(var i=0;arguments[i];i++) {
			var img=new Image();
			img.src=this.path+arguments[i];
			this.imgs[this.imgs.length]=img;
		}
	}
}
imageHandler.preload("x-bg.gif");

//keep info box on at all times to ensure it's always positioned correctly
document.onmousemove = followMouse;

if (displayduration > 0)
	setTimeout("hideTrail()", displayduration * 1000);
