
// Variabili Globali
xMousePos = 0; // Horizontal position of the mouse on the screen
yMousePos = 0; // Vertical position of the mouse on the screen
xMousePosMax = 0; // Width of the page
yMousePosMax = 0; // Height of the page

xMousePos_tmp = 0; // Horizontal position of the mouse on the screen
yMousePos_tmp = 0; // Vertical position of the mouse on the screen

//Varibili di Attivazione 
var outScope=false;
var ToolTipUp = '';
var activeToolTip = '';
var hideId=-1;

/* Configurazioni del ToolTip */
var timeToClose=1500;
var x_offset_tooltip = 5;
var y_offset_tooltip = -3;
var msg_loading='Loading content - please wait';
var msg_error='Page not found!';


var ajax_tooltip_MSIE = false;
//Controllo se è Explorer
if(navigator.userAgent.indexOf('MSIE')>=0)ajax_tooltip_MSIE=true;

var ajax_tooltipObj_iframe=null;

/**
<summary>
Cerca e carica uno specifico tooltip
</summary>
<param name="nameId">Nome della pagina html da caricare nel ToolTip</param>
<param name="inputObj">Oggetto chiamante</param>
<param name="e">Evento</param>
*/

//showtooltip('/it_IT/overlayer/tooltip-prod-gas.html',this,event);
function showtooltip(nameId ,inputObj, e)
{
	
	//Costruisco il path da richiamare
	var externalFile=nameId;

	//Setto semaforo
	if(hideId!=-1)
	{window.clearTimeout(hideId);}
	outScope=false;
	

	//Controllo che la pagina richiamata non sia la stessa
	if(externalFile==activeToolTip)	return;
	
	//Setto il ToolTip Attivo	               
	activeToolTip=externalFile;


	//Chiamo la funzione Principale
	ajax_showTooltip(externalFile,inputObj, e);
	return false;
}


if (document.layers) { // Netscape
    document.captureEvents(Event.MOUSEMOVE);
    document.onmousemove = captureMousePosition;
}else if (document.all) { // Internet Explorer
    document.onmousemove = captureMousePosition;
}else if (document.getElementById){ // Netcsape 6
    document.onmousemove = captureMousePosition;
}


/**
<summary>
Funzione per captturae la posizione del mouse all'interno dell pagina
</summary>
<param name="e">Evento</param>
*/
function captureMousePosition(e) {
    if (document.layers) {
        // When the page scrolls in Netscape, the event's mouse position
        // reflects the absolute position on the screen. innerHight/Width
        // is the position from the top/left of the screen that the user is
        // looking at. pageX/YOffset is the amount that the user has
        // scrolled into the page. So the values will be in relation to
        // each other as the total offsets into the page, no matter if
        // the user has scrolled or not.
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    } else if (document.all) {
        // When the page scrolls in IE, the event's mouse position
        // reflects the position from the top/left of the screen the
        // user is looking at. scrollLeft/Top is the amount the user
        // has scrolled into the page. clientWidth/Height is the height/
        // width of the current page the user is looking at. So, to be
        // consistent with Netscape (above), add the scroll offsets to
        // both so we end up with an absolute value on the page, no
        // matter if the user has scrolled or not.
        xMousePos = window.event.x+document.body.scrollLeft;
        yMousePos = window.event.y+document.body.scrollTop;
        xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
        yMousePosMax = document.body.clientHeight+document.body.scrollTop;
    } else if (document.getElementById) {
        // Netscape 6 behaves the same as Netscape 4 in this regard
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    }
}

/**
<summary>
Funzione che costruisce il Div ToolTip e richiama l'ajax
</summary>
<param name="externalFile">Url della pagina da richiamare</param>
<param name="inputObj">Chiamante</param>
<param name="e">Evento</param>
*/
function ajax_showTooltip(externalFile,inputObj, e)
{
	
	captureMousePosition(e);

	xMousePos_tmp = xMousePos; // Horizontal position of the mouse on the screen
	yMousePos_tmp = yMousePos; // Vertical position of the mouse on the screen
	
	//Costruzione del div
	ajax_tooltipObj = document.getElementById('ajax_tooltipObj');
	
	if(ajax_tooltipObj==null)
	{ajax_tooltipObj = document.createElement('DIV');}
	
	ajax_tooltipObj.style.position = 'absolute';
	ajax_tooltipObj.id = 'ajax_tooltipObj';
	
	ajax_tooltipObj.innerHTML="<div id=\"overlayer\"><div class=\"header\"></div><div class=\"corpo\">"+msg_loading+"<br></div><div class=\"footer\"></div></div>";
	ajax_tooltipObj.style.display='block';
	
	ajax_tooltipObj.style.left = '50px';
	ajax_tooltipObj.style.top = '50px';
	
	
	document.body.appendChild(ajax_tooltipObj);

  checkForIFrame('ajax_tooltipObj');

	//adjustZIndex(1000000);
	
	//Decommentare questa riga alla fine
	//ajax_tooltipObj.style.border= '2px solid #317082' ;

	//Costruzione del div

	
	//Sposto il Div sopra il chiamante
	moveTool(ajax_tooltipObj,inputObj);
	

	//Richiamo la funzione AJAX
	loadAjaxContent(externalFile,'ajax_tooltipObj',inputObj);


}

