var sunrise = 0.0;
var solarNoon = 0.0;
var sunset = 0.0;
var userCityMap = document.getElementById("userCityMap"); //element to show user location retrived from geo location from navigator
var cityMap = document.getElementById("cityMap");//element to show city location retrived from user's GUI choice
var userLatitude = 0.0;
var userLongitude = 0.0;
/**
Gets sunrise , sun noon and sunset for specified latitude, longitude and the day
@param pLatitude - latitude in decimal presentation
@param pLatitude - longitude in decimal presentation
@param pYear - sunrise's year
@param pMonth - sunrise's month
@param pDay - sunrise's day
@param pGmt - gmt for specified latitude and longitude
*/
function getSunrise(pLatitude, pLongitude, pYear, pMonth, pDay, pGmt) {
PI = 3.14159265358979;
//longitude=0.01;//19.360;
//latitude=51.5;//51.369;
//year=2013;
//month=12;
//day=31;
latitude = pLatitude;
longitude = pLongitude;
n3 = PI / 180;
d5 = Number(pYear);
d6 = Number(pMonth);
d7 = Number(pDay);
if (d6 <= 2) {
e6 = d6 + 12;
} else {
e6 = d6;
}
if (d6 <= 2) {
e7 = d5 - 1;
} else {
e7 = d5;
}
l5 = Math.floor(d5 / 100);
l6 = 2 - l5 + Math.floor(l5 / 4);
l7 = Math.floor(365.25 * (e7 + 4716)) + Math.floor(30.6001 * (e6 + 1)) + d7 + l6 - 1524.5;
m3 = (l7 - 2451545) / 36525;
m4 = 280.46646 + 36000.76983 * m3 + 0.0003032 * m3 * m3;
o3 = 57.29577951;
m5 = 357.52911 + 35999.05029 * m3 - 0.0001537 * m3 * m3;
n5 = m5 / 360;
o5 = (n5 - Math.floor(n5)) * 360;
m6 = (1.914602 - 0.004817 * m3 - 0.000014 * m3 * m3) * Math.sin(o5 * n3);
m7 = (0.019993 - 0.000101 * m3) * Math.sin(2 * o5 * n3);
m8 = 0.000289 * Math.sin(3 * o5 * n3);
m9 = m6 + m7 + m8;
n4 = m4 / 360;
o4 = (n4 - Math.floor(n4)) * 360;
n6 = o4 + m9;
n7 = 125.04 - 1934.136 * m3;
if (n7 < 0) {
n9 = n7 + 360;
} else {
n9 = n7;
}
n10 = n6 - 0.00569 - 0.00478 * Math.sin(n9 * n3);
m11 = 23.43930278 - 0.0130042 * m3 - 0.000000163 * m3 * m3;
n11 = Math.sin(m11 * n3) * Math.sin(n10 * n3);
n12 = Math.asin(n11) * 180 / PI;
n15 = longitude / 15;
o15 = latitude;
m13 = (7.7 * Math.sin((o4 + 78) * n3) - 9.5 * Math.sin(2 * o4 * n3)) / 60;
o16 = Math.cos(n12 * n3) * Math.cos(o15 * n3);
n16 = -0.01483 - Math.sin(n12 * n3) * Math.sin(o15 * n3);
p15 = 2 * (Math.acos(n16 / o16) * o3) / 15;
sunrise = 13 - n15 + m13 - (p15 / 2) - 1 ; // - 1 to set time for Greenwich (UTC + 0)
solarNoon = 13 - n15 + m13 - 1; // {gorowanie}
sunset = 13 - n15 + m13 + (p15 / 2) - 1; // {zachod} - 1 to set time for Greenwich (UTC + 0)
pGmt = pGmt/60;
sunrise = sunrise + pGmt;
solarNoon = solarNoon + pGmt;
sunset = sunset + pGmt;
}
/**
Invoked from GUI. Calc sunrise, solarNoon and sunset for the specified parameter by user on GUI.
*/
function calcSunrise() {
var sunriseDate = new Date();
var gmt = 0;
timeZone = timeZones[timeZonesIndex];
dateToTab = document.getElementsByName("sunriseDate")[0].value.split("-");
sunriseDate.setFullYear(dateToTab[0], dateToTab[1] - 1, dateToTab[2]);
gmt = getGmtAtTimeZone(timeZone, sunriseDate);
getSunrise(convertDegreeToDecimal(timeZone.latitude), convertDegreeToDecimal(timeZone.longitude), dateToTab[0], dateToTab[1], dateToTab[2], gmt); // year, month, day, gmt
sunriseHour = Math.floor(sunrise)%24;
sunriseMin = Math.round((sunrise - Math.floor(sunrise)) * 60);
if (sunriseMin >= 60 ) {
sunriseMin = 0;
sunriseHour = sunriseHour + 1;
}
document.getElementById("sunrise").innerHTML = lpadTime(sunriseHour) + ':' + lpadTime(sunriseMin);
solarNoonHour = Math.floor(solarNoon)%24;
solarNoonMin = Math.round((solarNoon - Math.floor(solarNoon)) * 60);
if (solarNoonMin >= 60 ) {
solarNoonMin = 0;
solarNoonHour = solarNoonHour + 1;
}
document.getElementById("solarNoon").innerHTML = lpadTime(solarNoonHour) + ':' + lpadTime(solarNoonMin);
sunsetHour = Math.floor(sunset)%24;
sunsetMin = Math.round((sunset - Math.floor(sunset)) * 60);
if (sunsetMin >= 60 ) {
sunsetMin = 0;
sunsetHour = sunsetHour + 1;
}
document.getElementById("sunset").innerHTML = lpadTime(sunsetHour) + ':' + lpadTime(sunsetMin);
showCity(timeZone);
}
//var dateAtDest = getTimeDateAtLocationWith(timeZones[timeZonesIndex], vDate);
/**
Convert the degree to decimal number
*/
function convertDegreeToDecimal(value) {
convVal = 0.0;
if (value >= 0) {
convVal = Math.floor(value) + (value - Math.floor(value)) / 0.6;
} else {
convVal = Math.ceil(value) + (value - Math.ceil(value)) / 0.6;
}
return convVal;
}
/**
Invoked on page load. Calc sunrise, solarNoon and sunset at today for the user geo location retrived from navigator.
*/
function calcUserLocationSunrise(){
var sunriseUserDate = new Date();
var gmt = 0;
dateToTab = document.getElementsByName("sunriseUserDate")[0].value.split("-");
sunriseUserDate.setFullYear(dateToTab[0], dateToTab[1] - 1, dateToTab[2]);
gmt = 0 - sunriseUserDate.getTimezoneOffset();
getSunrise(userLatitude, userLongitude, dateToTab[0], dateToTab[1], dateToTab[2], gmt); // year, month, day, gmt
sunriseHour = Math.floor(sunrise)%24;
sunriseMin = Math.round((sunrise - Math.floor(sunrise)) * 60);
if (sunriseMin >= 60 ) {
sunriseMin = 0;
sunriseHour = sunriseHour + 1;
}
document.getElementById("userSunrise").innerHTML = lpadTime(sunriseHour) + ':' + lpadTime(sunriseMin);
solarNoonHour = Math.floor(solarNoon)%24;
solarNoonMin = Math.round((solarNoon - Math.floor(solarNoon)) * 60);
if (solarNoonMin >= 60 ) {
solarNoonMin = 0;
solarNoonHour = solarNoonHour + 1;
}
document.getElementById("userSolarNoon").innerHTML = lpadTime(solarNoonHour) + ':' + lpadTime(solarNoonMin);
sunsetHour = Math.floor(sunset)%24;
sunsetMin = Math.round((sunset - Math.floor(sunset)) * 60);
if (sunsetMin >= 60 ) {
sunsetMin = 0;
sunsetHour = sunsetHour + 1;
}
document.getElementById("userSunset").innerHTML = lpadTime(sunsetHour) + ':' + lpadTime(sunsetMin);
}
function getUserLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showUserPosition, showUserPositionError);
} else {
userCityMap.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showUserPosition(position) {
var latlon = position.coords.latitude + "," + position.coords.longitude;
userLatitude = position.coords.latitude;
userLongitude = position.coords.longitude;
var img_url = "http://maps.googleapis.com/maps/api/staticmap?center=" + latlon + "&zoom=12&size=400x240&markers=color:blue%7Clabel:Y%7C"+latlon+"&sensor=false";
document.getElementById("userCityMap").innerHTML = "";
calcUserLocationSunrise();
}
function showUserPositionError(error) {
switch (error.code) {
case error.PERMISSION_DENIED:
userCityMap.innerHTML = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
userCityMap.innerHTML = "Location information is unavailable."
break;
case error.TIMEOUT:
userCityMap.innerHTML = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
userCityMap.innerHTML = "An unknown error occurred."
break;
}
}
function showCity(timeZone) {
var latlon = convertDegreeToDecimal(timeZone.latitude) + "," + convertDegreeToDecimal(timeZone.longitude);
var img_url = "http://maps.googleapis.com/maps/api/staticmap?center=" + latlon + "&zoom=3&size=400x300&markers=color:blue%7Clabel:Y%7C"+latlon+"&sensor=false";
document.getElementById("cityMap").innerHTML = "";
}