/* ksun.js - Kaluach suntimes Javascript routines
 *   Version 1.00 (initial release)
 *   Version 1.01 (fixed bug with time adjust - AM/PM and 24 hour clock)
 *   Version 1.02 (fixed bug with time adjust [again] - AM/PM and 24 hour clock)
 * Copyright (C) 5760-5762 (2000,2001 CE), by Abu Mami and Yisrael Hersch.
 *   All Rights Reserved.
 *   All copyright notices in this script must be left intact.
 * Based on:
 *	 - the program SUN.C by Michael Schwartz
 *   - an algorithm contained in:
 *         Almanac for Computers, 1990
 *         published by Nautical Almanac Office
 *         United States Naval Observatory
 *         Washington, DC 20392
 * Permission will be granted to use this script on your web page
 *   if you wish. All that's required is that you please ask.
 *   (Of course if you want to send a few dollars, that's OK too :-)
 * website: http://www.kaluach.net
 * email: abumami@kaluach.org
 */

var monCount = new makeArray(1, 32, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366);

function makeArray() {
	this[0] = makeArray.arguments.length;
	for (i = 0; i < makeArray.arguments.length; i = i + 1)
		this[i+1] = makeArray.arguments[i];
}

function doy(d, m, y) {
	return monCount[m] + d + (m > 2 && leap(y));
}

function suntime(
	d, m, y,
	zendeg, zenmin,
	londeg, lonmin, ew,
	latdeg, latmin, ns,
	tz)
{

	var lonhr;
	var longitude, latitude;
	var	coslat, sinlat, cosz;
	var sindec, cosdec;
	var xm_rise,  xm_set;
	var xl_rise,  xl_set;
	var a_rise,   a_set;
	var ahr_rise, ahr_set;
	var h_rise,   h_set;
	var t_rise,   t_set;
	var	ut_rise,  ut_set;

	var	retval = 0;	// NORMAL
	var day = doy(d, m, y);

	cosz = Math.cos(0.01745 * todec(zendeg, zenmin));

	longitude = todec(londeg, lonmin) * ((ew == 0) ? 1 : -1);
	lonhr	  = longitude / 15.0;
	latitude  = todec(latdeg, latmin) * ((ns == 0) ? 1 : -1);
	coslat	  = Math.cos(0.01745 * latitude);
	sinlat	  = Math.sin(0.01745 * latitude);

	t_rise = day + (6.0 + lonhr) / 24.0;
	t_set  = day + (18.0 + lonhr) / 24.0;

	xm_rise = M(t_rise);
	xl_rise = L(xm_rise);
	xm_set  = M(t_set);
	xl_set  = L(xm_set);
	
	a_rise = 57.29578 * Math.atan( 0.91746 * Math.tan(0.01745 * xl_rise) );
	a_set  = 57.29578 * Math.atan( 0.91746 * Math.tan(0.01745 * xl_set) );

	if(Math.abs(a_rise + 360.0 - xl_rise) > 90.0)
		a_rise += 180.0;
	if(Math.abs(a_rise + 360.0 - xl_rise) > 90.0)
		a_rise += 180.0;
	if(a_rise > 360.0)
		a_rise -= 360.0;

	if(Math.abs(a_set + 360.0 - xl_set) > 90.0)
		a_set += 180.0;
	if(Math.abs(a_set + 360.0 - xl_set) > 90.0)
		a_set += 180.0;
	if(a_set > 360.0)
		a_set -= 360.0;

	ahr_rise = a_rise / 15.0;
	sindec = 0.39782 * Math.sin(0.01745 * xl_rise);
	cosdec = Math.sqrt(1.0 - sindec * sindec);
	h_rise = (cosz - sindec * sinlat) / (cosdec * coslat);

	ahr_set = a_set / 15.0;
	sindec = 0.39782 * Math.sin(0.01745 * xl_set);
	cosdec = Math.sqrt(1.0 - sindec * sindec);
	h_set = (cosz - sindec * sinlat) / (cosdec * coslat);

	if(Math.abs(h_rise) <= 1.0)
		h_rise = 57.29578 * Math.acos(h_rise);
	else
		retval |= 2;	//NO_SUNRISE;

	if(Math.abs(h_set) <= 1.0)
		h_set = 57.29578 * Math.acos(h_set);
	else
		retval |= 1;	//NO_SUNSET;

	ut_rise  = ((360.0 - h_rise) / 15.0) + ahr_rise + adj(t_rise) + lonhr;
	ut_set  = (h_rise / 15.0) + ahr_set + adj(t_set) + lonhr;

	var ret = new Object();
	ret[1] = retval;
	ret[2] = ut_rise + tz;	// sunrise
	ret[3] = ut_set  + tz;	// sunset
	return ret;
}

