/* * * * * * * * * *    SUNRISET SCRIPT    * * * * * * * * * *\
*                                                             *
*     Computes Sun rise/set times, start/end of twilight.     *
*                                                             *
*        Originally written as DAYLEN.C, 1989-08-16           *
*            Modified to SUNRISET.C, 1992-12-01               *
*              (c) Paul Schlyter, 1989, 1992                  *
*                                                             *
*  Released to the public domain by Paul Schlyter, Dec. 1992  *
*                                                             *
*  --  Modified by Bo Johansson  1995-05-12 , 1995-10-14  --  *
*  --  Ported to JavaScript by Bo Johansson  1998-05-22   --  *
*  --  Very minor revision by Bo Johansson  1999-10-25    --  *
*  --   Very minor Y2K fix by Bo Johansson  2000-01-03    --  *
*  --  Minor Opera 4 bug fix by Bo Johansson  2000-10-14  --  *
*                                                             *
*  --  http://w1.545.telia.com/%7Eu54504162/index_e.htm   --  *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

// Global variables:
  
var twAngle = -6.0;    // For civil twilight, set to -12.0 for
                       // nautical, and -18.0 for astr. twilight

var srAngle = -35.0/60.0;    // For sunrise/sunset

var sRiseT;      // For sunrise/sunset times
var sSetT;
var srStatus;

var twStartT;    // For twilight times
var twEndT;
var twStatus;

var sDIST;       // Solar distance, astronomical units
var sRA;         // Sun's Right Ascension
var sDEC;        // Sun's declination

var sLON;        // True solar longitude


//-------------------------------------------------------------
// A function to compute the number of days elapsed since
// 2000 Jan 0.0  (which is equal to 1999 Dec 31, 0h UT)
//-------------------------------------------------------------
function dayDiff2000(y, m, d)
{
  return (367.0 * y -((7 *(y +((m + 9) / 12))) / 4) +
          ((275 * m)/9) + d - 730530.0);
}


//-------------------------------------------------------------
// Some conversion factors between radians and degrees

var RADEG = 180.0 / Math.PI;
var DEGRAD = Math.PI / 180.0;


//-------------------------------------------------------------
// The trigonometric functions in degrees

function sind(x) { return Math.sin(x * DEGRAD); }
function cosd(x) { return Math.cos(x * DEGRAD); }
function tand(x) { return Math.tan(x * DEGRAD); }

function atand(x) { return (RADEG * Math.atan(x)); }
function asind(x) { return (RADEG * Math.asin(x)); }
function acosd(x) { return (RADEG * Math.acos(x)); }

//function atan2d(y,x) { return (RADEG*Math.atan2(y,x)); }

function atan2d(y,x)          //###  Hack to work in MSIE3 ###
{
  var at2 = (RADEG * Math.atan(y/x));

  if( x < 0 && y < 0)
    at2 -= 180;

  else if( x < 0 && y > 0)
    at2 += 180;

  return at2;
}

function sunRiseSet(year, month, day, lat, lon)
{
  return (sunTimes(year, month, day, lat, lon, srAngle, 1));
}

function civTwilight(year, month, day, lat, lon)
{
  return (sunTimes( year, month, day, lat, lon, twAngle, 0));
}

function sunTimes(year, month, day, lat, lon, altit, sUppLimb)
{
  var dayDiff;    // Days since 2000 Jan 0.0 (negative before)
  var sRadius;    // Sun's apparent radius
  var diuArc;     // Diurnal arc
  var sSouthT;    // Time when Sun is at south
  var locSidT;    // Local sidereal time

  var stCode = 0;     // Status code from function - usually 0

      /* Compute dayDiff of 12h local mean solar time */
      
  dayDiff = dayDiff2000(year, month, day) + 0.5 - lon/360.0;

      /* Compute local sideral time of this moment */

  locSidT = revolution( GMST0(dayDiff) + 180.0 + lon );


      /* Compute Sun's RA + Decl at this moment */

  sunRaDec( dayDiff );

      /* Compute time when Sun is at south - in hours UT */
      

  sSouthT = 12.0 - rev180(locSidT - sRA)/15.0;


      /* Compute the Sun's apparent radius, degrees */

  sRadius = 0.2666 / sDIST;


      /* Do correction to upper limb, if necessary */

  if ( sUppLimb != 0)            // For surise/sunset
    altit -= sRadius;


      /* Compute the diurnal arc that the Sun traverses */
      /* to reach the specified altitude altit:         */

    var cost = ( sind(altit) - sind(lat) * sind(sDEC) ) /
               ( cosd(lat) * cosd(sDEC) );

    if ( cost >= 1.0 )
    {
       stCode = -1;
       diuArc = 0.0;       // Sun always below altit
    }

    else if ( cost <= -1.0 )
    {
       stCode = 1;
       diuArc = 12.0;      // Sun always above altit
    }

    else
    {                             // *** Opera bug fix 2000-10-14
                                  // The diurnal arc, hours
       diuArc = acosd(cost)/15.0;
    }


      /* Compute rise and set times - in hours UT */

  if ( sUppLimb != 0)       // For sunrise/sunset
  {
    sRiseT = sSouthT - diuArc;

    if(sRiseT < 0)          // Sunrise day before
      sRiseT += 24;
      
    sSetT  = sSouthT + diuArc;

    if(sSetT > 24)          // Sunset next day
      sSetT -= 24;

    srStatus = stCode;
  }

  else                      // For twilight times
  {
    twStartT = sSouthT - diuArc;

    if(twStartT < 0)
      twStartT += 24;
      
    twEndT  = sSouthT + diuArc;

    if(twEndT > 24)
      twEndT -= 24;

    twStatus = stCode;
  }

} //================== sunTimes() =====================


