/*
Simple Image Trail script- By JavaScriptKit.com
Visit http://www.javascriptkit.com for this script and more
This notice must stay intact
*/

var trailimage=['',0,0];//["http://192.168.1.2/gunnersens.com.au/www/components/com_virtuemart/shop_image/product/17b36f097dc66db6d7e0d5489b46089c.jpg", 100, 99] //image path, plus width and height
var offsetfrommouse=[50,-20] //image x,y offsets from cursor position in pixels. Enter 0,0 for no offset
var displayduration=1 //duration in seconds image should remain visible. 0 for always.

var isIE = false;
if(navigator.userAgent.indexOf("MSIE")!=-1 && navigator.userAgent.indexOf("Opera")==-1 ){
	isIE = true;	
} else {
	isIE = false;	
}
var defaultimageheight = 50;
var defaultimagewidth = 50;

var currSource = '';

title = 'no image yet';
newHTML = '<div id="trailimageid" style="position:absolute;visibility:visible;left:0px;top:0px;width:1px;height:1px;z-index:110;">';
newHTML += '<div id="main_hover_container" style="padding:10px;margin:10px;border:1px solid black;background-color:white;width:'+trailimage[1]+'px">';
newHTML += '<img id="hover_image_img" src="'+trailimage[0]+'" border="0" width="'+trailimage[1]+'px" height="'+trailimage[2]+'px" />';
newHTML += '<div id="swatch_title" style="text-align:center;font-weight:bold;width:100%;font-size:10px;">'+title+'</div>';
newHTML += '</div>';
newHTML += '</div>';

if (document.getElementById || document.all) {
	document.write(newHTML);
	//document.write('<div id="trailimageid" style="position:absolute;visibility:hidden;left:0px;top:0px;width:1px;height:1px;z-index:110">');
	//document.write('<img id="im1" src="'+trailimage[0]+'" border="0" width="'+trailimage[1]+'px" height="'+trailimage[2]+'px" />');
	//document.write('</div>');
}
newHTML = '';
var called = 0;

function gettrailobj(){
	if (document.getElementById)
	return document.getElementById("trailimageid").style
	else if (document.all)
	return document.all.trailimagid.style
}

function gettrailobj_nostyle(){
	if (document.getElementById)
	return document.getElementById("trailimageid")
	else if (document.all)
	return document.all.trailimagid
}

function showtrail(imagename,title,width,height) {
	
	var img = document.getElementById('hover_image_img');
	img.style.visibility = 'visible';
	img.style.width = width+'px';
	img.style.height = height+'px';
	
	//gettrailobj().width = width+'px';
	
	var div_cont = document.getElementById('main_hover_container');
	div_cont.style.width = width+'px';
	
	i = imagename
	t = title
	w = width
	h = height
	//helps avoid mouse going over the shown image?
	//timer = setTimeout("show('"+i+"',t,w,h);",200);
	show(i,t,w,h);
}

function show(imagepath,title,width,height){
	trailimage = [imagepath, width, height];
	var obj = gettrailobj_nostyle();
	obj.style.visibility = 'visible';
	
	newHTML = '';
	
	defaultimageheight = height+100;
	defaultimagewidth = width;
	divWidth = width;
	
	
	//if(navigator.userAgent.indexOf("MSIE")!=-1 && navigator.userAgent.indexOf("Opera")==-1 ){
		
		var img = document.getElementById('hover_image_img');
		var tit = document.getElementById('swatch_title');
		tit.innerHTML = title;
		img.src = imagepath;
		
		
		
		newHTML += '<div style="padding:10px;margin:10px;border:1px solid black;background-color:white;width:'+divWidth+'px">';
		newHTML += '<div id="swatch_title" style="text-align:center;font-weight:bold;width:100%;font-size:10px;">'+title+'</div>';
		newHTML += '<img id="im1" src="'+img.src+'" border="0" width="'+width+'px" height="'+height+'px" />';
		newHTML += '</div>';
		
	/*
	} else {
		newHTML += '<div id="main_hover_container" style="padding:10px;margin:10px;border:1px solid black;background-color:white;width:'+divWidth+'px">';
		newHTML += '<div style="text-align:center;font-weight:bold;width:100%;">'+title+'</div>';
		newHTML += '<img src="'+imagepath+'" border="0" width="'+width+'px" height="'+height+'px" />';
		newHTML += '</div>';
		obj.innerHTML = newHTML;
	}
	*/
	

	
	document.onmousemove=followmouse;
}


function truebody(){
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function hidetrail(){
	gettrailobj().visibility="hidden"
	gettrailobj().left = '-1000px';
	var img = document.getElementById('hover_image_img');
	img.style.visibility = 'hidden';
	document.onmousemove=""
}


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 e != "undefined"){
		if (docwidth - e.pageX < defaultimagewidth + 2*offsetfrommouse[0]){
			xcoord = e.pageX - xcoord - defaultimagewidth; // Move to the left side of the cursor
		} else {
			xcoord += e.pageX;
		}
		if (docheight - e.pageY < defaultimageheight + 2*offsetfrommouse[1]){
			ycoord += e.pageY - Math.max(0,(2*offsetfrommouse[1] + defaultimageheight + e.pageY - docheight - truebody().scrollTop));
		} else {
			ycoord += e.pageY;
		}

	} else if (typeof window.event != "undefined"){
		if (docwidth - event.clientX < defaultimagewidth + 2*offsetfrommouse[0]){
			xcoord = event.clientX + truebody().scrollLeft - xcoord - defaultimagewidth-100; // Move to the left side of the cursor
		} else {
			xcoord += truebody().scrollLeft+event.clientX
		}
		if (docheight - event.clientY < (defaultimageheight + 2*offsetfrommouse[1])){
			ycoord += event.clientY + truebody().scrollTop - Math.max(0,(2*offsetfrommouse[1] + defaultimageheight + event.clientY - docheight));
		} else {
			ycoord += truebody().scrollTop + event.clientY;
		}
	}
	
	//xcoord -= 600;
	//ycoord -=300;
	
	gettrailobj().left=xcoord+"px"
	gettrailobj().top=ycoord+"px"
	
	if(isIE) {
		document.onmousemove = '';	
	}
	
	debug('Top: '+ycoord+"px"+ ' Left: '+xcoord+"px");
}


function debug(msg) {
	//var d = document.getElementById('debug');
	//d.innerHTML = msg;
}

hidetrail();
