/*
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,25]; //image x,y offsets from cursor position in pixels. Enter 0,0 for no offset
var displayduration=0; //duration in seconds image should remain visible. 0 for always.

var previewImageHeight;	// maximum image size.
var previewImageWidth;	// maximum image size.

var defaultImageHeight = 400;	// maximum image size.
var defaultImageWidth = 500;	// maximum image size.

var timer;

function addPreviewDiv() {
	// Write div
	var newdiv = document.createElement('div');
	newdiv.setAttribute('id', 'preview_div');
	document.body.appendChild(newdiv);
}

attachEventListener(window, 'load', addPreviewDiv, false);

var previewImg;

function gettrailobj(){
	if (document.getElementById)
		return document.getElementById("preview_div").style
}

function gettrailobjnostyle(){
	if (document.getElementById)
		return document.getElementById("preview_div")
}

function truebody(){
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function hideTrail(){	
	if(!gettrailobjnostyle() || !previewImg) return false;
	gettrailobj().display= "none";
	detachEventListener(previewImg, 'mousemove', followMouse, false);	
	gettrailobj().left="-1024px"
	clearTimeout(timer);
}

function showTrail(imgCtl, imgUrl, title, width, height){

	if(!gettrailobjnostyle()) return false;

    var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth - offsetfrommouse[0];
	var docheight=document.all? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight);


//	if( (navigator.userAgent.indexOf("Konqueror")==-1  || navigator.userAgent.indexOf("Firefox")!=-1 || (navigator.userAgent.indexOf("Opera")==-1 && navigator.appVersion.indexOf("MSIE")!=-1)) && (docwidth>650 && docheight>500)) {
	if( (navigator.userAgent.indexOf("Konqueror")==-1  || navigator.userAgent.indexOf("Firefox")!=-1 || (navigator.userAgent.indexOf("Opera")==-1 && navigator.appVersion.indexOf("MSIE")!=-1)) ) {

		var newImage = new Image();
		newImage.src = imgUrl;
		width = newImage.width;
		height = newImage.height;
		
		if ((width > defaultImageWidth) || (height > defaultImageHeight))
		{
			thumbWidth 	= defaultImageHeight;
			thumbHeight	= Math.round((thumbWidth/width)*height);
		
			if(thumbHeight > height)
			{
				thumbHeight	= height;
				thumbWidth 	= Math.round((thumbHeight/height)*width);
			}
			
			width 	= thumbWidth?thumbWidth:width;
			height	= thumbHeight?thumbHeight:height;
		}
		
		( !width ) ? width = defaultImageWidth: '';
		( !height ) ? height = defaultImageHeight: '';

		widthDiv = width + 30;
		heightDiv = height + 50;
		//width+=30;
		//height+=50;
		
		previewImageHeight = height;
		previewImageWidth = width;

		newHTML = '<div class="border_preview" style="width:'+  widthDiv +'px;height:'+ heightDiv +'px"><div id="loader_container"><div id="loader"><center><div id=progress></div></center></div></div>';
		newHTML = newHTML + '<div class="preview_temp_load"><b>' + title + '</b><div style=height:5px;></div>'
    	newHTML = newHTML + '<img onload="javascript:remove_loading();" class="imgBorder" src="' + imgUrl + '" width="'+  width +'" height="'+ height +'" border="0" ></div>';
		newHTML = newHTML + '</div>'; 
		
		if(navigator.userAgent.indexOf("MSIE")!=-1 && navigator.userAgent.indexOf("Opera")==-1 ){
			newHTML = newHTML+'<iframe src="about:blank" scrolling="no" frameborder="0" width="'+width+'" height="'+height+'"></iframe>';
		}		

		gettrailobjnostyle().innerHTML = newHTML;
		gettrailobj().display="block";
		
		previewImg = imgCtl;
		attachEventListener(imgCtl, 'mousemove', followMouse, false);
	}
}

function followMouse(e){

	var xcoord=offsetfrommouse[0];
	var ycoord=offsetfrommouse[1];

	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);

	if (typeof window.event != "undefined"){

		if ((docwidth - event.clientX) < (previewImageWidth + 2*offsetfrommouse[0])){
			xcoord = event.clientX + truebody().scrollLeft - (xcoord*3) - previewImageWidth; // Move to the left side of the cursor
		} else {
			xcoord += truebody().scrollLeft+event.clientX;
		}
		if (docheight - event.clientY < (previewImageHeight + 2*offsetfrommouse[1])){
			ycoord += event.clientY + truebody().scrollTop - Math.max(0,(2*offsetfrommouse[1] + previewImageHeight + event.clientY - docheight));
		} else {
			ycoord += truebody().scrollTop + event.clientY;
		}
	//	xcoord -= 20;
		ycoord -= 50;
//			alert('here' + ycoord);
	} else if (typeof e != "undefined"){
		if ((docwidth - e.pageX) < (previewImageWidth + 2*offsetfrommouse[0])){
			xcoord = e.pageX - (xcoord*3) - previewImageWidth; // Move to the left side of the cursor
		} else {
			xcoord += e.pageX;
		}
		if ((docheight - e.pageY) < (previewImageHeight + 2*offsetfrommouse[1])){
			ycoord += e.pageY - Math.max(0,((2*offsetfrommouse[1] + previewImageHeight + e.pageY) - (docheight + truebody().scrollTop) ));
		} else {
			ycoord += e.pageY;
		}
		xcoord -= 10;
		ycoord -= 50;
	}

	var trailObj = gettrailobj();

	trailObj.left=xcoord+"px";
	trailObj.top=ycoord+"px";
}
