/*
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 offsetframex = 0;
var offsetframey = 0;
var displayduration = 0; //duration in seconds image should remain visible. 0 for always.
var docobj = document;
var winobj = window;
var frameid = "";
var ieframes = Array();

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, theframeid){
	if (typeof(theframeid) == "undefined") {
		// Set Defaults
		var docobj = document;
		var winobj = window;
	} else {
		// Get frame.
		theframe = document.getElementById(theframeid);
		frameid = theframeid;
		offsetframey = 0;
		if (document.all) { // IE
			docobj = theframe.Document;
			// THIS MAY BE AN INCREDIBLY UGLY HACK, BUT -- it works in IE!!  >:(
			theframe.contentWindow.setWin();
			offsetframey = 0 - truebody().scrollTop;
			hideSelects();
		} else {
			docobj = theframe.contentDocument;
			winobj = theframe.contentWindow;
		} 
		
		// Extend offset from mouse even further to compensate for frame location.
		offsetframex = findPosX(theframe);
		offsetframey += findPosY(theframe);
		//console.log("Left: " + findPosX(theframe) + ", Top: " + findPosY(theframe));
	}
	docobj.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"
	docobj.onmousemove = ""
	//document.onmousemove=""
	gettrailobj().left = "-1000px"
	gettrailobj().top = "-1000px"
	if (document.all) showSelects();
}

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 winobj.event != "undefined" && winobj.event != null){
		// IE 5/6
		mousex = winobj.event.clientX + truebody().scrollLeft;
		mousey = winobj.event.clientY + truebody().scrollTop;
	} else if (winobj == null) {
		// IE is very messed up -- just give up this particular issue.
		hidetrail();
		return false;
	}
	
	// Compensate mouse position based on frame location (of applicable).
	mousex += offsetframex;
	mousey += offsetframey;
	
	/**
	 * 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"
}






function findPosX(obj) {
  var curleft = 0;
  if(obj.offsetParent)
      while(1) 
      {
        curleft += obj.offsetLeft;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
  else if(obj.x)
      curleft += obj.x;
  return curleft;
}

function findPosY(obj) {
  var curtop = 0;
  if(obj.offsetParent)
      while(1)
      {
        curtop += obj.offsetTop;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
  else if(obj.y)
      curtop += obj.y;
  return curtop;
}




function hideSelects(){
	var oSelects=document.getElementsByTagName("select");
	for(var i=0;i<oSelects.length;i++)
		oSelects[i].style.visibility = "hidden";
}


function showSelects(){
	var oSelects=document.getElementsByTagName("select");
	for(var i=0;i<oSelects.length;i++)
		oSelects[i].style.visibility = "visible";
}


