/*
Adopted and updated from Simple Image Trail script- By JavaScriptKit.com
Visit http://www.javascriptkit.com for this script and more
This notice must stay intact
*/

var offsetfrommouse=[5,20]; //image x,y offsets from cursor position in pixels. Enter 0,0 for no offset
var displayduration=0; //duration in seconds image should remain visible. 0 for always.
var currentimageheight = 270;	// maximum image size.
var trailDivId = "TrailImageDivId";
 
if (document.getElementById || document.all)
{
	document.write('<div id=\"TrailImageDivId\" style=\"position: absolute; top: 0px; left: 0px; visibility: hidden;\">');
	document.write('</div>');
}

function getTrailDivObject()
{
    return document.getElementById(trailDivId);
}

function truebody()
{   
    var oTrueBodyReference;
    oTrueBodyReference = document.body;
	return oTrueBodyReference;
}

function showtrail(imagename,title,description,showthumb)
{
	document.onmousemove=followmouse;
	newHTML = '<div style=\"position: relative; padding: 5px; background-color: #ffffff; border: 1px solid #000000; \">';

	if (showthumb > 0)
	{
		newHTML = newHTML + '<center><img src="' + imagename + '" border="0"></center>';
	}

	newHTML = newHTML + '</div>';
	getTrailDivObject().innerHTML = newHTML;
	getTrailDivObject().style.visibility="visible";
}


function hidetrail()
{
	getTrailDivObject().style.visibility="hidden";
	document.onmousemove="";
}

function followmouse(e)
{
	var xcoord=offsetfrommouse[0]
	var ycoord=offsetfrommouse[1]

	var docwidth = document.body.clientWidth;
	var docheight= document.body.clientHeight;

	if (typeof e != "undefined"){
		if (docwidth - e.pageX < 400)
		{
			xcoord = e.pageX - xcoord - 400; // Move to the left side of the cursor
		}
		else
		{
			xcoord = xcoord + e.pageX - 150;
		}
		
		if (docheight - e.pageY < (currentimageheight + 110))
		{
			ycoord += e.pageY - Math.max(0,(110 + currentimageheight + e.pageY - docheight - truebody().scrollTop));
		}
		else
		{
			ycoord += e.pageY;
		}

	}
	else if (typeof window.event != "undefined")
	{
		if (docwidth - event.clientX < 400){
			xcoord = event.clientX + truebody().scrollLeft - xcoord - 400; // Move to the left side of the cursor
		} else {
			xcoord = xcoord + truebody().scrollLeft+event.clientX - 150;			
		}
		if (docheight - event.clientY < (currentimageheight + 110)){
			ycoord += event.clientY + truebody().scrollTop - Math.max(0,(110 + currentimageheight + event.clientY - docheight));
		} else {
			ycoord += truebody().scrollTop + event.clientY;
		}
	}
    if(ycoord < 0) { ycoord = ycoord*-1; }
	
	var oTrailDiv = getTrailDivObject();
	if (oTrailDiv != null)
	{
		//alert("x = " + xcoord + ", y = " + ycoord);
		oTrailDiv.style.left=xcoord;
		oTrailDiv.style.top=ycoord;
		
		// Must set zIndex here, after x and y coords have
		// been reset, and not before, such as during <div> setup.
        oTrailDiv.style.zIndex = 5;		
	}

}

