require(["dojo/parser", "dojo/_base/lang", "dojo/_base/declare", "dijit/registry",
"dojo/date/locale", "dojo/store/JsonRest", "dijit/Calendar",
"dojo/dom", "dojo/dom-style", "dojo/date", "dojo/html", "dijit/form/TimeTextBox",
"dijit/form/NumberTextBox", "dojo/data/ItemFileReadStore", "dojo/domReady!"],
function(parser, lang, declare, registry, locale, JsonRest, Calendar, dom,
domStyle, date, html, TimeTextBox, NumberTextBox, ItemFileReadStore) {
var now = locale.format(new Date(), {datePattern: 'MM-dd-yyyy',selector: 'date'});
var loadingProgress = 0;
var calendar = new Calendar({
value: new Date(),
onChange: function(value){
refresh();
}
}, "calendarSelect");
function refresh() {
var calendarValue = dijit.byId("calendarSelect").value;
var actualTimeValue = dijit.byId("actualTime").value;
var dateStr = locale.format(calendarValue, {datePattern: 'MM-dd-yyyy',selector: 'date'});
var timeStr = locale.format(actualTimeValue, {timePattern: 'HH:mm:ss',selector: 'time'});
html.set(dom.byId("LunarText1"),"It's Day "+getMoonAge(getJulianDay())+" of the current lunar month.");
html.set(dom.byId("LunarText2"),getLunarDate());
html.set(dom.byId("LunarText3"),moonPhases_[computePhaseIndex()]+" in X degrees and X minutes of [sign]");
html.set(dom.byId("SolarText1"),TEN_SPACES+dateStr+" CE"+TEN_SPACES+TEN_SPACES+TEN_SPACES+timeStr);
html.set(dom.byId("SolarText2"),getTide());
html.set(dom.byId("SolarText3"),"The Sun is in X degrees, X minutes and X seconds of [sign].");
html.set(dom.byId("XmasText"),"There are only "+getXmas()+" more shopping days until Xmas and "+getXDay()+" until X-Day.");
html.set(dom.byId("FrenchText")," FRENCH: "+getFrenchStr());
html.set(dom.byId("MayanText"),"MAYAN DATE: "+getTzolkinDate() + " " + getHaabDate()+" ("+getLongCount()+")");
html.set(dom.byId("JulianText")," The Julian Day is "+getJulianDay()+".");
html.set(dom.byId("ChinaText")," Chinese year of the "+getChineseYear()+".");
html.set(dom.byId("DiscordianText")," DISCORDIAN: Today is "+getDiscordianDay()+" -- "+getDiscordianRandomText());
html.set(dom.byId("PlanetaryHourText"),"Planetary Hour of X.");
html.set(dom.byId("FeriaText")," FERIÆ: "+getFeria());
}
function getChineseYear() {
var calendarValue = dijit.byId("calendarSelect").value;
var chineseYearOffset = calendarValue.getUTCFullYear() - 1970;
var jan1 = new Date(calendarValue.getUTCFullYear(),0,1);
var dateDiff = date.difference(jan1,calendarValue);
dateDiff = dateDiff + 1;
while (chineseYearOffset > chineseNewYear_.length) {
chineseYearOffset = chineseYearOffset - 65;
}
if (dateDiff < chineseNewYear_[chineseYearOffset]) {
chineseYearOffset--;
}
if (chineseYearOffset < 0) {
chineseYearOffset = 0;
}
var chineseYear = chineseZodiac_[chineseYearOffset % 12];
return chineseYear;
}
function getTide() {
var calendarValue = dijit.byId("calendarSelect").value;
var jan1 = new Date(calendarValue.getUTCFullYear(),0,1);
var dateDiff = date.difference(jan1,calendarValue) + 1;
var offset = 0;
if (date.isLeapYear(calendarValue)) {
offset = 1;
}
var tide = "Roll Tide";
if (dateDiff >= CANDLEMAS_START && dateDiff <= CANDLEMAS_END + offset) {
tide = "Tide of Candlemas";
}
else if (dateDiff >= BELTANE_START + offset && dateDiff <= BELTANE_END + offset) {
tide = "Tide of Beltane";
}
else if (dateDiff >= LAMMAS_START + offset && dateDiff <= LAMMAS_END + offset) {
tide = "Tide of Lammas";
}
else if (dateDiff >= HALLOWEEN_START + offset && dateDiff <= HALLOWEEN_END + offset) {
tide = "Tide of Hallowe'en";
}
tide = tide + " ";
return tide;
}
function getJulianDay() {
var calendarValue = dijit.byId("calendarSelect").value;
var actualTimeValue = dijit.byId("actualTime").value;
var time_exact = actualTimeValue.getHours()/24 + actualTimeValue.getMinutes()/1440;
time_exact = time_exact - 0.5;
//alert(actualTimeValue.getHours() + " " + time_exact);
calendarValue.setHours(actualTimeValue.getHours());
calendarValue.setMinutes(actualTimeValue.getMinutes());
var baseTime = new Date(1970,0,1);
baseTime.setHours(0);
//var julian = date.difference(baseTime,calendarValue,"day") + 2440587.5;
var julian = calendarValue.getTime()/86400000 + 2440587.5
//julian = julian + time_exact;
// Might not work before 1970, but oh well.
/*
var julianYear = calendarValue.getUTCFullYear();
var julianMonth = calendarValue.getUTCMonth();
if (julianMonth > 2) {
julianMonth++;
}
else {
julianYear--;
julianMonth = julianMonth + 13;
}
var day = calendarValue.getUTCDate();
var julian = (365.25 * julianYear)
+ (30.6001*julianMonth) + day + 1720995.0;
var ja = 0.01 * julianYear;
julian += (2 - ja + (0.25 * ja));
*/
calendarValue.setHours(0);
calendarValue.setMinutes(0);
//return julian;
return Math.floor(julian*2) / 2;
}
function getLunarDate() {
var lunar105 = new Date (1993,7,4);
var calendarValue = dijit.byId("calendarSelect").value;
var lunarYear = 105;
var timeDiff = date.difference(lunar105,calendarValue,"day");
lunarYear = lunarYear + Math.floor(timeDiff / (19*365));
var lunarMonth = 1;
var lunarDay = 1;
if (timeDiff < 0) {
lunarMonth = 365;
lunarDay = 19;
}
timeDiff = timeDiff % (19*365);
lunarMonth = lunarMonth + Math.floor(timeDiff / 19);
lunarDay = lunarDay + (timeDiff % 19);
var lunarDate = lunarDay + " - " + lunarMonth + " - " + lunarYear;
return lunarDate;
}
function getHaabDate() {
var haabBase = new Date(1980,3,10);
var calendarValue = dijit.byId("calendarSelect").value;
var timeDiff = date.difference(haabBase,calendarValue,"day");
while (timeDiff < 0) {
timeDiff = timeDiff + 365;
}
timeDiff = timeDiff % 365;
var daysign = Math.floor(timeDiff / 20);
var period = timeDiff % 20;
return period + " " + haabDaySignArray_[daysign];
}
function getTzolkinDate() {
var tzolkinBase = new Date(1999,0,4);
var calendarValue = dijit.byId("calendarSelect").value;
var timeDiff = date.difference(tzolkinBase,calendarValue,"day");
// dates before '99 may be wonky. Oh well.
while (timeDiff < 0) {
timeDiff = timeDiff + 260;
}
var daySign = timeDiff % 20;
var period = timeDiff % 13;
return periodArray_[period] + " " + tzolkinDaySignArray_[daySign];
}
function getLongCount() {
var baktun13 = new Date(2012,11,21); // 13.0.0.0.0
var calendarValue = dijit.byId("calendarSelect").value;
var baktun = 13;
var katun = 0;
var tun = 0;
var uinal = 0;
var k_in = 0;
if (date.compare(baktun13,calendarValue) > 0) {
var timeDiff = date.difference(calendarValue,baktun13,"day");
baktun--;
katun = 19;
tun = 19;
uinal = 17;
k_in = 19;
baktun = baktun - Math.floor(timeDiff / 144000);
timeDiff = timeDiff % 144000;
katun = katun - Math.floor(timeDiff / 7200);
timeDiff = timeDiff % 7200;
tun = tun - Math.floor(timeDiff / 360);
timeDiff = timeDiff % 360;
uinal = uinal - Math.floor(timeDiff / 20);
timeDiff = timeDiff % 20;
k_in = k_in - timeDiff;
}
else {
var timeDiff = date.difference(baktun13,calendarValue,"day");
baktun = baktun + Math.floor(timeDiff / 144000);
timeDiff = timeDiff % 144000;
katun = katun + Math.floor(timeDiff / 7200);
timeDiff = timeDiff % 7200;
tun = tun + Math.floor(timeDiff / 360);
timeDiff = timeDiff % 360;
uinal = uinal + Math.floor(timeDiff / 20);
timeDiff = timeDiff % 20;
k_in = k_in + timeDiff;
}
return baktun + "." + katun + "." + tun + "." + uinal + "." + k_in;
}
function getXmas() {
var calendarValue = dijit.byId("calendarSelect").value;
var xmasDate = new Date(calendarValue.getUTCFullYear(),11,25);
if (date.compare(xmasDate,calendarValue) < 0) {
xmasDate = date.add(xmasDate,"year",1);
}
return date.difference(calendarValue,xmasDate,"day");
}
function getXDay() {
var calendarValue = dijit.byId("calendarSelect").value;
var xDayDate = new Date(8661,6,5);
return date.difference(calendarValue,xDayDate,"day");
}
function getDiscordianDay() {
var calendarValue = dijit.byId("calendarSelect").value;
var jan1 = new Date(calendarValue.getUTCFullYear(),0,1);
var dateDiff = date.difference(jan1,calendarValue) + 1;
var discordianDate = "";//"Today is ";
var discordianDay = dateDiff;
if (discordianDay == FEB_29 && date.isLeapYear(calendarValue)) {
discordianDate = "St. Tib's Day";
}
else {
if (discordianDay > FEB_29 && date.isLeapYear(calendarValue)) {
discordianDay--;
}
var discordianDayOfWeek = (discordianDay - 1) % DISCORDIAN_DAYS_PER_WEEK;
var discordianMonth = Math.floor((discordianDay - 1) / DISCORDIAN_DAYS_PER_MONTH);
var discordianDayOfMonth = ((discordianDay - 1) % DISCORDIAN_DAYS_PER_MONTH) + 1;
discordianDate += discordianWeekday_[discordianDayOfWeek];
discordianDate += ", the ";
discordianDate += discordianDayOfMonth;
if (discordianDayOfMonth % 10 == 1) {
discordianDate += "st";
}
else if (discordianDayOfMonth % 10 == 2) {
discordianDate += "nd";
}
else if (discordianDayOfMonth % 10 == 3) {
discordianDate += "rd";
}
else {
discordianDate += "th";
}
discordianDate += " day of ";
discordianDate += discordianMonth_[discordianMonth];
var discordianHolyDay = -1;
switch (discordianDay) {
case JAN_5:
discordianHolyDay = 0;
break;
case FEB_19:
discordianHolyDay = 1;
break;
case MAR_19:
discordianHolyDay = 2;
break;
case MAY_3:
discordianHolyDay = 3;
break;
case MAY_31:
discordianHolyDay = 4;
break;
case JUL_15:
discordianHolyDay = 5;
break;
case AUG_12:
discordianHolyDay = 6;
break;
case SEP_26:
discordianHolyDay = 7;
break;
case OCT_24:
discordianHolyDay = 8;
break;
case DEC_8:
discordianHolyDay = 9;
break;
}
if (discordianHolyDay > -1) {
discordianDate += "( "+discordianHolyDay_[discordianHolyDay]+")";
}
}
discordianDate += " in the YOLD "+(calendarValue.getUTCFullYear()+1166);
return discordianDate;
}
function getDiscordianRandomText() {
var randomTextIndex = Math.floor(Math.random() * 14);
return discordianRandomText_[randomTextIndex];
}
function getFrenchStr() {
var calendarValue = dijit.byId("calendarSelect").value;
var year = calendarValue.getUTCFullYear();
year = year - 1792;
year = year + 1;
var frenchStart = new Date(1792,8,22);
var frenchCurrent = new Date(1792,calendarValue.getUTCMonth(),calendarValue.getUTCDate());
var currentDay = 0;
if (date.compare(frenchCurrent,frenchStart) < 0 && date.isLeapYear(calendarValue)) {
frenchCurrent = date.add(frenchCurrent,"year",4);
currentDay = date.difference(frenchStart,frenchCurrent,"day");
while (currentDay > 365) {
currentDay = currentDay - 365;
}
year = year - 1;
}
else {
if (date.compare(frenchCurrent,frenchStart) < 0 && !date.isLeapYear(calendarValue)) {
frenchCurrent = date.add(frenchCurrent,"year",1);
year = year - 1;
}
currentDay = date.difference(frenchStart,frenchCurrent,"day");
}
var dayOfMonth = currentDay % 30;
var month = Math.floor(currentDay / 30);
var dayOfWeek = currentDay % 10;
var returnLine = "";
if (month < 12) {
returnLine+= frenchDayOfWeek_[dayOfWeek]+", "; // ex: Duodi (does not apply to Sansculottides)
}
returnLine += (dayOfMonth+1)+ " "; // ex: 22
returnLine += frenchMonth_[month] + ", "; // ex: Brumaire
returnLine += year; // ex: 224
returnLine += " ";
returnLine += frenchDayDescription_[currentDay];
return returnLine;
}
function getFeria() {
var calendarValue = dijit.byId("calendarSelect").value;
var jan1 = new Date(calendarValue.getUTCFullYear(),0,1);
var jan189 = new Date(1989,0,1);
var dateDiff = date.difference(jan1,calendarValue,"day");
if (dateDiff+1 >= FEB_29 && !date.isLeapYear(calendarValue)) {
dateDiff++;
}
var feria = feriae[dateDiff];
var dateDiff2 = date.difference(jan189,calendarValue,"day");
if (dateDiff2 % 8 == 0) {
if (feria == null) {
feria = "Nundinæ";
}
else {
feria = "Nundinæ; "+feria;
}
}
if (feria == null) {
feria = "None today "+TEN_SPACES+TEN_SPACES+TEN_SPACES+TEN_SPACES+"Dies Fastus";
}
return feria;
}
function FIXANGLE(a) {
return (a) - 360.0 * (Math.floor((a) / 360.0))
}
function TORAD(d) {
return (d) * (MY_PI / 180.0);
}
function TODEG(r) {
return (r) * (180.0 / MY_PI);
}
function kepler(m) {
var e;
var delta;
e = TORAD(m);
m = TORAD(m);
while (Math.abs(delta) - KEPLER_EPSILON > 0.0) {
delta = e - ECCENT_EARTH_ORBIT * Math.sin(e) - m;
e -= delta / (1.0 - ECCENT_EARTH_ORBIT * Math.cos(e));
}
return (e);
}
function days_of_february() {
var calendarValue = dijit.byId("calendarSelect").value;
if (date.isLeapYear(calendarValue)) {
return 29;
}
else {
return 28;
}
}
function SGN(gc_x) {
if (gc_x < 0) {
return -1;
}
else if (gc_x > 0) {
return 1;
}
else {
return 0;
}
}
function getMoonAge(julian_date) {
var date_within_epoch;
var sun_eccent;
var sun_mean_anomaly;
var sun_perigree_co_ordinates_to_epoch;
var sun_geocentric_elong;
var moon_evection;
var moon_variation;
var moon_mean_anomaly;
var moon_mean_longitude;
var moon_annual_equation;
var moon_correction_term1;
var moon_correction_term2;
var moon_correction_equation_of_center;
var moon_corrected_anomaly;
var moon_corrected_longitude;
var moon_present_age;
var moon_present_phase;
var moon_present_longitude;
/*
Calculation of the Sun's position.
*/
date_within_epoch = julian_date - EPOCH;
sun_mean_anomaly = FIXANGLE((360.0 / 365.2422) * date_within_epoch);
sun_perigree_co_ordinates_to_epoch = FIXANGLE(sun_mean_anomaly + SUN_ELONG_EPOCH - SUN_ELONG_PERIGEE);
sun_eccent = kepler(sun_perigree_co_ordinates_to_epoch);
sun_eccent = Math.sqrt((1.0 + ECCENT_EARTH_ORBIT) / (1.0 - ECCENT_EARTH_ORBIT)) * Math.tan(sun_eccent / 2.0);
sun_eccent = 2.0 * TODEG(atan(sun_eccent));
sun_geocentric_elong = FIXANGLE(sun_eccent + SUN_ELONG_PERIGEE);
/*
Calculation of the Moon's position.
*/
moon_mean_longitude = FIXANGLE(13.1763966 * date_within_epoch + MOON_MEAN_LONGITUDE_EPOCH);
moon_mean_anomaly = FIXANGLE(moon_mean_longitude - 0.1114041 * date_within_epoch - MOON_MEAN_LONGITUDE_PERIGREE);
moon_evection = 1.2739 * Math.sin(TORAD(2.0 * (moon_mean_longitude - sun_geocentric_elong) - moon_mean_anomaly));
moon_annual_equation = 0.1858 * Math.sin(TORAD(sun_perigree_co_ordinates_to_epoch));
moon_correction_term1 = 0.37 * Math.sin(TORAD(sun_perigree_co_ordinates_to_epoch));
moon_corrected_anomaly = moon_mean_anomaly + moon_evection - moon_annual_equation - moon_correction_term1;
moon_correction_equation_of_center = 6.2886 * Math.sin(TORAD(moon_corrected_anomaly));
moon_correction_term2 = 0.214 * Math.sin(TORAD(2.0 * moon_corrected_anomaly));
moon_corrected_longitude = moon_mean_longitude + moon_evection + moon_correction_equation_of_center
- moon_annual_equation + moon_correction_term2;
moon_variation = 0.6583 * Math.sin(TORAD(2.0 * (moon_corrected_longitude - sun_geocentric_elong)));
// true longitude
moon_present_longitude = moon_corrected_longitude + moon_variation;
moon_present_age = moon_present_longitude - sun_geocentric_elong;
moon_present_phase = 100.0 * ((1.0 - Math.cos(TORAD(moon_present_age))) / 2.0);
if (0.0 < FIXANGLE(moon_present_age) - 180.0) {
moon_present_phase = -moon_present_phase;
}
var moonAgeAsDays_ = SYNMONTH * (FIXANGLE(moon_present_age) / 360.0);
return Math.floor(moonAgeAsDays_);
}
function computePhaseIndex(){
var calendarValue = dijit.byId("calendarSelect").value;
var actualTimeValue = dijit.byId("actualTime").value;
// String moon_phase_name[] = { "New Moon", // 0
// "Waxing crescent", // 1
// "First quarter", // 2
// "Waxing gibbous", // 3
// "Full Moon", // 4
// "Waning gibbous", // 5
// "Third quarter", // 6
// "Waning crescent" }; // 7
var phase; // Moon phase
// double factor; // Moon phase factor
var year = calendarValue.getUTCFullYear()
var month = calendarValue.getUTCMonth();
var day = calendarValue.getUTCDate();
var hour = actualTimeValue.getHours();
var min = actualTimeValue.getMinutes();
var sec = actualTimeValue.getSeconds();
var time_exact = hour/24 + min/1440 + sec/86400;
//var phase; // Moon phase
var cent; // Century number (1979 = 20)
var epact; // Age of the moon on Jan. 1
var diy; // Day in the year
var golden; // Moon's golden number
if (month < 0 || month > 12) {
month = 0; // Just in case
} // Just in case
var jan1 = new Date(year,0,1);
diy = date.difference(jan1,calendarValue) + 1 + time_exact;
cent = (year / 100) + 1; // Century number
golden = (year % 19) + 1; // Golden number
epact = ((11 * golden) + 20 // Golden number
+ (((8 * cent) + 5) / 25) - 5 // 400 year cycle
- (((3 * cent) / 4) - 12)) % 30; //Leap year correction
if (epact <= 0) {
epact += 30;
} // Age range is 1 .. 30
if ((epact == 25 && golden > 11) ||
epact == 24) {
epact++;
// Calculate the phase, using the magic numbers defined above.
// Note that (phase and 7) is equivalent to (phase mod 8) and
// is needed on two days per year (when the algorithm yields 8).
}
// Calculate the phase, using the magic numbers defined above.
// Note that (phase and 7) is equivalent to (phase mod 8) and
// is needed on two days per year (when the algorithm yields 8).
// this.factor = ((((diy + (double)epact) * 6) + 11) % 100 );
phase = (((((Math.floor(diy + epact)) * 6) + 11) % 177) / 22) & 7;
return(phase);
}
function atan(x) {
var SQRT3 = 1.732050807568877294;
var signChange=false;
var Invert=false;
var sp=0;
var x2, a;
// check up the sign change
if(x<0.) {
x=-x;
signChange=true;
}
// check up the invertation
if(x>1.) {
x=1/x;
Invert=true;
}
// process shrinking the domain until xMY_PI/12) {
sp++;
a=x+SQRT3;
a=1/a;
x=x*SQRT3;
x=x-1;
x=x*a;
}
// calculation core
x2=x*x;
a=x2+1.4087812;
a=0.55913709/a;
a=a+0.60310579;
a=a-(x2*0.05160454);
a=a*x;
// process until sp=0
while(sp>0) {
a=a+MY_PI/6;
sp--;
}
// invertation took place
if(Invert)
a=Math.PI/2-a;
// sign change took place
if(signChange)
a=-a;
//
return a;
}
function getWeekNumber(d) {
// Copy date so don't modify original
d = new Date(+d);
d.setHours(0,0,0);
// Set to nearest Thursday: current date + 4 - current day number
// Make Sunday's day number 7
d.setDate(d.getDate() + 4 - (d.getDay()||7));
// Get first day of year
var yearStart = new Date(d.getFullYear(),0,1);
// Calculate full weeks to nearest Thursday
var weekNo = Math.ceil(( ( (d - yearStart) / 86400000) + 1)/7)
// Return array of year and week number
return weekNo;
}
var latitude = new NumberTextBox({
name: "latitude",
constraints: {min: -180, max: 180, pattern: "0.#######"},
onChange: function(value) {
refresh();
}
}, "latitude");
var longitude = new NumberTextBox({
name: "longitude",
constraints: {min: -180, max: 180, pattern: "0.#######"},
onChange: function(value) {
refresh();
}
}, "longitude");
// latitude.startup();
// longitude.startup();
var actualTime = new TimeTextBox({name: "actualTime",
constraints: {
timePattern: 'HH:mm',
clickableIncrement: 'T00:15',
visibleIncrement: 'T00:15',
visibleRange: 'T01:00'
},
onChange: function(value) {
refresh();
},
value: new Date()
}, "actualTime");
gps();
refresh();
function gps() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else { // Assume you're in New England, because why wouldn't you be?
alert("Geolocation is not supported by this browser.");
latitude.setValue(41);
longitude.setValue(-72);
}
}
function showPosition(position) {
dijit.byId("latitude").set("value",""+Number(Math.round(position.coords.latitude+'e7')+'e-7'));
dijit.byId("longitude").set("value",""+Number(Math.round(position.coords.longitude+'e7')+'e-7'));
latitude.startup();
longitude.startup();
}
});