/*
Simple Image Trail script- By JavaScriptKit.com
Visit http://www.javascriptkit.com for this script and more
This notice must stay intact
*/ 

var w=1
var h=1

// set initial state of the trailimageid div. As soon as onMouseover is registered on thumbnail, visibility changes to visible, 
// left and top change to an offset value from cursor positions, and width and height change according to 
// the width and height values of the full-size image.  ttimg is a placeholder for the fullsize image, and should be a 'loading' type gif.
if (document.getElementById || document.all)
document.write('<div id="trailimageid" style="position:absolute;visibility:hidden;top:0px;left:-1000px;border:1px solid #888888;background:#EEE;text-align:left;padding:0px;margin-right:20px;max-width:250px;z-index:2001"></div>')

// returns the styles of trailimageid
function gettrailobj()
{
	if (document.getElementById) return document.getElementById("trailimageid").style
	else if (document.all) return document.all.trailimagid.style
}

// some function that appears to deal only with opera compatibility
function truebody()
{
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

// 1. clears the onmousemove events
// 2. changes the path to the s.gif file
// 3. calls the gettrailobj function and resets the visibility and position of the trailimageid div.
function hidetrail()
{
	document.onmousemove=""
	document.getElementById('ttimg').src='s.gif'
	gettrailobj().visibility="hidden"
	gettrailobj().left=-1000+"px";
	gettrailobj().top=0+"px";
}

// 1. passes in the width, height, and name of the full-size image.
// 2. sets the src of the ttimg to the name of the preview file
// 3. calls the followmouse function everytime the mouse is moved.
// 4. changes the visibility, and the width and height  of the trailimageid div 
function showtrail(file,content,maxw,width)
{
	if(navigator.userAgent.toLowerCase().indexOf('opera') == -1)
	{
		// Set the optional parameter if needed
	   
	   if ( width === undefined ) {
		  width = maxw;
	   } else {
			maxw=width;   
	   }
	   
		w=width;
		h='auto'; 
		
		// followmouse()
		
		if (!maxw === undefined) { // if a max width override is specified, use it. Otherwise leave at default of 250px
			gettrailobj().maxWidth=maxw;
		}
		
		if (content === undefined) {
			document.getElementById('trailimageid').innerHTML='<img id="ttimg" src="' + file + '" />';	
		} else {
			gettrailobj().padding="5px";
			document.getElementById('trailimageid').innerHTML='<img id="ttimg" src="' + file + '" /><br />' + content;
		}
		//document.getElementById('ttimg').src=file
		document.onmousemove=followmouse
		gettrailobj().visibility="visible"
		gettrailobj().width=w
		gettrailobj().height=h
	}
}


// tracks the mouse coordinates. Sets the left and top styles of the trailimageid div.
// offsets by 20px x and y, and compensates for the image going off the screen by re-adjusting as needed.
function followmouse(e)
{

	if(navigator.userAgent.toLowerCase().indexOf('opera') == -1)
	{

		var xcoord=20
		var ycoord=-20

		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
		}

		// do away with off-screen compensation, because we're not dealing with large images
		
		var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15
		var docheight=document.all? Math.max(truebody().scrollHeight, truebody().clientHeight) : Math.max(document.body.offsetHeight, window.innerHeight)

		if (xcoord+w+3>docwidth) {
			xcoord=xcoord-w-(20*2)
		}

		if (ycoord-truebody().scrollTop+h>truebody().clientHeight) {
			actualHeight = gettrailobj().height
			ycoord=ycoord-actualHeight-20;
		}
		
		
		gettrailobj().left=xcoord+"px"
		gettrailobj().top=ycoord+"px"

	}

}
