// JavaScript Document

var oHoverDiv;
var oHoverImg;
var mouseOffsetX=15;
var mouseOffsetY=25;
var defaultimageheight = 40;	// maximum image size.
var defaultimagewidth = 40;	// maximum image size.
mouseLocation = new Point(-500,-500);

function Point(x,y) {  this.x = x; this.y = y; }


/* 
Example:
function test()
{
  if (document.layers) getMouseLoc;     //NS
  else if (document.all) getMouseLoc(); //IE
  alert(mouseLocation.x+","+mouseLocation.y);
}
in the BODY:
<a href="#" onmouseover="test()">test</a>
*/

var hoverTimer;
function checkDiv() {
	if(!oHoverDiv) {
		oHoverDiv=document.createElement("div");
		oHoverDiv.setAttribute("id","HoverDiv");
		oHoverDiv.style.display="none";
		oHoverDiv.style.position="absolute";
		oHoverDiv.style.width="auto";
		oHoverDiv.style.height="auto";
		oHoverDiv.style.zIndex=100;
		oHoverDiv.style.backgroundColor="#E0E0E0";
		
		oHoverImg=document.createElement("img");
		oHoverImg.style.width="auto";
		oHoverImg.style.border="solid 2px black";
		oHoverDiv.appendChild(oHoverImg);
		var bodyRef = document.getElementsByTagName("body").item(0);
		bodyRef.appendChild(oHoverDiv);
	}
}
function showHover() {
	oHoverDiv.style.display="block";
	positionDiv();
	
	if(hoverTimer) {
		window.clearTimeout(hoverTimer);
		hoverTimer=null;
	}
	
}
function registerHoverElement(oElementID,sImage) {
	var oElement=document.getElementById(oElementID);
	if(!oElement) return null;
	var oImagePreload=new Image();
	oElement.onmouseover=function(e) {
		if(!e) e=window.event;
		if(!document.all)  //NS
		{
			mouseLocation.x = e.pageX;
			mouseLocation.y = e.pageY;
		}
		else               //IE
		{
			mouseLocation.x = event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			mouseLocation.y = event.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}
		checkDiv();
		oHoverImg.src=sImage;
		oHoverDiv.style.top=(mouseLocation.y + 400) + "px";
		if(mouseLocation.x>600) oHoverDiv.style.left=mouseLocation.x-560
		else oHoverDiv.style.left=(mouseLocation.x+60) + "px";
		hoverTimer=window.setTimeout(showHover,500);
	}
	oElement.onmouseout=function(e) {
		if(hoverTimer) {
			window.clearTimeout(hoverTimer);
			hoverTimer=null;
		}
		if(oHoverDiv) oHoverDiv.style.display="none";
		if(oHoverImg) oHoverImg.src="/images/spacer.gif";
	}
	oElement.onmousemove=function(e) {
		checkDiv();
		if(!e) e=window.event;
		if(!document.all)  //NS
		{
			mouseLocation.x = e.pageX;
			mouseLocation.y = e.pageY;
		}
		else               //IE
		{
			mouseLocation.x = event.x + document.body.scrollLeft + document.documentElement.scrollLeft;
			mouseLocation.y = event.y + document.body.scrollTop + document.documentElement.scrollTop;
		}
		if(oHoverDiv.style.display!="none") positionDiv();
	}
	oImagePreload.src=sImage;
}

function positionDiv() {
	var pageX=mouseLocation.x;
	var pageY=mouseLocation.y;
	var x=parseInt(mouseOffsetX);
	var y=parseInt(mouseOffsetY);
	
	var scrollTop=truebody().scrollTop;
	var scrollLeft=truebody().scrollLeft;
	
	var imageHeight=oHoverImg.offsetHeight;
	if(imageHeight<1) imageHeight=100;
	var imageWidth=oHoverImg.offsetWidth;
	if(imageWidth<1) imageWidth=100;
	
	var docwidth=document.all? scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15;
	var docheight=document.all? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight);
	
	if (docwidth - pageX < imageWidth + 2*mouseOffsetX) {
		x = pageX - x - imageWidth; // Move to the left side of the cursor
	} 
	else {
		x += pageX;
	}
	y=pageY;
	if(y+imageHeight>docheight + scrollTop) y=docheight + scrollTop-imageHeight;
	//alert("Y: " + y + " scrollTop: " + scrollTop + " docel: " + document.documentElement.scrollTop + " scrollY " + window.scrollY);
	if(window.scrollY) scrollTop=Math.max(scrollTop,window.scrollY);
	if(window.scrollX) scrollLeft=Math.max(scrollLeft,window.scrollX);
	x=Math.max(scrollLeft,x);
	y=Math.max(scrollTop,y);
	oHoverDiv.style.left=x+"px";
	oHoverDiv.style.top=y+"px";
}

function truebody(){
return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}
