var plot_grau_ver=110/220;     			 // Relação de pixel por graus: Tamanho do mapa: 110  range: -110 0 +100 (220)
var plot_grau_hor=110/220;    			 // Relação de pixel por graus: Tamanho do mapa: 110  range: -110 0 +100 (220)
var qty_plots                 			 // Quantidade de plots válidos recebidos de track_efemerides.php  
var plot_real=0            			 // Quantidade real de plots na tela
window['larguraPonto']=2;  			 // Largura do plot da órbita

var flag_aos=0;			     		 // flag que indica que achou a próxima passagem (AOS)
var flag_los=0;			       		 // flag que indica que achou o final da passagem (LOS)
var max_elev=1;					 // default da máxima elevação

var flag_aos_2=0;			     	 // flag que indica que achou a próxima passagem (AOS_2)
var flag_los_2=0;			       	 // flag que indica que achou o final da passagem (LOS_2)
var max_elev_2=1;				 // default da máxima elevação_2
var max_elev_az_2;
var range_max_elev_2;

var min_utc_aos;
var hora_utc_aos;
var min_utc_los;
var hora_utc_los;
var recalc_aos=0;
var tempo_mensagem_aos;

var $tabela_efemerides="";
var $tabela_segundos="";

var $epoch;
var $test=0;
var $ano_efem;
var $mes_efem;
var $dia_efem;
var $pi=3.14159265358979;
var $PI=3.14159265358979;


var $LOC_TXT=$id_local;
var $LA=$latitude_local;
var $LO=$longitude_local;
var $HT=$altitude_local;

var $YM;
var $MN;
var $M2=0;                      // Usado em SATVEC

var $ALON=10;                   // Não usado
var $ALAT=10;                   // Não usado


function mouseOver(id)
{
document.getElementById(id).style.display="block"; 
}
function mouseOut(id)
{
document.getElementById(id).style.display="none"; 
}


function toggle(id){
//      document.getElementById(ida).style.display="block"; 
var target = document.getElementById(id); 
 	if (target) with (target.style)
 	{ 
		if (display!= "none")
		 display = "none";
		 else display = "" 
		 
	}
}


function RAD ($graus) {		// Tranforma grau em radiano
return $graus*$pi/180;
}

function DEG ($radianos) {	// Tranforma radiano em graus
return $radianos*180/$pi;
}
         
function FNatn ($Y,$X) {
if ($Y == 0){   $sinal=0;  }
if ($Y <= 0){  $sinal=-1; }
if ($Y > 0 ){  $sinal=1;  }

if ($X != 0){
 $A=Math.atan($Y/$X);
 }else{
 $A=$pi/2*$sinal;
}

if ($X < 0){  $A=$A+$pi; }
if ($A < 0){  $A=$A+2*$pi; }

return $A;  

// $Y=  0.31513211805032 
// $X=  -0.11788275270316 
// $A=  1.9287549722323
}


function FNrn($X){
return $X+0.5;
}

function FNdate ($D) {
// ---------- Convert day-number to date; valid 1900 Mar 01 - 2100 Feb 28
// $D=24
// Saida= "24 set 2011"

$meses=" JanFevMarAbrMaiJunJulAgoSetOutNovDez";
$d_semana=" DomSegTerQuaQuiSexSab";
$D=$D+428; $DW=($D+5)%7; 
$Y=Math.floor(($D-122.1)/$YM); $D=$D-Math.floor($Y*$YM);
$MN=Math.floor($D/30.61); $D=$D-Math.floor($MN*30.6);
$MN=$MN-1;  
if ($MN>12){  $MN=$MN-12;  $Y=$Y+1;   }

//$ano_efem="$Y"; 
//$mes_efem="$MN";
//$dia_efem="$D";

$ano_efem=$Y; 
$mes_efem=$MN;
$dia_efem=$D;
return $d_semana.substr(3*$DW+1,3) + ", " +$D + " " + $meses.substr(3*$MN-2,3) + " " +$Y;
}


//Convert date to day-number
function FNday ($Y,$M,$D) {
 if ($M <=2){     $Y=$Y-1;$M=$M+12;   }
 return Math.floor($Y*$YM) + Math.floor(($M+1)*30.6) + $D-428;
}

function hora_local(hora,minuto){
return (parseInt($HR_TXT) + $fuso_local) + ":" + $MIN;
}


