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

/**
 * Then copied from iStockPhoto and HIGHLY modified...
 *
 * Modified to allow up to two images, vertically or side by side and to
 * automatically calculate image height without receiving parameters for height.
 *
 * Modifications by Patrick Nelson (pat@catchyour.com -- patricksmedium.com)
 */

var offsetfrommouse = 15;
var displayduration = 0; //duration in seconds image should remain visible. 0 for always.

if (document.getElementById || document.all){
	document.write('<div id="trailimageid" style="position: absolute; display: none; left: 0px; top: 0px; z-index: 200;">');
	document.write('</div>');
}

function gettrailobj(){
if (document.getElementById)
return document.getElementById("trailimageid").style
else if (document.all)
return document.all.trailimagid.style
}

function gettrailobjnostyle(){
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 showtrail(imagename1, imagename2, horizontal){

	document.onmousemove = followmouse;
	if (typeof(horizontal) == "undefined") horizontal = true;
	
	newHTML = '<div style="padding: 5px; background-color: #FFF; border: 1px solid #888;">';
	newHTML = newHTML + '<div align="center" style="padding: 2px 2px 2px 2px;">';
	if (typeof(imagename2) != "undefined") {
		if (horizontal) {
			newHTML = newHTML + '<table border="0" cellspacing="0" cellpadding="0"><tr>';
			newHTML = newHTML + '<td valign="top"><img src="' + imagename1 + '" border="0"></td>';
			newHTML = newHTML + '<td>&nbsp;&nbsp;&nbsp;</td>';
			newHTML = newHTML + '<td valign="top"><img src="' + imagename2 + '" border="0"></td>';
			newHTML = newHTML + '</tr></table>';
		} else {
			newHTML = newHTML + '<div><img src="' + imagename1 + '" border="0"></div>';
			newHTML = newHTML + '<div style="padding-top: 8px;"><img src="' + imagename2 + '" border="0"></div>';
		}
	} else {
		newHTML = newHTML + '<img src="' + imagename1 + '" border="0">';
	}
	newHTML = newHTML + '</div>';
	
	newHTML = newHTML + '</div>';
	gettrailobjnostyle().innerHTML = newHTML;
	gettrailobj().display="inline";
}


function hidetrail(){
	gettrailobj().display="none"
	document.onmousemove=""
	gettrailobj().left="-1000px"
	gettrailobj().top="-1000px"
}

function followmouse(e){
	// Ultimate position of div.
	var xcoord = offsetfrommouse;
	var ycoord = offsetfrommouse
	
	// Document (window) dimensions.
	var docwidth = document.all ? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15
	var docheight = document.all ? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight)
	
	// Trailing DIV dimensions.
	var divheight = gettrailobjnostyle().offsetHeight + 16;
	var divwidth = gettrailobjnostyle().offsetWidth + 16;
	
	// Get mouse position.
	if (typeof e != "undefined"){
		// Most Mozilla-based browsers.
		mousex = e.pageX;
		mousey = e.pageY;
	} else if (typeof window.event != "undefined"){
		// IE 5/6
		mousex = event.clientX + truebody().scrollLeft;
		mousey = event.clientY + truebody().scrollTop;
	}
	
	/**
	 * Reposition DIV based on mouse position.
	 */
	xbalancing = false;
	mousexWin = mousex + truebody().scrollLeft;
	if (docwidth - mousex < divwidth){
		// Check to see if there's room on the left side.
		if (mousex > divwidth) {
			xcoord = mousex - divwidth;
		} else {
			// Balance DIV on the right side of the window.
			xbalancing = true;
			xcoord = docwidth - divwidth + offsetfrommouse;
		}
	} else {
		// Update to right of cursor.
		xcoord = mousex + offsetfrommouse;
	}
	
	// See if DIV is passing bottom of window.
	mouseyWin = mousey - truebody().scrollTop;
	if (docheight - mouseyWin < divheight){
		if (!xbalancing) {
			// Balance DIV on the bottom of the window.
			ycoord = docheight + truebody().scrollTop - divheight + offsetfrommouse;
		} else {
			// Try to move DIV above mouse instead (of the area above it has more room).
			//if (
			//ycoord = mousey - divheight;
			if (mouseyWin > docheight - mouseyWin) {
				ycoord = mousey - divheight;
			} else {
				ycoord = mousey + offsetfrommouse;
			}
		}
	} else {
		ycoord = mousey + offsetfrommouse;
	}
	
	// Update DIV position.
	gettrailobj().left	= xcoord + "px"
	gettrailobj().top	= ycoord + "px"
}

