/*
Simple Image Trail script- By JavaScriptKit.com
Visit http://www.javascriptkit.com for this script and more
This notice must stay intact
*/

/*
 *  Adapted to suit requirements of strategicecommerce.com.au
 *
 *  ie 
 *      cursor hovers over thumbnails and we show a larger version in a floating div.
 *      floating div displayed on left or right of cursor depending on available space.
 *      floating div appearance is completely defined in a style sheet.
 */
var mouseOffSetX = 12;
var mouseOffSetY = 12;
var debugging = false;
var debugText = '';

if (document.getElementById || document.all)
{
	document.write('<div id="trailimageid" name="trailimageid"></div>');
}

/*
 *  Modified to return the actual object instead of the style object.
 *  Existing callers also modified to use obj.style syntax.
 */
function gettrailobj()
{
	if (document.getElementById)
		return document.getElementById("trailimageid");
	else if (document.all)
		return document.all.trailimagid;
}

function truebody()
{
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

function hidetrail()
{
	gettrailobj().style.visibility="hidden";
	gettrailobj().style.left = -500;  // stops the flicker caused if last position of trailobj is under current cursor position.
	document.onmousemove='';
}

function followmouse(e)
{
	debugText = '';

	var mouseY = 0;
	var mouseX = 0;
	var xcoord = 0;
	var ycoord = 0;
	var docwidth = 0;
	var docheight = 0;
	var distToBottom = 0;

	if (typeof e != "undefined")
	{
		// Firefox etc
		mouseX = e.pageX;
		mouseY = e.pageY;
		docwidth = pageXOffset + window.innerWidth;
		docheight = document.body.scrollTop + window.innerHeight;
		docheight -= 20; // avoid creating scroll bars and making the images jump around
		docwidth -= 35; // avoid creating scroll bars and making the images jump around

		debugText += 'height = ' + docheight;
		debugText += '<br>document.body.scrollTop = ' + document.body.scrollTop;
		debugText += '<br>document.body.offsetHeight = ' + document.body.offsetHeight;
		debugText += '<br>window.innerHeight = ' + window.innerHeight;
		distToBottom = docheight - mouseY - mouseOffSetY - gettrailobj().offsetHeight;
		debugText += '<br>distToBottom = ' + distToBottom;
		debugText += '<br>gettrailobj().offsetHeight = ' + gettrailobj().offsetHeight;
		if (distToBottom <= 0)
		{
			ycoord = docheight - gettrailobj().offsetHeight;
			debugText += '<br> first ycoord = ' + ycoord;
		}
		else
		{
			ycoord = mouseY + mouseOffSetY;
			debugText += '<br> second ycoord = ' + ycoord;
		}
	} 
	else if (typeof window.event != "undefined")
	{
		// IE 
		mouseX = event.clientX;
		mouseY = event.clientY;
		docwidth = truebody().scrollLeft + truebody().clientWidth;
		docheight = Math.min(truebody().scrollHeight, truebody().clientHeight) - 12;
		debugText += 'height = ' + docheight;

		distToBottom = docheight - mouseY - mouseOffSetY;
		debugText += '<br>distToBottom = ' + distToBottom;
		debugText += '<br>gettrailobj().offsetHeight = ' + gettrailobj().offsetHeight;
		if (gettrailobj().offsetHeight > distToBottom)
		{
			ycoord = docheight - gettrailobj().offsetHeight + mouseOffSetY + truebody().scrollTop;
			debugText += '<br> adjusted ycoord = ' + ycoord;
		}
		else
		{
			ycoord = mouseY + mouseOffSetY + truebody().scrollTop;
			debugText += '<br> normal ycoord = ' + ycoord;
		}
	}
	
	if (docwidth - mouseX < gettrailobj().offsetWidth)
	{
		xcoord = mouseX - mouseOffSetX - gettrailobj().offsetWidth; // Move to the left side of the cursor
	}
	else
	{
		xcoord = mouseX + mouseOffSetX;
	}
	debugText += '<br>mouseY = ' + mouseY;
	debugText += '<br>mouseOffSetY = ' + mouseOffSetY;
	debugText += '<br>truebody().scrollTop = ' + truebody().scrollTop;
	ycoord = Math.abs(ycoord);
	gettrailobj().style.left = xcoord + "px"
	gettrailobj().style.top = ycoord + "px"
}

/*
 *  Opposite of existing hidetrail, used as mouseover for thumbnail image.
 */
function showtrail(imagename, title, description, secondsToDisplay)
{
	//
	//  Duration in seconds that the image should remain visible. 
	//  0 or simply not passed, for display always.
	//
	if (secondsToDisplay > 0)
		setTimeout("hidetrail()", secondsToDisplay * 1000);

	//
	//  Rebuild the innerHTML of the image trail object to show the image
	//  and data passed as parameters.
	//
	var trailHTML = '';
	var trailObject = gettrailobj();

	// 
	//  Make sure these are all treated as strings.
	//
	imagename = '' + imagename;
	title = '' + title;
	description = '' + description;

	trailHTML = '<div class="trailImageContentPanel">';
	if (debugging && debugText.length > 0)
	{
		trailHTML = trailHTML + '<p class="trailImageDescription">' + debugText + '</p>';
	}
	if (title.length > 0)
	{
		trailHTML = trailHTML + '<p class="trailImageTitle">' + title + '</p>';
	}
	if (description.length > 0)
	{
		trailHTML = trailHTML + '<p class="trailImageDescription">' + description + '</p>';
	}
	if (imagename.length > 0)
	{
		trailHTML = trailHTML + '<div class="trailImageContainer"><img class="trailImage" src="' + imagename + '" ></div>';
	}

	trailHTML = trailHTML + '</div>';

	trailObject.innerHTML = trailHTML;
	trailObject.style.visibility = "visible";
	document.onmousemove = followmouse;
}

