var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
var latitude = 50.483;
var longitude =  -19.35 ;


function kalendarz_licz()
{
var dt_as_str, mm, dd, yy;
var yyy, mmm, a, b;


mm = eval(curr_month+1);
dd = eval(curr_date);
yy = eval(curr_year);

with (Math) {
var yyy=yy;
var mmm=mm;
if (mm < 3)
{
yyy = yy - 1;
mmm = mm + 12;
}
a = floor(yyy/100);
b = 2 - a + floor(a/4);

return floor(365.25*yyy) + floor(30.6001*(mmm+1)) + dd + 1720994.5 + b;
}
}







function rano_pop(h)
{
with (Math) {
var hours, minutes, ampm, result="";
hours = floor(h);
minutes = floor((h - hours) * 60 + 0.5);


if (minutes == 60)
{
hours += 1;
minutes = 0;
}



if (hours < 1) hours = 12;

if (hours < 10)
{result = result + "0" + floor(hours);}
else
{result = result + floor(hours);}
result = result + ":";

if (minutes < 10)
{result = result + "0" + minutes;}
else
{result = result + minutes;}
}
result = result



return result;
}
function sign_of(x)
{
return x == 0 ? 0 : (x > 0 ? 1 : -1);
}
function kalendarz_licz_zoned()
{
var dt_as_str, mm, dd, yy;
var yyy, mmm, a, b;



mm = eval(curr_month+1);
dd = eval(curr_date);
yy = eval(curr_year);


with (Math) {
var yyy=yy;
var mmm=mm;
if (mm < 3)
{
yyy = yy - 1;
mmm = mm + 12;
}
a = floor(yyy/100);
b = 2 - a + floor(a/4);

return floor(365.25*yyy) + floor(30.6001*(mmm+1)) + dd + 1720994.5 + b;
}
}


