
function ShowTrailImage(ximg_path, img_descr, img_id) {

// HACK!!! 
// popup_height hardcoded because FF doesn't always return the correct value (apparently is not the image that is loading, must investigate)
// this is around line 20

// get vars
var img_path = ximg_path; //image path
var offsetfrommouse=[10,10] //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 popup_class = "image_container"; // the class associated with the boxes (to reset visibility)

// reset other popups (FOR IE6/ IE7, to avoid flickering)
function hide_all(){

allPageTags = document.getElementsByTagName('div');
  
	// go through all divs
  for (i=0; i < allPageTags.length; i++) {
				var thePopup = 0;  
					// thi div shorcut
					if (allPageTags[i].className == popup_class) thePopup = allPageTags[i];
					
					if (thePopup) {
  					// go thought arrtributes
  					for (var n = 0; n < thePopup.attributes.length; n++) {
  						// find id attribute
  						if( thePopup.attributes[n].nodeName.toLowerCase() == 'id' ) {						    
  								// if id of div is not the one we care about hide it
  								if (thePopup.attributes[n].nodeValue != img_id) thePopup.style.visibility='hidden';	          
  						} // find id
  					} // go thought attributes
					} // if the popup

  } // go thourhg divs
} // function hide all 

hide_all();

// output div id (still hidden at this stage), but only if it wasn't created already before
if (!document.getElementById(img_id)) {
if (document.getElementById || document.all)
document.body.innerHTML += '<div class="image_container" id="' + img_id + '" style="position:absolute; top: 0; left: 0;visibility:hidden;"><div class="inner_div"><p>' + img_descr + '</p><img src="'+img_path+'"></div></div>';
}

// get popup dimensions
var popup = document.getElementById(img_id);
var popup_width = popup.offsetWidth;
// var popup_height = popup.offsetHeight                     HACK!!!
var popup_height = 449;  

// getElementById shortcut/ outdated browsers support
function gettrailobj(){
if (document.getElementById)
return document.getElementById(img_id).style
else if (document.all)
return document.all.trailimagid.style
}

function truebody(){
return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}




///////////////////////// FUNCTION TO MAKE POPUP FOLLOW THE MOUSE //////////////////////////

function followmouse(e){

// make popup visible (used first time function is executed)
document.getElementById(img_id).style.visibility = 'visible';



// trova lo spazio del viewport visible
function getViewportSize()
{
 var size = [0, 0];

 if (typeof window.innerWidth != 'undefined')
 {
   size = [
       window.innerWidth,
       window.innerHeight
   ];
 }
 else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0)
 {
   size = [
       document.documentElement.clientWidth,
       document.documentElement.clientHeight
   ];
 }
 else
 {
   size = [
       document.getElementsByTagName('body')[0].clientWidth,
       document.getElementsByTagName('body')[0].clientHeight
   ];
 }

 return size;
}

var size = getViewportSize(); 




// get how much user has scrolled

function getScrollY() {
  var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
  } else if(document.body && document.body.scrollTop) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
  } else if (document.documentElement && document.documentElement.scrollTop) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }
  return scrOfY;
}

var scrollY = getScrollY();




// *****************************************************************************
                          // ok, most importan functionality
													// is right here: if it doesn't fit,
													// do stuff
// *****************************************************************************

// get cursor position
var cursorX
var cursorY
if (typeof e != "undefined"){
cursorX = e.pageX
cursorY = e.pageY
}
else if (typeof window.event !="undefined"){
cursorX = truebody().scrollLeft+event.clientX
cursorY = truebody().scrollTop+event.clientY
}

// initialize popup position
// 1) popup follows the cursor, so it has same coordinates (plus offset)
// 2) if popup doesn't fit we change the coordinates (evaluation etc. done below)



// fai roba se popup va oltre finestra in orizzontale
// attenzione: scroll orizzontale non considerato (dato che verrebbe cmq considerato un errore nel design
if (cursorX + popup_width + offsetfrommouse[0] > size[0]) {
var popupX = cursorX - popup_width - offsetfrommouse[0]
}

else var popupX = cursorX + offsetfrommouse[0];




function get_popupY() {

// fai roba se immagine va oltre finestra in verticale
if (cursorY + popup_height + offsetfrommouse[1] > (size[1] + scrollY)) {

    // if popup has room on top to be flipped
    if ((cursorY - scrollY) /* distance between cursor and top of viewport */ > (popup_height + offsetfrommouse[1])) {
   var  popupY = cursorY - popup_height - offsetfrommouse[1]
    	}
			
		// if popup doesn't have room on top
		else if ((cursorY - scrollY) /* distance between cursor and top of viewport */ < (popup_height + offsetfrommouse[1])) {
		var popupY = scrollY + size[1] - popup_height;
	    }
	
}

else var popupY = cursorY + offsetfrommouse[1];

return popupY;
}

var popupY = get_popupY();

// position popup
gettrailobj().display=""
gettrailobj().left = popupX + "px"
gettrailobj().top = popupY + "px"
}

/*
alert ("popupX: " + popupX + ", popupY: " + popupY);
}
*/

document.onmousemove=followmouse


if (displayduration>0)
setTimeout("hidetrail()", displayduration*1000)


} // function 




function HideTrailImage(img_id) {
document.getElementById(img_id).style.visibility = 'hidden';// nascondi immagine
document.onmousemove="" // smetti di fare cose al movimento del mouse
}



