//----- Baha'i date displayer
// Original code by Glen Little. Developed in April 1999 / Splendor 156
// code altered by Rick Poolman, Holland for bahaitwente.nl and bahai.be

// Version 1.0

// function to construct a BahaiDate object
function BahaiDate(timeToConvert, longitude, latitude)
 {
  var now = 'x'

  if(typeof(timeToConvert)=='string')  // given a string, convert to date
   {
    now = new Date()
    now.setTime(Date.parse(timeToConvert))
   }

  if(isNaN(now))   // wasn't a string, or a valid string
   {
    if(isNaN(timeToConvert))    // nothing given, use current date/time
     {
      now = new Date()
     }
    else  // is a number or date
     if(timeToConvert.constructor==Date)  // valid date
      {
       now = new Date(timeToConvert.getTime())
      }
     else   // number of ms
      {
       now = new Date()
       now.setTime(timeToConvert)
      }
   }

  if(now==null) return   // nothing worked, return null

    // store gregorian date/time in object for later use
  this.gDateTime = new Date(now.getTime())

    // determine the year
  var gYear = now.getFullYear()

    // determine some important dates
  var NawRuz = new Date(gYear,2,20)
      NawRuz.sunset(longitude, latitude)
      NawRuz.normalize(now)

  var lastNawRuz = new Date(gYear-1,2,20)
      lastNawRuz.sunset(longitude, latitude)
      lastNawRuz.normalize(now)

  var Loftiness1 = new Date(gYear,2,1)
      Loftiness1.sunset(longitude, latitude)
      Loftiness1.normalize(now)

  var AyyamiHa1 = new Date(gYear,1,25)
      AyyamiHa1.sunset(longitude, latitude)
      AyyamiHa1.normalize(now)

    // determine last sunset.
  var gDateSunset = new Date(now.getTime())
      gDateSunset.sunset(longitude, latitude)

    // if sun not set, use yesterday's sunset
  if(now < gDateSunset)
   {
    gDateSunset.setDate(gDateSunset.getDate()-1)
    gDateSunset.sunset(longitude, latitude)
   }
  gDateSunset.normalize(now)

    // calculate today
  var afterNawRuz = (gDateSunset >= NawRuz)

    // do what we can with the year
  this.year = gYear - 1844 + afterNawRuz
  this.vahid = 1 + (this.year / 19)
  this.kullishay = 1 + (this.vahid / 19)

    // determine what month and day this is, based on last sunset
  if     (afterNawRuz)              { var sectionStart = NawRuz }
  else if(gDateSunset < AyyamiHa1)  { var sectionStart = lastNawRuz }
  else if(gDateSunset >= Loftiness1){ var sectionStart = Loftiness1 }
  else                              { var sectionStart = AyyamiHa1 }

  if     (sectionStart==Loftiness1) { this.month = 19 }
  else if(sectionStart==AyyamiHa1)  { this.month = 0 }
  else                              { this.month = 1 + (((gDateSunset - sectionStart) * msToDays) / 19) }

  this.day = 1 + (((gDateSunset - sectionStart) * msToDays) % 19)

    // time since sunset
  this.hours = ((now - gDateSunset)) * msToDays * 24
  this.minutes = (this.hours - Math.floor(this.hours)) * 60
  this.seconds = (this.minutes - Math.floor(this.minutes)) * 60

  // truncate all the numbers
  for(e in this)
   {
    if(!isNaN(this[e])) { this[e]=Math.floor(this[e]) }
   }
 }


//----- Misc functions to make using a BahaiDate object easier

BahaiDate.prototype.toString = function()
 {
  return this.year + '/' + this.month + '/' + this.day
 }

BahaiDate.prototype.toValue = function()
 {
  return this.gDateTime.valueOf()
 }


BahaiDate.prototype.monthName = function(lang, variation)
 {
  return bDateNames[validateLang(lang,variation)][this.month]
 }

BahaiDate.prototype.dayName = function(lang, variation)
 {
  if(this.month!=0)
   {
    return bWeekdays[validateLang(lang,variation)][this.day]
   }
  else
   {
    return bAyyamiHa[validateLang(lang,variation)][this.day-1]
   }
 }

