var offsetfrommouse=[300,-4]; //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 currentimageheight = 200; // maximum image size.

if (document.getElementById || document.all){
	document.write('<div id="popup" style="display:none; position: absolute;">');
	document.write('</div>');
}

function gettrailobj(){
	if (document.getElementById)
	return document.getElementById("popup").style
	else if (document.all)
	return document.all.trailimagid.style
}

function gettrailobjnostyle(){
	if (document.getElementById)
	return document.getElementById("popup")
	else if (document.all)
	return document.all.trailimagid
}

function truebody(){
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function getScrollTop(){
	return (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
}

function getScrollLeft(){
	return (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
}

var fadeTimer = null;
var currentFade = 0;

function startFade(id){
	stopFade();
	var speed = 2;
	currentFade = 0;
	if (!fadeTimer){
		fadeTimer = window.setInterval('doFade(\''+id+'\','+speed+')', 5);
	}
	doFade(id,0);
}

function doFade(id,speed){
	if (currentFade < 100){
		currentFade+=speed;
//		alert(currentFade);
		if (currentFade>100) currentFade=100;
		
		var element = document.getElementById(id);

		element.style.opacity = currentFade/100;
		element.style.filter = 'alpha(opacity='+(currentFade)+')'
	} else {
		stopFade();
	}
//	element.scrollLeft = element.scrollLeft + speed;
}

function stopFade(){
	window.clearInterval(fadeTimer);
	fadeTimer = null;
	currentFade = 0;
}

function showtrail(imagename,title,description,ratingaverage,ratingnumber,showthumb,height,filetype){

	if (height > 0){
		currentimageheight = height;
	}

	document.onmousemove=followmouse;

	cameraHTML = '';

	newHTML = '<div>';

	if (showthumb > 0){
		newHTML += '<div>';
		newHTML += '<div>';
		newHTML += '<img id="popup_img" src="' + imagename + '" style="border: 1px solid #000;"></div></div>';
	}
	
	newHTML = newHTML + '</div>';
	gettrailobjnostyle().innerHTML = newHTML;
	gettrailobj().display="inline";
	startFade('popup_img');
}

function hidetrail(){
//	stopFade();
	gettrailobj().innerHTML = " ";
	gettrailobj().display="none";
	document.onmousemove="";
	gettrailobj().left="-500px";
}

function followmouse(e){
	var xcoord=offsetfrommouse[0];
	var ycoord=offsetfrommouse[1];

	var docwidth=document.all? getScrollLeft()+truebody().clientWidth : pageXOffset+window.innerWidth-15
	var docheight=document.all? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight)

	if (typeof e != "undefined"){
		xpos = e.pageX;
		if (docwidth - xpos < 380){
			xcoord =  xpos - xcoord - 400; // Move to the left side of the cursor
		} else {
			xcoord += xpos;
		}
		if (docheight - e.pageY < (currentimageheight + 110)){
			ycoord += e.pageY - Math.max(0,(110 + currentimageheight + e.pageY - docheight - getScrollTop()));
			//ycoord += e.pageY;
		} else {
			ycoord += e.pageY;
		}
	} else if (typeof window.event != "undefined"){
		xpos = event.clientX;
		if (docwidth - xpos < 380){
			xcoord = xpos + getScrollLeft() - xcoord - 400; // Move to the left side of the cursor
		} else {
			xcoord += getScrollLeft()+xpos;
		}
		if (docheight - event.clientY < (currentimageheight + 110)){
			ycoord += event.clientY + getScrollTop() - Math.max(0,(110 + currentimageheight + event.clientY - docheight));
		} else {
			ycoord += getScrollTop() + event.clientY;
		}
	}

	var docwidth=document.all? getScrollLeft()+truebody().clientWidth : pageXOffset+window.innerWidth-15
	var docheight=document.all? Math.max(truebody().scrollHeight, truebody().clientHeight) : Math.max(document.body.offsetHeight, window.innerHeight)
	if(ycoord < 0) { 
		ycoord = ycoord*-1; 
	}
	
	gettrailobj().left=xcoord+"px";
	gettrailobj().top=ycoord+"px";
}