function timeadj(t, ampm) {
	var hour;
	var min;

	var time = t;

	var hour = Math.floor(time);
	var min  = Math.floor((time - hour) * 60.0 + 0.5);

	if(min >= 60) {
		hour += 1;
		min  -= 60;
	}

	if(hour < 0)
		hour += 24;

	if(ampm) {
		ampm_str = (hour > 11) ? ' PM' : ' AM';
		hour %= 12;
		hour = (hour < 1) ? 12 : hour;
	}
	else
		ampm_str = '';

	var str = hour + ':' + ((min < 10) ? '0' : '') + min + ampm_str;
	return str;

}

function todec(deg, min) {
	return (deg + min / 60.0);
}

function M(x) {
	return (0.9856 * x - 3.251);
}

function L(x) {
	return (x + 1.916 * Math.sin(0.01745 * x) + 0.02 * Math.sin(2 * 0.01745 * x) + 282.565);
}

function adj(x) {
	return (-0.06571 * x - 6.620);
}

function leap(y) {
	return ((y % 400 == 0) || (y % 100 != 0 && y % 4 == 0));
}

function makeArray() {
    this[0] = makeArray.arguments.length;
    for (i = 0; i<makeArray.arguments.length; i++)
        this[i+1] = makeArray.arguments[i];
}

var daysofmonth   = new makeArray( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var daysofmonthLY = new makeArray( 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

function LeapYear(year) {
    if ((year/4)   != Math.floor(year/4))   return false;
    if ((year/100) != Math.floor(year/100)) return true;
    if ((year/400) != Math.floor(year/400)) return false;
    return true;
}

function NthDay(nth,weekday,month,year) {
    if (nth > 0) return (nth-1)*7 + 1 + (7 + weekday -
DayOfWeek((nth-1)*7 + 1,month,year))%7;
    if (LeapYear(year)) var days = daysofmonthLY[month];
    else                var days = daysofmonth[month];
    return days - (DayOfWeek(days,month,year) - weekday + 7)%7;
}

function DayOfWeek(day,month,year) {
    var a = Math.floor((14 - month)/12);
    var y = year - a;
    var m = month + 12*a - 2;
    var d = (day + y + Math.floor(y/4) - Math.floor(y/100) +
Math.floor(y/400) + Math.floor((31*m)/12)) % 7;
    return d+1;
}

function y2k(number)    { return (number < 1000) ? number + 1900 : number; }

var today = new Date();
var year = y2k(today.getYear());

var DSTstart = new Date(year,4-1,NthDay(1,1,4,year),2,0,0);
var DSTend   = new Date(year,10-1,NthDay(-1,1,10,year),2,0,0);

function getMS(date) {
    return Date.UTC(y2k(date.getYear()),date.getMonth(),date.getDate(),date.getHours(),date.getMinutes(),date.getSeconds());
}

var todayMS = getMS(today);
var DSTstartMS = getMS(DSTstart);
var DSTendMS = getMS(DSTend);

function daylightsavings() {
    if (todayMS > DSTstartMS && todayMS < DSTendMS) return 1;
    else return 0;
}

function this_shabbos_in_ny() {
	var lngd=71.03;
	var lngm=0;
	var ewi=0;
	var latd=42.37;
	var latm=0;
	var nsi=0;
	var adj=-5;
	var ampm=1;
	var now = new Date();
	var days_til_shabbos = 5 - now.getDay();
	var thisy = now.getYear();
	if (thisy < 1900)
		thisy += 1900;
	var shabbos = new Date(thisy,now.getMonth(),now.getDate()+days_til_shabbos,now.getHours(),now.getMinutes(),now.getSeconds());
	var d = shabbos.getDate();
	var m = shabbos.getMonth()+1;
	var y = shabbos.getYear();
	if(y < 1900)
		y += 1900;
	var time = suntime(d, m, y, 90, 50, lngd, lngm, ewi, latd, latm, nsi, adj);
	//alert(d + " " + m + " " + y + " " + 90 + " " + 50 + " " + lngd + " " + lngm + " " + ewi + " " + latd + " " + latm + " " + nsi + " " + adj);
var months = new makeArray('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
document.write(timeadj(time[3] - 18.0/60.0 + daylightsavings(), ampm));
}

this_shabbos_in_ny();