/**
<summary>
Funzione che sopsta il div sopra il Mouse
</summary>
<param name="ajax_tooltipObj">Oggetto da spostare</param>
*/
function moveTool(ajax_tooltipObj,inputObj)
{
	var st = 0;

	//Se sono in IE calcolo lo scroll
	if(ajax_tooltip_MSIE)
	{st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);}

	var tooltipPosition = callPosition(inputObj);

	var leftPos = tooltipPosition[0];
	//alert('quindi ottengo la posizione di sinistra '+leftPos);
	var widthPos = tooltipPosition[1];
	//alert('la larghezza '+widthPos);
	var topPos = tooltipPosition[2];
	//alert('la posizione alta '+topPos);
	var heightPos = tooltipPosition[3];
	//alert('e la altezza '+heightPos);
	
	//alert('di conseguenza la posizione da sinistra del tooltip sarà '+(leftPos + (widthPos/2))+'px, mentre quella alta sarà '+(topPos - heightPos - 300)+'px');
	if(navigator.appName == 'Microsoft Internet Explorer'){
		//ajax_tooltipObj.style.left = (xMousePos_tmp + widthPos) + 'px';
		ajax_tooltipObj.style.left = (leftPos+(widthPos/2)) + 'px';
		//ajax_tooltipObj.style.top = (yMousePos_tmp + 150) + 'px';
		ajax_tooltipObj.style.top = (yMousePos_tmp - heightPos) + 'px';
	}else{
		ajax_tooltipObj.style.left = (leftPos+(widthPos/2)) + 'px';
		ajax_tooltipObj.style.top = (yMousePos_tmp - heightPos - 285) + 'px';
	}
	
	if(ajax_tooltipObj_iframe!=null)
	{
		ajax_tooltipObj_iframe.style.width = ajaxTooltip_getWidthPos(ajax_tooltipObj) - x_offset_tooltip + 6 + 'px';
		//ajax_tooltipObj_iframe.style.border= '1px solid #117012' ;
		ajax_tooltipObj_iframe.style.height = ajaxTooltip_getHeightPos(ajax_tooltipObj) - 20 + 'px';
		//ajax_tooltipObj_iframe.style.zIndex = ajax_tooltipObj.style.zIndex - 10;
		
		ajax_tooltipObj_iframe.style.left  = -1;
		ajax_tooltipObj_iframe.style.top = -1;
		ajax_tooltipObj_iframe.style.diplay='block';
	}
}


/**
<summary>
Funzione che setta z_index
</summary>
<param name="z_Index">Indice Z</param>
*/
function toBack()
{
	ajax_tooltipObj.style.zIndex = -1000000;

	if(ajax_tooltipObj_iframe!=null)
	{
		ajax_tooltipObj_iframe.style.zIndex = ajax_tooltipObj.style.zIndex - 10;
	}
}

/**
<summary>
Funzione che setta z_index
</summary>
<param name="z_Index">Indice Z</param>
*/
function toFront()
{
	ajax_tooltipObj.style.zIndex = 1000000;
}

/**
<summary>
Calcola l'altezza di un oggetto
</summary>
<param name="inputObj">Oggetto</param>
*/
function ajaxTooltip_getHeightPos(inputObj)
{		
  var returnValue = inputObj.offsetHeight;
 
  while((inputObj = inputObj.offsetParent) != null)
  {
  	if((inputObj.tagName!='HTML')&&(inputObj.tagName!='BODY'))
  	{
  		returnValue += inputObj.offsetHeight;
  	}
  }
  return returnValue;
}


/**
<summary>
Calcola la largezza di un oggetto
</summary>
<param name="inputObj">Oggetto</param>
*/
function ajaxTooltip_getWidthPos(inputObj)
{		
  var returnValue = inputObj.offsetWidth;
 
  while((inputObj = inputObj.offsetParent) != null)
  {
  	if((inputObj.tagName!='HTML')&&(inputObj.tagName!='BODY'))
  	{
  		returnValue += inputObj.offsetWidth;
  	}
  }
  return returnValue;
}



