var offsetfrommouse=[15,15]; //image x,y offsets from cursor position in pixels. Enter 0,0 for no offset
var currentimagewidth = 400;
var currentimageheight = 400;

if (document.getElementById || document.all) {
 document.write('<div id="trailimageid">');
 document.write('</div>');
}

if(window.addEventListener){
window.addEventListener('load', trailInit, false);
}else{
	if(window.attachEvent){
window.attachEvent('onload', trailInit);
	}
} 

function trailInit(){
	
	var checky = $('top3').getElementsBySelector('a[onclick]');
	//alert(checky.length);
	
	//var value=checky[0].checked;
	 checky.each(function(kwick, i) { 
		
		val=kwick.attributes['onclick'].value.split('?'); 
		if(val[1]){
		val2=val[1].split('\''); 
		var q = val2[0].toQueryParams(); 
		//alert(q.file);	 
			 
		Event.observe(kwick, 'mouseover', function(event) {   
		//var element = Event.element(event); 
		//alert(q.file);
		showtrail(q.file,'');
		//alert(element.parentNode.attributes['onclick'].value);
		//alert(element.tagName);
		});
		
		Event.observe(kwick, 'mouseout', function(event) {   
		//var element = Event.element(event); 
		//alert(q.file);
		hidetrail();
		//alert(element.parentNode.attributes['onclick'].value);
		//alert(element.tagName);
		});
		}
		/*
		"showtrail('fileadmin/images/history/history_1887.jpg','')" onmouseout=hidetrail();
		*/
			
	 });
}

function gettrailobj(obj_name) {
 var obj = gettrailobjnostyle(obj_name)
 if (obj)
 return obj.style;
 return null;
}

function gettrailobjnostyle(obj_name) {
 if (document.getElementById)
 return document.getElementById(obj_name);
 else if (document.all)
 return document.all[obj_name];
}

function truebody() {
 return (!window.opera && document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
}

function setSize() {
 currentimagewidth = gettrailobjnostyle("trailimageid").offsetWidth;
 currentimageheight = gettrailobjnostyle("trailimageid").offsetHeight;
}

function showtrail(imagename, title) {
 document.onmousemove = followmouse;

 newHTML = '<div style="padding: 0px; background-color: #FFFFFF; border: 1px solid #590c00;">';
//newHTML = newHTML + '<h1>' + title + '</h1>';
 newHTML = newHTML + '<div align="center" style="padding: 0;">';
 newHTML = newHTML + '<img src="' + imagename + '" border="0" onload="setSize();"></div>';
 newHTML = newHTML + '</div>';

 gettrailobj("trailimageid").left = "-1000px";
 gettrailobjnostyle("trailimageid").innerHTML = newHTML;
 gettrailobj("trailimageid").display = "inline";
}

function hidetrail() {
 gettrailobj("trailimageid").innerHTML = " ";
 gettrailobj("trailimageid").display = "none";
 document.onmousemove = "";
 gettrailobj("trailimageid").left = "-1000px";
}

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 < (currentimagewidth - 20))
 xcoord = e.pageX - xcoord - currentimagewidth; // Move to the left side of the cursor
 else
 xcoord += e.pageX;
 if (docheight - e.pageY > (currentimageheight + 20))
 ycoord += e.pageY - Math.max(0,(20 + currentimageheight + e.pageY - docheight - truebody().scrollTop));
 else
 ycoord += e.pageY;
 } else if (typeof window.event != "undefined") {
 if (docwidth - event.clientX < (currentimagewidth - 20))
 xcoord = event.clientX + truebody().scrollLeft - xcoord - currentimagewidth; // Move to the left side of the cursor
 else
 xcoord += truebody().scrollLeft + event.clientX;
 if (docheight - event.clientY > (currentimageheight + 20))
 ycoord += event.clientY + truebody().scrollTop - Math.max(0, (20 + currentimageheight + event.clientY - docheight));
 else
 ycoord += truebody().scrollTop + event.clientY;
 }

 if(ycoord < 0)
 ycoord = ycoord*-1;
 gettrailobj("trailimageid").left = xcoord + "px";
 gettrailobj("trailimageid").top = ycoord + "px";
}

