var cursor = {x:0, y:0};
var div = null;
var body_page_height = 0;
var body_page_width = 0;
var opacityTo = null;

var ie = document.all;
var ns = document.layers;
var fi = document.getElementById && !document.all;



function fDomOffset(oObj,sProp)
{ 
// oObj : [Object] - Objet dont on désire le positionnement 
// sProp : [String] - Propriété désirée : offsetLeft - offsetTop 
	var iVal = 0; 
// On boucle tant que l'on n'est pas à la racine du document 
	while (oObj && oObj.tagName != 'BODY')
   { 
		eval('iVal += oObj.' + sProp + ';');
		oObj = oObj.offsetParent; 
	} 
	return iVal; 
} 

function fSwapSelect(mydiv) 
{ 
 	oObj = mydiv;
 	
 	Top_Element = Left_Element = Largeur_Element = Hauteur_Element  = 0;
	if(oObj != null) 
	{
		Top_Element  = fDomOffset(oObj, 'offsetTop'); 
 		Left_Element  = fDomOffset(oObj, 'offsetLeft'); 
 		Largeur_Element  = oObj.offsetWidth; 
 		Hauteur_Element  = oObj.offsetHeight; 	
	}
 	
 	oSelects = document.getElementsByTagName('SELECT'); 
 	if(oSelects.length > 0) 
	{ 
  		for(i = 0; i < oSelects.length; i++) 
		{ 
	   	oSlt = oSelects[i]; 
	   	Top_Select = fDomOffset(oSlt, 'offsetTop'); 
	   	Left_Select = fDomOffset(oSlt, 'offsetLeft'); 
	   	Largeur_Select = oSlt.offsetWidth; 
	   	Hauteur_Select = oSlt.offsetHeight; 
	   	isLeft = false; 
	   	if((Left_Element > (Left_Select - Largeur_Element)) && (Left_Element < (Left_Select + Largeur_Select))) 
			{ 
	    		isLeft = true; 
	   	} 
	   	isTop = false; 
	   	if ((Top_Element > (Top_Select - Hauteur_Element)) && (Top_Element < (Top_Select + Hauteur_Select)))
			{ 
	    		isTop = true; 
	   	} 
	   	if (isLeft && isTop) 
			{ 
	    		sVis = (oObj.style.visibility == 'hidden') ? 'visible' : 'hidden'; 
	    		if (oSlt.style.visibility != sVis)
				{
					oSlt.style.visibility = sVis;
				} 
	   	} 
			else 
			{ 
	    		if (oSlt.style.visibility != 'visible') 
				{
				 	oSlt.style.visibility = 'visible';
				} 
	   	} 
  		} 
 	} 
} 

function position(e)
{		
	if(!ie)
	{
		document.captureEvents(Event.MOUSEMOVE);
	}
   	
   if(ie)
	{
		cursor.x = event.clientX;
		cursor.y = event.clientY;
	}
	else
	{
		cursor.x = e.pageX;
		cursor.y = e.pageY;
	}

	var gap_y = 20;
	var gap_x = 20;
	
	var swidth = $(div).getWidth();
	var sheight = $(div).getHeight();
	
	
	if(cursor.x + gap_x + swidth <= body_page_width)
		div.style.left = (cursor.x + gap_x)  + 'px';
	else if(cursor.x - gap_x - swidth > 0)
		div.style.left = (cursor.x - gap_x - swidth) + 'px';
	else
		div.style.left = (cursor.x - gap_x - swidth / 2)  + 'px';	
	
	if(cursor.y - gap_y - sheight > 0)
		div.style.top = (cursor.y - gap_y - sheight + document.documentElement.scrollTop) + 'px';	
	else if(cursor.y + gap_y + sheight <= body_page_height)
		div.style.top = (cursor.y + gap_y) + 'px'; 
	else
		div.style.top = (cursor.y - gap_y - sheight / 2) + 'px';	
		
	fSwapSelect(div);
}

function setopacity(opacity)
{	
	/*if(opacity > 20)
	{
		opacity = opacity - 2;
		if(ie)
			document.body.style.filter = 'alpha(opacity=' + opacity + ')';
		else
		{	
			document.body.style.opacity = opacity / 100;
			div.style.opacity = 1;
		}		
		
		opacityTo = setTimeout('setopacity(' + opacity + ')', 10);	
	}	*/	
}

function show_image(id)
{	
	div = document.getElementById(id);	
	document.onmousemove = position;
	div.style.visibility = 'hidden';
	div.style.display = 'block';
	document.onmousemove = position;
	divTo = setTimeout('set_div()', 500);			
}

function set_div()
{	
	if((body_page_height > 0) && (body_page_width > 0) && (div != null))
	{		
		///
		//if(ie)
		//	setopacity(100);
		///
		div.style.visibility = 'visible';
	}						
}

function hide_image(id)
{

	div = document.getElementById(id);
   div.style.display = 'none';
   clearTimeout(divTo);
   /////
   if(opacityTo != null)
   {
		clearTimeout(opacityTo);
		opacityTo = null;	
	}	

   /*if(ie)
   	document.body.style.filter = 'alpha(opacity=100)';
   else
   	document.body.style.opacity = 1;	*/
   ////
	document.onmousemove = null;
   div = null;
   fSwapSelect(null);
}

function getwindowsize()
{
	body_page_height = document.documentElement.scrollHeight;
  	body_page_width = document.documentElement.scrollWidth;
}

//Fonction ajoutée au window.onload
AddWindowOnLoadFunction(getwindowsize);

window.onresize = function()
{
  getwindowsize();
}