/* DogCalendar1.2 - a JavaScript calendar greeting generator.
   (C)opyright J. A. H. Futterman 2003.
   Routines marked //CC are adapted from "Calendrical Calculations" 
   by Reingold and Dershowitz, Cambridge U Press, 2001,2002. */

var theYear;
var DayName = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday",
                        "Friday","Saturday");
var MonthName = new Array("January","February","March","April","May",
                         "June","July","August","September",
                         "October","November","December");
var HebMonth = new Array("Nisan","Iyyar","Sivan","Tammuz","Av",
                         "Elul","Tishri","Marheshvan","Kislev",
                         "Tevet","Shevat","Adar","Adar II");
var HebHol=new Array();
var epoch = new Array(); epoch["Gregorian"]=1; epoch["Hebrew"]=-1373427;

function altDate(yyyy,mm,dd){ this.year=yyyy; this.month=mm; this.day=dd; }
altDate.prototype.getYear=getaYear;
altDate.prototype.getMonth=getaMonth;
altDate.prototype.getDate=getaDay;
function getaYear() { return this.year; }
function getaMonth() { return this.month; }
function getaDay() { return this.day; }

function fixFmGregorian(yyyy,mm,dd){//CC
	var y1=yyyy-1;
	var fdate=epoch["Gregorian"] - 1
            + 365*y1
            + Math.floor(y1/4)
            - Math.floor(y1/100)
            + Math.floor(y1/400)
            + Math.floor((367*(mm+1)-362)/12)
            + dd;
	if(mm>1) fdate -= leapYear(yyyy) ? 1 : 2 ;
	return fdate;
}

function GregorianYearFmFix(fdate){//CC
	var d0=fdate-epoch["Gregorian"];
	var n400=Math.floor(d0/146097);
	var d1 = mod(d0,146097);
	var n100=Math.floor(d1/36524);
	var d2=mod(d1,36524);
	var n4=Math.floor(d2/1461);
	var d3=mod(d2/1461);
	var n1=Math.floor(d3/365);
	var yyyy=400*n400+100*n100+4*n4+n1;
	return (n100==4 || n4 == 4) ? yyyy : yyyy+1 ;
}

function GregorianFmFix(fdate){//CC
	var yyyy=GregorianYearFmFix(fdate); var corr=0;
	var pdays=fdate-fixFmGregorian(yyyy,0,1);
	if(fdate>fixFmGregorian(yyyy,2,1)) corr+=(leapYear(yyyy))? 1 : 2 ;
	var mm = Math.floor((12*(pdays+corr)+373)/367)-1;
	var dd = fdate-fixFmGregorian(yyyy,mm,1)+1;
	return new Date(yyyy,mm,dd);
}

function HebrewLeapYear(hyear) {//CC
   return (mod(7*hyear+1,19)<7) ? 1 : 0 ; }

function molad(hyear,hmonth){//CC
	var y=(hmonth<6) ? hyear+1: hyear;
	var months_elapsed = hmonth-6+Math.floor((235*y-234)/19);
	return epoch["Hebrew"]-876/25920+months_elapsed*(29.5+793/25920);
}

function HebCalElapsDays(hyear){//CC
	var months_elapsed = Math.floor((235*hyear-234)/19);
	var parts_elapsed = 12084+13753*months_elapsed;
	var day = 29*months_elapsed+Math.floor(parts_elapsed/25920);
	return ((mod(3*(day+1),7)<3) ? day+1 : day );
}

function HebNYDelay(hyear){//CC
	var ny0 = HebCalElapsDays(hyear-1);
	var ny1 = HebCalElapsDays(hyear);
	var ny2 = HebCalElapsDays(hyear+1);
	var days = 0;
	if((ny2-ny1)==356) days=2;
	if((ny1-ny0)==382) days=1;
	return days;
}

function HebNewYear(hyear){//CC
  return epoch["Hebrew"]+HebCalElapsDays(hyear)+HebNYDelay(hyear);
  }

function lastDayHebMonth(hyear,hmonth){//CC
	if(hmonth==1 || hmonth==3 || hmonth==5 || hmonth==9 || hmonth==12)
               return 29;
	if(hmonth==11 && !HebrewLeapYear(hyear))  return 29;
	var y=daysInHebYear(hyear);
	if(hmonth==7 && (y!=355 && y!=385)) return 29; // not long marheshvan
	if(hmonth==8 && (y==353 || y==383)) return 29; // short kislev
	return 30;	
}

function daysInHebYear(hyear){//CC
   return HebNewYear(hyear+1)-HebNewYear(hyear); }

function fixFmHebrew(hyear,hm,hd){//CC
	var ddd=HebNewYear(hyear)+hd-1;
	if(hm<6){
		var mlast=(HebrewLeapYear(hyear)) ? 13 : 12 ;
		for(m=6;m<mlast;m++) { ddd+=lastDayHebMonth(hyear,m); }
		for(m=0;m<hm;m++) { ddd+=lastDayHebMonth(hyear,m); }
	} else { for(m=6;m<hm;m++) { ddd+=lastDayHebMonth(hyear,m); } } 
	return ddd;
}

function HebrewFmFix(fdate){//CC
	var hyear, hmonth, hday=1, start, approx, mlast; 
	approx=Math.floor((fdate-epoch["Hebrew"])/(35975351/98496));
	for(y=approx; HebNewYear(y)<=fdate; y++){ hyear=y; }
	mlast=(HebrewLeapYear(hyear)) ? 13 : 12 ;
	start=(fdate<fixFmHebrew(hyear,0,1)) ? 6 : 0 ; hmonth=start-1;
	for(m=start; m<mlast ; m++){ 
		if(fdate<=fixFmHebrew(hyear,m,lastDayHebMonth(hyear,m)))
                      {hmonth=m; break;}
                                   }
	hday=fdate - fixFmHebrew(hyear,hmonth,1) + 1;
	return new altDate(hyear,hmonth,hday);
}

