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

/**
 * Modifications by Patrick Nelson (pat@catchyour.com -- patricksmedium.com)
 *
 * Copied from iStockPhoto and modified to:
 *
 * Allow up to two images, vertically or side by side and to
 * automatically calculate image height without receiving parameters for height.
 * Also modified to better reposition image when mouse in ANY location. 
 * Also added preload code to show "wait.gif" image while image loads. Tested
 * and compatible with IE6, IE7, IE8, Firefox, Opera and Chrome.
 */

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


// Preload "wait.gif" image. Will work even if image doesn't exist (has failsafe to prevent showing red "X").
var usewaitimg = false;
var waitimg = new Image();
waitimg.onload = function() { usewaitimg = true; }
waitimg.src = "/images/wait.gif";

// Output trail image DIV.
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 {
		useimg = imagename1;
		if (usewaitimg) useimg = "/images/wait.gif";
		newHTML = newHTML + '<img src="' + useimg + '" border="0" id="trailimage_image">';
	}
	newHTML = newHTML + '</div>';
	
	newHTML = newHTML + '</div>';
	gettrailobjnostyle().innerHTML = newHTML;
	gettrailobj().display="inline";
	if (typeof(imagename2) == "undefined") trail_preload(imagename1);
}


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
	
	// MODIFIED
	scrolltop = jQuery ? Math.max(truebody().scrollTop, $("body").scrollTop()) : truebody().scrollTop;
	
	// 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 + 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 - scrolltop;
	if (docheight - mouseyWin < divheight){
		if (!xbalancing) {
			// Balance DIV on the bottom of the window.
			ycoord = docheight + 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"
}




var loadedimgs = Array();
function trail_preload(imagename1) {
	// Make sure image hasn't already been loaded.
	for(i=0; i<loadedimgs.length; i++) {
		if (loadedimgs[i][0] == imagename1) {
			// Go ahead and display image now.
			document.getElementById("trailimage_image").src = loadedimgs[i][1];
			debug("set image to previously loaded:  " + loadedimgs[i][1]);  
			return true;
		}
	}
	
	debug("creating new image to load:  " + imagename1);
	loadimg = new Image();
	loadimg.onload = function() {
		document.getElementById("trailimage_image").src = this.src;
		debug("loaded:  " + this.src);
		followmouse();
	}
	loadimg.src = imagename1;
	loadedimgs[loadedimgs.length] = Array(imagename1, loadimg.src);
	return true;
}

function debug(text) {
	if (typeof(console) != "undefined") {
		console.log(text);
	}
}