/*****************************************************************************/


function sign(d){
  if (d<0) d+=360;
  if (d>360) d-=360;
  return Math.floor(d/30);
}


function phase(s,m){
  d = m-s;
  if (d<0) d+=360;
  if (d>360) d-=360;
  return Math.floor(d/45);

}

function formattime(h, m) {

   if (m < 10) m = "0" + m; // leading 0

   if (h == 24)    return "12:" + m + "am"; // actually the next day
   if (h > 12)    return (h-12) + ":" + m + "pm";
   if (h == 12)   return "12:" + m + "pm";
   if (h == 0)   return "12:" + m + "am";
   return h + ":" + m + "am";
}

function timeofday(s) {
   d = new Date(s);
   m = d.getMinutes();
   h = d.getHours();

   return formattime(h,m);
}

function showriseset(now){

   year_start = new Date(now.getFullYear(), 0, 1);
   day_in_year = Math.floor((1 + (now.valueOf() - year_start.valueOf())/86400000)); 

   // correct for Summer Time.
   DST = 0;
   if ((now.getTimezoneOffset() - year_start.getTimezoneOffset()) != 0) DST = 1;
     // This is in some error, but will do for now. Reports times between 12 and 1am as the previous day.
 
   srs = "Sun Rise: " + formattime (DST + Math.floor(rise_times[day_in_year] / 100), rise_times[day_in_year] % 100) 
   	+ " Set: " + formattime (DST + Math.floor(set_times[day_in_year] / 100), set_times[day_in_year] % 100);
   	
    if (moonrise_times[day_in_year] == 0) { mr = ""; } else
 	  {mr = "Rise: " + formattime (DST + Math.floor(moonrise_times[day_in_year] / 100), moonrise_times[day_in_year] % 100); }

    if (moonset_times[day_in_year] == 0) { ms = ""; } else
      { ms = "Set: " + formattime (DST + Math.floor(moonset_times[day_in_year] / 100), moonset_times[day_in_year] % 100); }

	if (moonrise_times[day_in_year] < moonset_times[day_in_year])
		mrs= "Moon " + mr + " " + ms;
	else 	mrs= "Moon " + ms + " " + mr;

    return srs + "<br>" + mrs;
}

function to_minutes(time) {

   min = time % 100;
   hour = (time-min) / 100;
   
   return (hour * 60 + min);
  
  
}

function showHour(nowTime) {
	
   now = nowTime.getHours() * 60 + nowTime.getMinutes();

  // correct for Summer Time.
   year_start = new Date(nowTime.getFullYear(), 0, 1);
   day_in_year = Math.floor((1 + (nowTime.valueOf() - year_start.valueOf())/86400000)); 
   DST = (nowTime.getTimezoneOffset() - year_start.getTimezoneOffset()) != 0;
   now = now + (nowTime.getTimezoneOffset() - year_start.getTimezoneOffset());


   hour_len = Math.round((to_minutes(set_times[day_in_year]) - to_minutes(rise_times[day_in_year])) / 10) ;

   
   auge = to_minutes(rise_times[day_in_year])-hour_len; 
   arktos = to_minutes(set_times[day_in_year])+hour_len;  // the *end* of arktos, which begins at sunset.

   hr = Math.floor(12 * (now-auge) / (arktos-auge));
     
  if ((now > auge) && (now < arktos) ) {
       return (hour_names[hr] + ",<br> the " + ordinal[hr] + " Hour of the day.");
  } else { return ("Night"); }
     
}

/*****************************************************************************/

sundegree = eval("sun"+today.getMonth())[today.getDate()];
moondegree = eval("moon"+today.getMonth())[today.getDate()];

sunsign=sign(sundegree);
moonsign=sign(moondegree);

sunsigndegree = Math.round(sundegree-sunsign*30);
moonsigndegree = Math.round(moondegree-moonsign*30);

moonphase = phase(sundegree, moondegree);


todaysol = getSolarDate(today);
todaylun = solar_to_lunar(todaysol);

lunarday = todaylun[1]
lunarmonth = todaylun[0]-1;


/*****************************************************************************/

// hol = holiday
hol = 0;
while ( (new Date(holiday[hol]).getTime() - today.getTime()) <= 0 ) { hol ++ }
hol--;
if (hol < 0) { hol = 7}; // today prior to first holiday in array
dayssinceholiday =  1 +  Math.floor((today.getTime() - new Date(holiday[hol]).getTime()) / (1000*60*60*24));
daystillholiday =   Math.floor((new Date(holiday[hol+1]).getTime() - today.getTime()) / (1000*60*60*24));
if (dayssinceholiday<0) {dayssinceholiday +=365;} // CHEATING - FIX LATER

// ha = astronomical holiday
tick = 0;
while ( (new Date(astroholiday[tick]).getTime() - today.getTime()) <= 0 ) { tick ++ }
ha = tick;
if (ha < 0) { ha = 7}; // today prior to first sun in array
tillastroholiday =   (new Date(astroholiday[ha]).getTime() - today.getTime()) / (1000*60*60*24);

// Find if Mercury is retrograde. 0 direct, 1 retrograde, -1 approaching retrograde
tick=0;
while ( (Date.parse(mercury[tick]) - today.getTime()) <= 0 ) { tick ++ }
merc= tick %2 ;
if (merc==0) if (Date.parse(mercury[tick]) - today.getTime() < mercwarning*(1000*60*60*24)) merc=-1;
mercdate = new Date(mercury[tick]);

/* DISABLED
//find almanac entry, if any
alm = -1;
tick=0;
while ( (Date.parse(almanactime[tick]) - today.getTime()) <= 0 ) { tick ++ }
tick--;
possmatch = new Date(almanactime[tick]);
if (today.getMonth() == possmatch.getMonth())
  if (today.getDate() == possmatch.getDate()) {alm = tick}

//find if Moon is Void of Course today
voc = -1;
tick=0;
while ( (Date.parse(moonentersign[tick]) - today.getTime()) <= 0 ) { tick ++ }
//tick--;
match1 = new Date(voidofcourse[tick]);
if (today.getMonth() == match1.getMonth())
  if (today.getDate() == match1.getDate()) {voc = tick}
match2 = new Date(moonentersign[tick]);
if (today.getMonth() == match2.getMonth())
  if (today.getDate() == match2.getDate()) {voc = tick}

*/

/*****************************************************************************/