function sunRaDec(dayDiff)
{
  var eclObl;   // Obliquity of ecliptic
                // (inclination of Earth's axis)
  var x;
  var y;
  var z;

      /* Compute Sun's ecliptical coordinates */

  sunPos( dayDiff );

      /* Compute ecliptic rectangular coordinates (z=0) */

  x = sDIST * cosd(sLON);

  y = sDIST * sind(sLON);

      /* Compute obliquity of ecliptic */
      /* (inclination of Earth's axis) */

//  eclObl = 23.4393 - 3.563E-7 * dayDiff;

  eclObl = 23.4393 - 3.563/10000000 * dayDiff;  // for Opera

      /* Convert to equatorial rectangular coordinates */
      /* - x is uchanged                               */

  z = y * sind(eclObl);
  y = y * cosd(eclObl);

      /* Convert to spherical coordinates */

  sRA = atan2d( y, x );
  sDEC = atan2d( z, Math.sqrt(x*x + y*y) );

} //================= sunRaDec() =======================

function sunPos(dayDiff)
{
  var M;       // Mean anomaly of the Sun
  var w;       // Mean longitude of perihelion
               // Note: Sun's mean longitude = M + w
  var e;       // Eccentricity of Earth's orbit
  var eAN;     // Eccentric anomaly
  var x;       // x, y coordinates in orbit
  var y;
  var v;       // True anomaly

      /* Compute mean elements */

  M = revolution( 356.0470 + 0.9856002585 * dayDiff );

//  w = 282.9404 + 4.70935E-5 * dayDiff;
//  e = 0.016709 - 1.151E-9 * dayDiff;

  w = 282.9404 + 4.70935/100000 * dayDiff;    // for Opera
  e = 0.016709 - 1.151/1000000000 * dayDiff;  // for Opera

      /* Compute true longitude and radius vector */

  eAN = M + e * RADEG * sind(M) * ( 1.0 + e * cosd(M) );

  x = cosd(eAN) - e;
  y = Math.sqrt( 1.0 - e*e ) * sind(eAN);

  sDIST = Math.sqrt( x*x + y*y );    // Solar distance

  v = atan2d( y, x );                // True anomaly

  sLON = v + w;                      // True solar longitude

  if ( sLON >= 360.0 )
    sLON -= 360.0;                   // Make it 0..360 degrees

} //=================== sunPos() =============================


var INV360 = 1.0 / 360.0;

function revolution( x )
{
  return (x - 360.0 * Math.floor( x * INV360 ));
}

function rev180( x )
{
  return ( x - 360.0 * Math.floor( x * INV360 + 0.5 ) );
}

function GMST0( dayDiff )
{
  var const1 = 180.0 + 356.0470 + 282.9404;

//  var const2 = 0.9856002585 + 4.70935E-5;

  var const2 = 0.9856002585 + 4.70935/100000;  // for Opera


  return ( revolution( const1 + const2 * dayDiff ) );
      
} //=================== GMST0() =========================

function toDayValues(name, lat, lon)
{
  toDay = new Date();

  var year = toDay.getYear();

  if (year < 1900)         // **** fixed 2000-01-03
    year += 1900;

  var month = (toDay.getMonth() + 1);

  var day =  toDay.getDate();

  sunRiseSet(year,month,day,lat,lon);

  civTwilight(year,month,day,lat,lon);

  showTable(name, lat, lon, year, month, day);
}


function showTable(name, lat, lon, year, month, day)
{
  var absLat = Math.abs(lat)
  var absLon = Math.abs(lon)

  var twsText = "&#346;wit: "
  var tweText = "Zmierzch: "

  var srText = "Wschód: "
  var ssText = "Zachód: "

  var twst_h = Math.floor(twStartT)
  var twst_m = Math.floor((twStartT - twst_h)*60)

  var sris_h = Math.floor(sRiseT)
  var sris_m = Math.floor((sRiseT - sris_h)*60)

  var sset_h = Math.floor(sSetT)
  var sset_m = Math.floor((sSetT - sset_h)*60)

  var twen_h = Math.floor(twEndT)
  var twen_m = Math.floor((twEndT - twen_h)*60)

  if(twStatus == 0)
  {
    document.write(" " + twsText)

    document.write(twst_h + 2 + ".")

    if(twst_m < 10)
      document.write("0")

    document.write(twst_m + "<br>")
  }

  if(srStatus == 0)
  {
    document.write(" " + srText)
  
    document.write(sris_h + 2 + ".")

    if(sris_m < 10)
      document.write("0")

    document.write(sris_m + "<br>")

    
    document.write(" " + ssText)

    document.write(sset_h + 2 + ".")

    if(sset_m < 10)
      document.write("0")

    document.write(sset_m + "<br>")
  }


  if(twStatus == 0)
  {
    document.write(" " + tweText)

    document.write(twen_h + 2 + ".")

    if(twen_m < 10)
      document.write("0")

    document.write(twen_m + "<br>")
    }

}
//* * * * * * * *   SUNRISET SCRIPT - END -  * * * * * * * * *