function leapYear(ayear) { // checks if year is a leap year
	return (((ayear % 4 == 0)
                  && (ayear % 100 != 0))
                 || (ayear % 400 == 0)) ? 1 : 0;
}

function mod(n,m)
 { if(m==0) return null;
   else return n-m*Math.floor(n/m); } // CC mod function

function Salutation(){ // Generates Hebrew salutation text.
	if (Salutation.arguments.length==0)
             aDate=new Date();
        else aDate=Salutation.arguments[0];
	var fdate = fixFmGregorian(aDate.getFullYear(),
                                   aDate.getMonth(),
                                   aDate.getDate());
	var newHebrew=HebrewFmFix(fdate);
	var hYear=newHebrew.getYear();
        var hMonth=newHebrew.getMonth();
        var hDay=newHebrew.getDate();
	var msg="Today is " + DayName[aDate.getDay()] + ", "
                + MonthName[aDate.getMonth()] + " "
                + aDate.getDate().toString() + ", "
                + aDate.getFullYear().toString() + " = " 
                + HebMonth[hMonth]
                + ((HebrewLeapYear(hYear) && hMonth==11) ? " I" : "")
                + " " +  hDay.toString() + ", "
                + hYear.toString()
                + ":  " + Message(hYear, hMonth, hDay);
	return msg;
}

function Message(y,m,d)// Hebrew year-month-day
 { if ((m == 0) && (d < 23))
      return "For a really special Passover dessert, make Almond Macaroons from Nancy, France."
   else if ((m == 0) || ((m == 1) && (d < 10)))
      return "Now that Passover is finished, make Pizza or Calzoni.  They're not as difficult as you might think!"
   else if ((m == 1) && (d < 19))
      return "For your Lag BaOmer barbecue, try Potato Corn Salad and Grilled Chicken Breasts with Peppers."
   else if (m == 1)
      return "Finish your meal with marvelous Warm Chocolate Cake!"
   else if ((m == 2) && (d < 8))
      return "Ruth's Cheesecake is essential for Shavuoth!"
   else if ((m == 2) || ((m == 3) && (d < 20)))
      return "Cherry Pie and Raspberry-Cherry Pie are wonderful ways to end the day!"
   else if ((m == 3) || ((m == 4) && (d < 20)))
      return "Cool off this summer with refreshing Melon-Mango Soup."
   else if (m == 4)
      return "Enjoy summer's bounty with Blueberry-Peach Soup or Asparagus, Corn, and Mixed Greens with Tarragon Dressing."
   else if ((m == 5) && (d < 25))
      return "It's a good time to learn to make Challah for Yom Tov."
   else if ((m == 5) || ((m == 6) && (d < 5)))
      return "Try Gefilte Fish Loaf and Apple Cake to make this a special Rosh Hashanah."
   else if ((m == 6) && (d < 22))
      return "Chicken Soup and Kreplach are perfect for Erev Yom Kippur and Hashannah Rabbah."
   else if ((m == 6) || (m == 7))
      return "Try Pecan Rolls for Sunday brunch--you'll look forward to waking up!"
   else if ((m == 8) && (d < 10))
      return "You'd swap your birthright for a bowl of Bulgarian Red Pepper Stew."
   else if ((m == 8) && (d < 20))
      return "It's Cholent weather again--try some this Shabbat!"
   else if ((m == 8) || ((m == 9) && (d < 3)))
      return "For Hannukah, try Vegetable Cutlets and Pot Roast for a change."
   else if (m == 9)
      return "It's the little things in life--Bourekas, Truffles, Jamaican Beef Patties, hot Bagels--that keep you going."
   else if (m == 10)
      return "Have a more delicious Tu B'Shevat with Chocolate Stuffed Figs and Apricots."
   else if ((m == 11) && HebrewLeapYear(y))
      return "Home-baked Onion Rolls make a lowly hamburger into a feast."
   else if (d < 20)
      return "Be happy it's Adar--and eat Hamantaschen!"
   else
      return "Use up your extra flour by baking some bread and pizza."; }

/* Copyright (C) 1996 Frequency Graphics All Rights Reserved.  Feel free to
   reuse this code snippet provided this header remains in tact Andy Augustine
   3.17.96 [www.FreqGrafx.com/411/] send comments to <mohammed@freqgrafx.com>
*/


function statusMessageObject(p,d) {
  this.msg = MESSAGE
  this.out = " "
  this.pos = POSITION
  this.delay = DELAY
  this.i     = 0
  this.reset = clearMessage
  }

function clearMessage() { this.pos = POSITION }

var POSITION = 100
var DELAY    = 90
var MESSAGE=Salutation()
var scroll = new statusMessageObject()

/* Make an array and set to all empty entries */

function makearray(n) {
  this.length = n;
  for(var i = 1; i <= n; i++) this[i] = ""
  return this;
}

function scroller() {
  // add spaces to begining of message
  for (scroll.i = 0; scroll.i < scroll.pos; scroll.i++) { scroll.out += " " }

  /* if you are still have leading spaces, just add custom string to tail of
     message OR else if the string is running off the screen, only add the
     characters left*/

  if (scroll.pos >= 0) scroll.out += scroll.msg
  else         scroll.out = scroll.msg.substring(-scroll.pos,scroll.msg.length)

  window.status = scroll.out

  // set parameters for next run

  scroll.out = " "
  scroll.pos--

  // if you are at the end of the message, reset parameters to start again
  if (scroll.pos < -(scroll.msg.length)) { scroll.reset() }
  setTimeout ('scroller()',scroll.delay)
  }