BahaiDate.prototype.toStringDateTime = function()
 {
  return this.year + '/' + this.month + '/' + this.day + ' ' + this.hours + ':' + this.minutes
 }

BahaiDate.prototype.toNames = function(lang, variation)
 {
  return this.dayName(lang, variation) + ' of ' + this.monthName(lang, variation) + ', ' + this.year + ' BE'
 }


// Misc date calculations needed

//----- Calculate sunset on a given date
Date.prototype.sunset = function(longitude, latitude)
// get exact sunset time
 {
  // calculate sunset


  // for now, simply assume 6:50pm
  this.setHours(18,50,0,0)
 }


//----- Normalize a date to be in the same timezone as the compareTime. This
//        eliminates daylight savings vs standard time differences
Date.prototype.normalize = function(compareTime)
 {
    // Return sunset time as if this date is in the same timezone as
    //   the compareTime date.

  if(!isNaN(compareTime))   // If no time given, don't bother normalizing
   {
    this.setMinutes(this.getMinutes() + compareTime.getTimezoneOffset() - this.getTimezoneOffset())
   }
 }


// define some constants for converting between milliseconds and days
var daysToMS = 24 * 60 * 60 * 1000
var msToDays = 1 / daysToMS


/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// Language specific settings here. Add lines for other languages!
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// If a language or variation is added, be sure to update the function at the
//   bottom to validate the new language option.
//   code altered by Rick Poolman, Holland for bahaitwente.nl and bahai.be 
//   the only options here are dutch and french

var bDateNames = new Object
bDateNames['taal'+'nl']=['de Schrikkeldagen','Pracht (Bah&aacute;)','Heerlijkheid (Jal&aacute;l)','Schoonheid (Jam&aacute;l)','Grootheid (&#39;Azam&aacute;t)','Licht (N&uacute;r)','Genade (Rahmat)','Woorden (Kalim&aacute;t)','Volmaaktheid (Kam&aacute;l)','Namen (Asm&aacute;&#39;)','Macht (&#39;Izzat)','Wil (Mash&iacute;yyat)','Kennis (&#39;Ilm)','Kracht (Qudrat)','Spraak (Qawl)','Vragen (Mas&aacute;&#39;il)','Eer (Sharaf)','Soevereiniteit (Sult&aacute;n)','Heerschappij (Mulk)','Verhevenheid (&#39;Al&aacute;&#39;)']
bDateNames['taal'+'fr']=['des Jours Intercalaires','Splendeur (Bah&aacute;)','Gloire (Jal&aacute;l)','Beaut&eacute; (Jam&aacute;l)','Grandeur (&#39;Azam&aacute;t)','Lumi&#269;re (N&uacute;r)','Mis&eacute;ricorde (Rahmat)','Paroles (Kalim&aacute;t)','Perfection (Kam&aacute;l)','Noms (Asm&aacute;&#39;)','Puissance (&#39;Izzat)','Volont&eacute; (Mash&iacute;yyat)','Connaissance (&#39;Ilm)','Pouvoir (Qudrat)','Discours (Qawl)','Questions (Mas&aacute;&#39;il)','Honneur (Sharaf)','Souverainet&eacute; (Sult&aacute;n)','Empire (Mulk)','El&eacute;vation (&#39;Al&aacute;&#39;)']
// special characters are replaced by ASCII HTML Codes é = &eacute; | è = &#269; | ì = &#283; | í = &iacute; | ç = &ccedil; | ' =  &#39; | á = &aacute; (if needed)

var bAyyamiHa = new Object
bAyyamiHa['taal'+'nl']=['1','2','3','4','5']
bAyyamiHa['taal'+'fr']=['1','2','3','4','5']
// don't know how this works, wait for the days and see.

function validateLang(lang, variation)
 {                                                                                                                                                                                    
  // validate for each language
  switch (lang)
   {
    case 'taal':  // only one defined
      break
    default:
        // none of the valid options given
        // changed --> lang is always 'taal' --> variation is the language
        // This gives you more options if you want to alter 
      lang = 'taal'
      variation = 'nl'
   }

  return lang + variation
 }
 
// Go back in history:

 function goBack(n){
	if (n>0) n*=-1;
	if (n==0) n = -1;
	history.go(n);
}
//Turn layers on or off:

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}
