/*
Simple Image Trail script- By JavaScriptKit.com
Visit http://www.javascriptkit.com for this script and more
This notice must stay intact
*/
var offsetfrommouse=[15,0];//image x,y offsets from cursor position in pixels. Enter 0,0 for no offset
var anchorImageToCursor=false;
function gettrailobj(){
if(document.getElementById){
return document.getElementById("imagePreviewContainerOuterDiv").style;
}else if(document.all){
return document.all.imagePreviewContainerOuterDiv.style;
}
}
function gettrailobjnostyle(){
if(document.getElementById){
return document.getElementById("imagePreviewContainerOuterDiv");
}else if(document.all){
return document.all.imagePreviewContainerOuterDiv;
}
}
function truebody(){
return(!window.opera&&document.compatMode&&document.compatMode!="BackCompat")?document.documentElement:document.body;
}
function hidetrail(){
gettrailobjnostyle().innerHTML="";
gettrailobj().display="none";
document.onmousemove="";
gettrailobj().left="-1500px";
}
function showtrail(imagename,maxWidth,maxHeight,minWidth,minHeight,displayIfMin,anchorToCursor){
anchorImageToCursor=anchorToCursor;
document.onmousemove=followmouse;
var html='<div id="imagePreviewContainerInnerDiv"><div id="imagePreviewDiv"><img id="largeImage"src="/listings/img/stock/page/ajax-loader.gif"alt="loading image"/></div></div>';
gettrailobjnostyle().innerHTML=html;
gettrailobj().display="inline";
var image=new Image();
image.onload=function(){
var width=image.width;
var height=image.height;
var solwidth=500;
var solheight=500;
var scaleRatio;
if(width>maxWidth||height>maxHeight){
if(width>height){
scaleRatio=width/maxWidth;
width=width/scaleRatio;
height=height/scaleRatio;
}else{
scaleRatio=height/maxHeight;
width=width/scaleRatio;
height=height/scaleRatio;
}
}
if(!displayIfMin&&(width<=minWidth&&height<=minHeight)){
hidetrail();
}
if(document.getElementById('largeImage')!=undefined){
document.getElementById('largeImage').width=width;
document.getElementById('largeImage').height=height;
document.getElementById('largeImage').src=imagename;
}
}
image.src=imagename;
}
function findPos(obj){
var curleft=curtop=0;
if(obj.offsetParent){
curleft=obj.offsetLeft
curtop=obj.offsetTop
while(obj=obj.offsetParent){
curleft+=obj.offsetLeft
curtop+=obj.offsetTop
}
}

return[curleft,curtop];
}

function followmouse(e){
var xcoord=offsetfrommouse[0];
var ycoord=offsetfrommouse[1];
var largeImage=document.getElementById('largeImage');
var imagePaddingHeight=45;
var imagePaddingWidth=35;
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);
var leftNav=document.getElementById("leftcol");

if(leftNav==null){
  leftNav = document.body;
}

var leftNavCoords=findPos(leftNav);

//Calculate how much padding to give so that the image preview stays in the
//middle of the left navigation
var imagePreviewContainer=document.getElementById('imagePreviewContainerInnerDiv');
var leftPadding=((leftNav.offsetWidth-(imagePreviewContainer.offsetWidth))/2);

//move the x-cords to move the image to the center of leftnav
leftNavCoords[0] = leftNavCoords[0] + leftPadding;


if(typeof e!="undefined"){
//Flip image to the left of the pointer
  if(docwidth-e.pageX<largeImage.width+imagePaddingWidth){
    if(anchorImageToCursor){
      xcoord=leftNavCoords[0];
    }else{
      xcoord=e.pageX-xcoord-largeImage.width;
    }
  }else{
     if(anchorImageToCursor){
     	  xcoord=leftNavCoords[0];
		 }else{
     		xcoord+=e.pageX;
  	 }
	}
	
 	 //Flip image to the top of the pointer
	if(docheight-e.pageY<(largeImage.height+imagePaddingHeight)){
	 		ycoord+=e.pageY-Math.max(0,(imagePaddingHeight+largeImage.height+e.pageY-docheight-truebody().scrollTop));
	}else{
			ycoord+=e.pageY;
	}
}
else if(typeof window.event!="undefined"){
  if(docwidth-event.clientX<largeImage.width+imagePaddingWidth){
    if(anchorImageToCursor){
			xcoord=leftNavCoords[0];
    }else{
      xcoord=event.clientX+truebody().scrollLeft-xcoord-largeImage.width-10;
    }
 }else{
   if(anchorImageToCursor){
     xcoord=leftNavCoords[0];
   }else{
     xcoord+=truebody().scrollLeft+event.clientX;
   }
 }
 
 if(docheight-event.clientY<(largeImage.height+imagePaddingHeight)){
 		ycoord+=event.clientY+truebody().scrollTop-Math.max(0,(imagePaddingHeight+largeImage.height+event.clientY-docheight));
 }else{
    ycoord+=truebody().scrollTop+event.clientY;
 }
}

if(ycoord<0){
 ycoord=ycoord*-1;
}
gettrailobj().display="block";
gettrailobj().left=xcoord +"px";
gettrailobj().top=ycoord+"px";
}