function proc_init(){
// ------------------- PROC_INIT ----------------------- // ----- Abre as constantes e dados do programa    

    $YE=$YE_back;
    $TE=$TE_back;
    $IN=$IN_back;
    $RA=$RA_back;
    $EC=$EC_back;
    $WP=$WP_back;
    $MA=$MA_back;
    $MM=$MM_back;
    $RV=$RV_back;

// ------------------------------ Observer's location + North, + East, ASL(m)
$LOC_TXT=$id_local;
$LA=$latitude_local;
$LO=$longitude_local;
$HT=$altitude_local;

$LA = RAD($LA); $LO = RAD($LO); $HT = $HT/1000;
$CL = Math.cos($LA); $SL = Math.sin($LA); $CO = Math.cos($LO); $SO = Math.sin($LO);
$RE = 6378.137; $FL = 1/298.257224;                             // ------------   WGS-84 Earth ellipsoid
$RP = $RE*(1-$FL); $XX = $RE*$RE; $ZZ = $RP*$RP;
$D  = Math.sqrt ($XX*$CL*$CL + $ZZ*$SL*$SL);
$Rx = $XX/$D + $HT; $Rz = $ZZ/$D + $HT;

// ------------------------------ Observer's unit vectors UP EAST and NORTH in GEOCENTRIC coords.
$Ux =$CL*$CO; $Ex =-$SO; $Nx =-$SL*$CO;
$Uy =$CL*$SO; $Ey = $CO; $Ny =-$SL*$SO;
$Uz =$SL; $Ez=0; $Nz = $CL;
 
// ------------------------------ Observer's XYZ coords at Earth's surface
$Ox = $Rx*$Ux; $Oy = $Rx*$Uy; $Oz = $Rz*$Uz;
// ------------------------------ Convert angles to radians etc.

$RA = RAD($RA); $IN = RAD($IN); $WP = RAD($WP);
$MA = RAD($MA); $MM = $MM*2*$PI; $M2 = $M2*2*$PI;
$YM = 365.25;                                                  // ------------- Mean Year,     days
$YT = 365.2421874;                                             // ------------- Tropical year, days
$WW = 2*$PI/$YT;                                               // ------------- Earth's rotation rate, rads/whole day
$WE = 2*$PI + $WW;                                             // ------------- ditto   radians/day
$W0 = $WE/86400;                                               // ------------- ditto   radians/sec
$VOx=-$Oy*$W0; $VOy=$Ox*$W0;                                    // ------------- Observer's velocity, GEOCENTRIC coords. (VOz=0)


// ------------------------------ Convert satellite Epoch to Day No. and Fraction of day
$DE = FNday($YE,1,0)+Math.floor($TE); $TE = $TE-Math.floor($TE);
// ------------------------------ Average Precession rates
$GM = 3.986E5;                                                 // ------------- Earth's Gravitational constant km^3/s^2
$J2 = 1.08263E-3;                                              // ------------- 2nd Zonal coeff, Earth's Gravity Field
$N0 = $MM/86400;                                               // ------------- Mean motion rad/s
$A0 = Math.pow (($GM/$N0/$N0), (1/3));                         // ------------- Semi major axis km
$B0 = $A0*Math.sqrt (1-$EC*$EC);                               // ------------- Semi minor axis km
                                                               //$semi_minor_axis = $semi_major_axis * sqrt(1-pow($eccentricity,2));
$eixo_semi_maior=$A0;
$eixo_semi_menor=$B0;
$SI = Math.sin($IN); 
$CI = Math.cos($IN);
$PC = $RE*$A0/($B0*$B0); $PC = 1.5*$J2*$PC*$PC*$MM;            // ------------- Precession const, rad/Day
$QD = -$PC*$CI;                                                // ------------- Node precession rate, rad/day
$WD =  $PC*(5*$CI*$CI-1)/2;                                    // ------------- Perigee precession rate, rad/day
$DC = -2*$M2/$MM/3;                                            // ------------- Drag coeff. (Angular momentum rate)/(Ang mom)  s^-1

// ------------------------------ Sidereal and Solar data. NEVER needs changing. Valid to year ~2015
$YG = 2000; $G0 = 98.9821;                                      // -------------- REM GHAA, Year YG, Jan 0.0
$MAS0 = 356.0507; $MASD = 0.98560028;                           // -------------- MA Sun and rate, deg, deg/day
$INS = RAD(23.4393); $CNS = Math.cos($INS); $SNS = Math.sin($INS);        // -------------- Sun's inclination
$EQC1=0.03342; $EQC2=0.00035;                                   // -------------- Sun's Equation of centre terms
 
// ------------------------------  Bring Sun data to Satellite Epoch
$TEG  = ($DE-FNday($YG,1,0)) + $TE;                             // -------------- Elapsed Time: Epoch - YG
$GHAE = RAD($G0) + $TEG*$WE;                                    // -------------- GHA Aries, epoch
$MRSE = RAD($G0) + $TEG*$WW + $PI;                              // -------------- Mean RA Sun at Sat epoch
$MASE = RAD($MAS0 + $MASD*$TEG);                                // -------------- Mean MA Sun  
 
// ------------------------------ Antenna unit vector in orbit plane coordinates.
$CO=Math.cos(RAD($ALON)); $SO=Math.sin(RAD($ALON));
$CL=Math.cos(RAD($ALAT)); $SL=Math.sin(RAD($ALAT));
$ax = -$CL*$CO; $ay = -$CL*$SO; $az = -$SL;
 
// ------------------------------   Miscellaneous
$OLDRN=-99999;
// --------------------- FIM PROC INIT ------------------------
return true; 
}