function oblicz_ksiezyc(latitude,longitude,tzone)
{
with (Math) {

var r_a = new Array(3);
var dec = new Array(3);
var dist = new Array(3);
var i, c0, x, y, p1, p2, r1, RAD, k1, b5, l5, h, z0, t, t0;
var s, l, m, f, d, n, g, v, u, w, a5, d5, r5, z1, c, z, m8, w8;
var a0, d0, p, f0, f1, f2, a, b, a2, d2, l0, l2, h0, h2, d1;
var v0=0, v1=0, v2=0, e, t3, rise_str = "", set_str = "", result = "";

p1 = PI;
p2 = 2 * p1;
r1 = PI / 180;
RAD = 1 / r1;
k1 = 15 * r1 * 1.002738;

b5 = latitude * RAD;
l5 = longitude * RAD;
h = tzone;
l5 = -l5 / 360;
z0 = -h / 24;

t = (kalendarz_licz() - 2451545.0);

t0 = t / 36525;
s = (24110.5 + 8640184.813 * t0 + 86636.6 * z0 + 86400 * l5) / 86400;
s = s - floor(s);
t0 = s * 360 * r1;
t = t + z0;

for (i=1; i<4; i++)
{
l = 0.606434 + 0.036601 * t;
m = 0.374897 + 0.036292 * t;
f = 0.259091 + 0.036748 * t;
d = 0.827362 + 0.033863 * t;
n = 0.347343 - 1.470939e-04 * t;
g = 0.993126 + 2.737778e-03 * t;

l = (l - floor(l)) * p2;
m = (m - floor(m)) * p2;
f = (f - floor(f)) * p2;
d = (d - floor(d)) * p2;
n = (n - floor(n)) * p2;
g = (g - floor(g)) * p2;

v = 0.39558*sin(f+n) + 0.082*sin(f) + 0.03257*sin(m-f-n) + 0.01092*sin(m+f+n) + 6.66e-03*sin(m-f) - 6.44e-03*sin(m+f-2*d+n) - 3.31e-03*sin(f-2*d+n) - 3.04e-03*sin(f-2*d) - 2.4e-03*sin(m-f-2*d-n) + 2.26e-03*sin(m+f) - 1.08e-03*sin(m+f-2*d) - 7.9e-04*sin(f-n) + 7.8e-04*sin(f+2*d+n);

u = 1 - 0.10828*cos(m) - 0.0188*cos(m-2*d) - 0.01479*cos(2*d) + 1.81e-03*cos(2*m-2*d) - 1.47e-03*cos(2*m) - 1.05e-03*cos(2*d-g) - 7.5e-04*cos(m-2*d+g);

w = 0.10478*sin(m) - 0.04105*sin(2*f+2*n) - 0.0213*sin(m-2*d) - 0.01779*sin(2*f+n) + 0.01774*sin(n) + 9.87e-03*sin(2*d) - 3.38e-03*sin(m-2*f-2*n) - 3.09e-03*sin(g) - 1.9e-03*sin(2*f) - 1.44e-03*sin(m+n) - 1.44e-03*sin(m-2*f-n) - 1.13e-03*sin(m+2*f+2*n) - 9.4e-04*sin(m-2*d+g) - 9.2e-04*sin(2*m-2*d);

s = w / sqrt(u-v*v);
a5 = l + atan(s / sqrt(1-s*s));
s = v / sqrt(u);
d5 = atan(s / sqrt(1-s*s));
r5 = 60.40974 * sqrt(u);
r_a[i] = a5;
dec[i] = d5;
dist[i] = r5;

t += 0.5;
}

if (r_a[2] < r_a[1]) r_a[2] += p2;
if (r_a[3] < r_a[2]) r_a[3] += p2;
 
z1 = r1 * (90.567 - 41.685 / dist[2]);
s = sin(b5*r1);
c = cos(b5*r1);
z = cos(z1);
m8 = 0;
w8 = 0;
a0 = r_a[1];
d0 = dec[1];

for (c0=0; c0<24; c0++)
{
p = (c0 + 1) / 24;
f0 = r_a[1];
f1 = r_a[2];
f2 = r_a[3];
a = f1 - f0;
b = f2 - f1 - a;
f = f0 + p * (2 * a + b * (2 * p - 1));
a2 = f;

f0 = dec[1];
f1 = dec[2];
f2 = dec[3];
a = f1 - f0;
b = f2 - f1 - a;
f = f0 + p * (2 * a + b * (2 * p - 1));
d2 = f;

l0 = t0 + c0 * k1;
l2 = l0 + k1;
if (a2 < a0) a2 += p2;
h0 = l0 - a0;
h2 = l2 - a2;
h1 = (h2 + h0) / 2;
d1 = (d2 + d0) / 2;
if (c0 < 1) v0 = s * sin(d0) + c * cos(d0) * cos(h0) - z;
v2 = s * sin(d2) + c * cos(d2) * cos(h2) - z;
if (sign_of(v0) != sign_of(v2))
{
 v1 = s * sin(d1) + c * cos(d1) * cos(h1) - z;
 a = 2 * v2 - 4 * v1 + 2 * v0;
 b = 4 * v1 - 3 * v0 - v2;
 d = b * b - 4 * a * v0;
 if (d >= 0)
 {
 d = sqrt(d);
 e = (-b + d) / (2 * a);
 if (e > 1 || e < 0) e = (-b - d) / (2 * a);
 t3 = rano_pop(c0 + e);
 if (v0 < 0 && v2 > 0)
 {
  m8 = 1;
  rise_str = t3;
 }
 if (v0 > 0 && v2 < 0)
 {
  w8 = 1;
  set_str = t3;
 }
 }
}
a0 = a2;
d0 = d2;
v0 = v2;
}

if (m8 == 0 && w8 == 0)
{
 if (v2 < 0)
 {
 rise_str = "down 24h";
 set_str = "down 24h";
 }
 if (v2 > 0)
 {
 rise_str = " up 24h ";
 set_str = " up 24h ";
 }
}
else
{
 if (m8 == 0) rise_str = " ------ ";
 if (w8 == 0) set_str = " ------ ";
}

result = rise_str + " / " + set_str;

return result;
}
}

