// JavaScript Document
/*Codice popup istanziato ritardato con 4 parametri: (1 se deve entrare, 2  uscire) , quanto aspettare, in quanto tempo apparire,  dove deve poppare, cosa deve poppare*/
var myTimer;

function ballooner(inOut,timeWait,timeFade,wherePop,whatPop){
	

/*metto dove e cosa in due variabili */
if (wherePop.style == null) { var	where=document.getElementById(wherePop); }

else { var where = wherePop;}

if (whatPop.style == null) {
	var	what=document.getElementById(whatPop);
}

else { 
  	var span_list = whatPop.getElementsByTagName("span");
		var what = span_list[0];
}


var xx;
var yy;
var obscurity;

		
/* Calcola la posizione dell'elemento e lo fa apparire*/
var waitIn= function (){	
		
	if (what.style.zIndex == 0){
		obscurity=0;
		/*calcolo quanto è alto il cosa e lo centro rispetto al dove*/
		var whatHeight=what.offsetHeight;
		var whereHeight=where.offsetHeight;
		var centerWhat = what.offsetWidth;
		centerWhat = Math.round( centerWhat / 2);
		var centerWhere = where.offsetWidth;
		centerWhere = Math.round( centerWhere / 2);
		xx=getX(where);
		yy=getY(where);
		xx= xx-centerWhat+centerWhere;
		/*yy= yy + whereHeight;*/
		yy= yy -whatHeight;
		/*aspetto il timeWait per far partire il comparire*/
		myTimer=setTimeout(fadeIn, timeWait);  
	}
}


var fadeIn= function () {
	
	if(document.all) /* se sto usando internet explorer*/
       what.style.filter = "alpha(opacity=" + 0+ ")";
    else
       what.style.opacity = 0;
	what.style.left= (xx)+'px' ;	
	what.style.top= (yy)+'px' ;
	what.style.zIndex= myTimer;
	var speed = Math.round(timeFade/ 100);
    var frame = 0;
    for(fade = 0; fade < 100; fade++) { 
      setTimeout(setOpacity, (speed * frame) );
      frame++;
    }
}

	
/*Fa scomparire l'elemento*/
var waitOut= function(){ 
	clearTimeout(myTimer);
	if (what.style.zIndex !=0){
	myTimer=setTimeout(delayedOut, timeWait);				
	}
}
	
	
var delayedOut= function(){
	fadeOut();	
	setTimeout(hide, timeFade);
}


var fadeOut =function() { 
    obscurity=100;
    var speed = Math.round(timeFade/ 100);
    var frame = 0;
    for(fade = 100; fade > 0; fade--) { 
      setTimeout(setOpacity, (speed * frame));
      frame++;
    }
		
}


/* sisposta l'elemento fuori dalla schermo*/
var hide= function (){
	what.style.left = (-5000)+"px";
what.style.zIndex=0;
}	


/* setta l' opacità dell'elemento */
var setOpacity= function () {
	if (inOut =="1"){	  obscurity++;	  }
	else{  obscurity--;	  }
	if(document.all) /* se sto usando internet explorer*/
       what.style.filter = "alpha(opacity=" + obscurity + ")";
    else
       what.style.opacity = obscurity / 100;
}
  
  
/*Calcola la posizione di un elemento*/
var getY = function ( oElement ){
	var iReturnValue = 0;
    while( oElement != null ) {
      iReturnValue += oElement.offsetTop;
      oElement = oElement.offsetParent;
    }
    return iReturnValue;
}


var getX= function ( oElement ){
	var iReturnValue = 0;
    while( oElement != null ) {
      iReturnValue += oElement.offsetLeft;
      oElement = oElement.offsetParent;
    }
    return iReturnValue;
}


/* controllo se deve entrare o uscire*/
if (inOut =="1"){
	waitIn();
	}
	else if (inOut =="3")
	{
		
		clearTimeout(myTimer);
	}
	else
	{
	waitOut();
	}
}