
/*
Simple Image Trail script- By JavaScriptKit.com
Visit http://www.javascriptkit.com for this script and more
This notice must stay intact
*/

var screenx = screen.width;
var screeny = screen.height;

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
//if( screenx > 1280 && screeny > 1024 ) {
//	var offsetfrommouse=[250,-25]; //image x,y offsets from cursor position in pixels. Enter 0,0 for no offset
//} else {
	var offsetfrommouse=[30,5]; //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 isIE = false;
if(navigator.userAgent.indexOf("MSIE")!=-1 && navigator.userAgent.indexOf("Opera")==-1 ){
	isIE = true;	
} else {
	isIE = false;	
}
*/
var defaultimageheight = 400;
var defaultimagewidth = 400;

var currSource = '';

var title = 'no image yet';
var 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 += '<div id="swatch_title" style="text-align:center;font-weight:bold;width:100%;">'+title+'</div>';
newHTML += '<img id="hover_image_img" src="'+trailimage[0]+'" border="0" width="'+trailimage[1]+'px" height="'+trailimage[2]+'px" />';
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) {
	
	defaultimageheight = height;
	defaultimagewidth = width;
	
	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%;">'+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 x_padding = 20;
	
	var right_offset_x = offsetfrommouse[0] - 200;
	var left_offset_x = offsetfrommouse[0]  + 150;
	
	var offset_y = offsetfrommouse[1];
	
	
	var xcoord=0;
	var ycoord=0;
	
	var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-50;
	//window height
	var docheight=document.all? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight);

	var scroll_top = truebody().scrollTop;
	var scroll_left = truebody().scrollLeft;

	//firefox stuff here
	if (typeof e != "undefined"){

		xcoord = right_offset_x + e.pageX;
		
		
		right_view_edge = xcoord+defaultimagewidth + right_offset_x;
		
		if (right_view_edge > docwidth-280){
			xcoord = -300;//e.pageX - defaultimagewidth - left_offset_x -200; // Move to the left side of the cursor
			//alert('right_view_edge: '+right_view_edge+' docwidth: '+docwidth+ 'repo to: '+xcoord);
		}
//		alert('right_view_edge: '+right_view_edge+' docwidth: '+docwidth);
		
		
		//check vertical lower and higher limits
		
		//lower
		ycoord = offset_y + e.pageY;
		lower_view_edge = ycoord + defaultimageheight - 80;
		
		if(lower_view_edge > docheight+scroll_top) {
			//alert('ycoord: '+ycoord+' lower_view_edge'+lower_view_edge+' defaultimageheight: '+defaultimageheight+' docheight: '+docheight);
			ycoord = e.pageY - defaultimageheight - offset_y;
			
		}
			
		/*}
		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();