function oblicz_slonce(latitude,longitude,tzone)
{
with (Math) {

var r_a = new Array(2);
var dec = new Array(2);
var p1 = PI;
var p2 = 2 * p1;
var dr = p1 / 180;
var RAD = 1 / dr;
var k1 = 15 * dr * 1.0027379;

var b5, l5, h, z0, t, tt, t0, s, l, g, v, u, w;
var a5, d5, r5, z1, c, z, m8, w8, a0, d0, da, dd;
var c0, p, a2, d2, l0, l2, h0, h1, h2, d1;
var v0=0, v1=0, v2=0, a, b, d, e, t3;
var rise_str = "", set_str = "", result = "";

b5 = latitude * RAD;
l5 = longitude * RAD;
h = tzone;
l5 = -l5 / 360;
z0 = -h / 24;
t = (kalendarz_licz() - 2451545.0);

tt = t / 36525 + 1;
t0 = t / 36525;
s = (24110.5 + 8640184.813 * t0 + 86636.6 * z0 + 86400 * l5) / 86400;
s = s - floor(s);
t0 = s * 360 * dr;

t = t + z0;
l = .779072+.00273790931*t;
g = .993126+.0027377785*t;
l = (l-floor(l))*p2;
g = (g-floor(g))*p2;
v = .39785*sin(l)-.01000*sin(l-g)+.00333*sin(l+g)-.00021*tt*sin(l);
u = 1-.03349*cos(g)-.00014*cos(2*l)+.00008*cos(l);
w = -.00010-.04129*sin(2*l)+.03211*sin(g)+.00104*sin(2*l-g)-.00035*sin(2*l+g)-.00008*tt*sin(g);
s = w/sqrt(u-v*v);
a5 = l+atan(s/sqrt(1-s*s));
s = v/sqrt(u);
d5 = atan(s/sqrt(1-s*s));
r5 = 1.00021*sqrt(u);
r_a[1] = a5;
dec[1] = d5;    

t = t + 1;

l = .779072+.00273790931*t;
g = .993126+.0027377785*t;
l = (l-floor(l))*p2;
g = (g-floor(g))*p2;
v = .39785*sin(l)-.01000*sin(l-g)+.00333*sin(l+g)-.00021*tt*sin(l);
u = 1-.03349*cos(g)-.00014*cos(2*l)+.00008*cos(l);
w = -.00010-.04129*sin(2*l)+.03211*sin(g)+.00104*sin(2*l-g)-.00035*sin(2*l+g)-.00008*tt*sin(g);
s = w/sqrt(u-v*v);
a5 = l+atan(s/sqrt(1-s*s));
s = v/sqrt(u);
d5 = atan(s/sqrt(1-s*s));
r5 = 1.00021*sqrt(u);
r_a[2] = a5;
dec[2] = d5;
if (r_a[2] < r_a[1]) r_a[2] += p2;
z1 = dr * 90.833;
s = sin(b5 * dr);
c = cos(b5 * dr);
z = cos(z1);
m8 = 0;
w8 = 0;
a0 = r_a[1];
d0 = dec[1];
da = r_a[2] - r_a[1];
dd = dec[2] - dec[1];

for (c0=0; c0<24; c0++)
{
p = (c0 + 1) / 24;
a2 = r_a[1] + p * da;
d2 = dec[1] + p * dd;

l0 = t0 + c0 * k1;
l2 = l0 + k1;
h0 = l0 - a0;
h2 = l2 - a2;
h1 = (h2 + h0) / 2;
d1 = (d2 + d0) / 2;
if (c0 < 1) v0 = s * sin(d0) + c * cos(d0) * cos(h0) - z;
v2 = s * sin(d2) + c * cos(d2) * cos(h2) - z;
if (sign_of(v0) != sign_of(v2))
{
 v1 = s * sin(d1) + c * cos(d1) * cos(h1) - z;
 a = 2 * v2 - 4 * v1 + 2 * v0;
 b = 4 * v1 - 3 * v0 - v2;
 d = b * b - 4 * a * v0;
 if (d >= 0)
 {
 d = sqrt(d);
 e = (-b + d) / (2 * a);
 if (e > 1 || e < 0) e = (-b - d) / (2 * a);
 


t3 = rano_pop(c0 + e);
 if (v0 < 0 && v2 > 0)
 {
  m8 = 1;
  
  rise_str = t3;
 }
 if (v0 > 0 && v2 < 0)
 {
  w8 = 1;
  set_str = t3;
 
 }
 }
}
a0 = a2;
d0 = d2;
v0 = v2;
}


if (m8 == 0 && w8 == 0)
{
 if (v2 < 0)
 {
 rise_str = "down 24h";
 set_str = "down 24h";
 }
 if (v2 > 0)
 {
 rise_str = " up 24h ";
 set_str = " up 24h ";
 }
}
else
{
 if (m8 == 0) rise_str = " ------ ";
 if (w8 == 0) set_str = " ------ ";
}

result = rise_str + " / " + set_str;

return result;
}
}