/**
<summary>
Funzione che controlla se devo creare un iframe
</summary>
*/
function checkForIFrame(nameDiv)
{
	if(ajax_tooltip_MSIE)
	{	/* Create iframe object for MSIE in order to make the tooltip cover select boxes */
		ajax_tooltipObj = document.getElementById(nameDiv);
		ajax_tooltipObj_iframe = document.createElement('<IFRAME frameborder="0">');
		ajax_tooltipObj_iframe.style.position = 'absolute';
		ajax_tooltipObj_iframe.border='0';
		ajax_tooltipObj_iframe.frameborder= '0' ;
		ajax_tooltipObj_iframe.style.backgroundColor='#FFF';
		ajax_tooltipObj_iframe.src = 'about:blank';
		ajax_tooltipObj.appendChild(ajax_tooltipObj_iframe);
		ajax_tooltipObj_iframe.style.left = '0px';
		ajax_tooltipObj_iframe.style.top = '0px';

		ajax_tooltipObj_iframe.style.width = 0 + 'px';
		ajax_tooltipObj_iframe.style.height = 0 + 'px';
		ajax_tooltipObj_iframe.style.zIndex = ajax_tooltipObj.style.zIndex-10;
	
	}		
}



/**
<summary>
Funzione che chiama il timer per la chiusura del ToolTip
</summary>
*/
function hideTooltip()
{
	outScope=true;
	hideId=window.setTimeout('ajax_hideTooltip()',timeToClose);
}

/**
<summary>
Funzione che elimina il ToolTip
</summary>
*/
function ajax_hideTooltip()
{
	if(outScope)
	{
		activeToolTip='';
		document.body.removeChild(ajax_tooltipObj);	
	}
}


/**
*
*		Funzioni AJAX
*
**/

/**
<summary>
Funzione che copia la response in un div
</summary>
<param name="url">The url to make the request to get the response data.</param>
<param name="nameDiv">The id of the div to set the innerHTML for.</param>
*/
function loadAjaxContent(url,nameDiv,inputObj)
{	
	
	try
	{
	xmlHttp=GetXmlHttpObject();
	



	xmlHttp.onreadystatechange=function()
  {
      if(xmlHttp.readyState==4)
      {
      	
      	//Costruzione del div
				ajax_tooltipObj = document.getElementById(nameDiv);
				
				
      	
      		if(xmlHttp.status == 404)
      		{
      			toBack();
				ajax_tooltipObj.innerHTML="<div id=\"overlayer\"><div class=\"header\"></div><div class=\"corpo\">"+msg_error+"<br></div><div class=\"footer\"></div></div>";
				checkForIFrame(nameDiv);
				moveTool(document.getElementById(nameDiv),inputObj);
				toFront();
				//adjustZIndex(1000000);
      		}
      		else
      		{
      			//ajax_tooltipObj.style.zIndex='-100';
						//adjustZIndex(-1000000);
						toBack();
						
		      	ajax_tooltipObj.innerHTML=xmlHttp.responseText;
		      	//document.getElementById(nameDiv).innerHTML="<div id=\"overlayer\"><div class=\"header\"></div><div class=\"corpo\">"+ xmlHttp.responseText  + "</div><div class=\"footer\"></div></div>";
	 					checkForIFrame(nameDiv);
		      	//Sposto il ToolTip ricalcolando la sua nuova Altezza
		      	moveTool(document.getElementById(nameDiv),inputObj);
		      	//ajax_tooltipObj.style.zIndex='1000000';
		      	//adjustZIndex(1000000);
		      	toFront();
	      	}
	      	
	      	moveTool(document.getElementById(nameDiv),inputObj);
	      	
      }  
  };	

	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	}
	catch(e3)
	{
		document.getElementById(nameDiv).innerHTML="<div id=\"overlayer\"><div class=\"header\"></div><div class=\"corpo\">"+msg_error+"<br></div><div class=\"footer\"></div></div>";
		moveTool(document.getElementById(nameDiv),inputObj);
	}
	
}


/**
<summary>
Browser Compatability function.
Returns the correct XMLHttpRequest 
  depending on the current browser.
</summary>
*/
function GetXmlHttpObject()
{ 
	var objXMLHttp=null;
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		alert("Your browser does not support AJAX!");
	}
	
	return objXMLHttp;
}

/**
*
*		FINE:Funzioni AJAX
*
**/

function callPosition(obj) {
	//alert('sono entrato nella funzione per il calcolo della posizione');
	var width = height = 0;
	width = obj.offsetWidth;
	//alert('la width oggetto corrisponde a '+width);
	height = obj.offsetHeight;
	//alert('la height oggetto corrisponde a '+height);
	
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	//alert('la curleft oggetto corrisponde a '+curleft);
	//alert('il curtop oggetto corrisponde a '+curtop);
	return [curleft,width,curtop,height];
}
