<!--
////////////////////////////////////////////////////////
// Copyright 2003, Timothy James Forsythe, all rights reserved.
// Permission granted to use, copy, modify, and distribute so long as
// the above copyright and this permission statement are retained in all
// copies.  THERE IS NO WARRANTY - USE AT YOUR OWN RISK.
////////////////////////////////////////////////////////

// Globals
var MAYAN_CORRELATION  = 584285;
var MAYAN2_CORRELATION = 584283;
//var MAYAN_CYCLE       = 1872000;
var MAYAN_CYCLE       = 2880000;

function MayanDate(kin,unial,tun,katun,baktun,tzolkinDay,tzolkinWeek,haabDay,haabMonth)
{
  this.kin 	= kin;
  this.unial 	= unial;
  this.tun 	= tun;
  this.katun 	= katun;
  this.baktun	= baktun

  this.tzolkinDay  = tzolkinDay;
  this.tzolkinWeek = tzolkinWeek;
  this.haabDay     = haabDay;
  this.haabMonth   = haabMonth;
}

function MayanDays(tzolkinDay,tzolkinWeek,haabDay,haabMonth)
{
  this.tzolkinDay  = tzolkinDay;
  this.tzolkinWeek = tzolkinWeek;
  this.haabDay     = haabDay;
  this.haabMonth   = haabMonth;
}

function MayanToJDN(kin,unial,tun,katun,baktun,cfactor)
{
  var jdn;
  
  if (cfactor)
  {
    jdn = MAYAN_CORRELATION;
  }
  else
  {
    jdn = MAYAN2_CORRELATION;
  }
  
  jdn += baktun * 144000;
  jdn += katun  * 7200;
  jdn += tun    * 360;
  jdn += unial  * 20;
  jdn += kin;
  
  return jdn;
}

function JDNToMayan(jdn,cfactor)
{
  var lc = new MayanDate(0,0,0,0,0,0,0,0,0);
  var count;

  if (cfactor)
  {
    // get number of days since 0.0.0.0.0 4 Ahau 8 Cumhu
    count = jdn - MAYAN_CORRELATION;
  }
  else
  {
    count = jdn - MAYAN2_CORRELATION;
  }

  while (count < 0)
  {
    // if the date is before -3114 AUG 13, then readjust
    count += MAYAN_CYCLE;
  }

  while (count >= MAYAN_CYCLE)
  {
    // if the date is after 2012 DEC 22, then readjust
    count -= MAYAN_CYCLE;
  }

  lc.baktun = Math.floor(count/144000);
  count -= (lc.baktun * 144000);
  lc.katun = Math.floor(count/7200);
  count -= (lc.katun * 7200);
  lc.tun = Math.floor(count/360);
  count -= (lc.tun * 360);
  lc.unial = Math.floor(count/20);
  count -= (lc.unial * 20);
  lc.kin = count;

  var days = JDNToMayanDays(jdn,cfactor);
  lc.tzolkinDay  = days.tzolkinDay;  
  lc.tzolkinWeek = days.tzolkinWeek;  
  lc.haabDay     = days.haabDay;  
  lc.haabMonth   = days.haabMonth;  
  
  return lc;
}


function JDNToMayanDays(jdn,cfactor)
{
  // The Tzolkin Week starts at "4 Ahau", where "4" is the Tzlokin Day,
  // and "Ahau" is the Tzolkin WeekDay.  The algorithm cycles both
  // the Tzolkin Day from 1-13 and the Tzolkin Week Day from 0-19.
  var TZOLKIN_WEEK_LENGTH = 20; // 0-19
  var TZOLKIN_DAY_LENGTH  = 13; // 1-13
  var TZOLKIN_YEAR_LENGTH = (TZOLKIN_DAY_LENGTH * TZOLKIN_WEEK_LENGTH);
  var TZOLKIN_WEEK_OFFSET = 19; // Ahau
  var TZOLKIN_DAY_OFFSET  =  4; // 4 Ahau

  // The Haab Month starts at "8 Cumhu", where "8" is the Haab Day, and
  // "Cumhu" is the Haab Month.  The algorithm increments the Haab Day
  // from 0-19, where it then resets it to 0 and then increments the
  // Haab Month. The Haab Month will cycle from 0-18.  When the Haab
  // Month is 18, the Haab Day will only count to 4 before resetting.
  var HAAB_MONTH_LENGTH =  18; // 0-17
  var HAAB_DAY_LENGTH   =  20; // 0-19
  var HAAB_UAYEB_LENGTH =   5; // 0-4
  var HAAB_YEAR_LENGTH  = (HAAB_DAY_LENGTH * HAAB_MONTH_LENGTH);
  var HAAB_FYEAR_LENGTH = (HAAB_YEAR_LENGTH + HAAB_UAYEB_LENGTH);
  var HAAB_MONTH_OFFSET = 348; // 8 Cumhu

  var TZOLKIN = new Array(
  "Imix",
  "Ik",		// Ik'
  "Akbal",	// Ak'bal
  "Kan",	// K'an
  "Chicchan",	// Chikchan
  "Cimi",	// Kimi
  "Manik'",
  "Lamat",
  "Muluc",	// Muluk
  "Oc",		// Ok
  "Chuen",	// Chuwen
  "Eb",
  "Ben",
  "Ix",
  "Men",
  "Cib",	// Kib
  "Caban",	// Kaban
  "Eznab",	// Etz'nab
  "Cauac",	// Kawak
  "Ahau");	// Ahaw

  var TZOLKIN_E = new Array(// AZTEC	OTHER
  "alligator",  	// "alligator"  waterlilly, crocodile, water, wine, sea dragon, dragon
  "wind",        	// "wind"       breath, air, life
  "night",		// "house"      darkness
  "lizard",		// "lizard"     corn, seed
  "snake",       	// "snake"      serpent
  "death",  		// "death"      death head
  "hunter",      	// "deer"       hand, grasp
  "rabbit",     	// "rabbit"     Venus, star
  "water",       	// "water"      rain, moon
  "dog",		// "dog"
  "frog",        	// "monkey"
  "road",       	// "grass"      skull, tooth, broom, human, stairway, road
  "corn",  		// "reed"       corn stalk, skywalker
  "jaguar",      	// "jaguar"     wizard, magician
  "eagle",       	// "eagle"      bird, wise-man, knower
  "vulture",      	// "vulture"    shell, owl, warrior, wax
  "earthquake",  	// "earthquake" force, earth, incense
  "knife",       	// "knife"      flint, mirror
  "storm", 		// "rain"
  "flower");		// "flower"     sun, lord

  var HAAB = new Array(
  "Pop",	// Pohp
  "Uo",		// Wo
  "Zip",	// Sip
  "Zotz'",	// Sotz'
  "Tzec",	// Sek
  "Xul",
  "Yaxkin",	// Yaxk'in
  "Mol",
  "Chen",	// Ch'en
  "Yax",
  "Sac",	// Zak
  "Ceh",	// Keh
  "Mac",	// Mak
  "Kankin",	// K'ank'in
  "Muan",	// Muwan
  "Pax",
  "Kayab",	// K'ayab
  "Cumhu",	// Kumhk'u
  "Uayeb");

  var HAAB_E = new Array(
  "mat",				// chief
  "frog",				// night jaguar
  "stag",				// cloud serpent
  "bat",
  "skull",				// sky earth
  "dog",	// termination
  "new sun",	// red			// first sun
  "water",	// to gather		// collection
  "black",  	// a well		// cave of the moon
  "green", 	// ?			// new
  "white", 	// ?			// frog
  "red",   	// a forest		// red deer
  "cover",				// enclosure
  "yellow",	// skeleton, ribs	// dog of the underworld
  "owl",	// falcon		// bird
  "drum",				// great puma
  "turtle",
  "dark",				// underworld dragon
  "ghost");				// poisoned

  var mdays = new MayanDays(0,0,0,0);

  var count;
  var haabCount;
  var tzolkinWeekIndex;
  var haabMonthIndex;

  if (cfactor)
  {
    // get number of days since 0.0.0.0.0 4 Ahau 8 Cumhu
    count = jdn - MAYAN_CORRELATION;
  }
  else
  {
    count = jdn - MAYAN2_CORRELATION;
  }

  while (count < 0)
  {
    // if the date is before -3114 AUG 13, then readjust
    count += MAYAN_CYCLE;
  }

  while (count >= MAYAN_CYCLE)
  {
    // if the date is after 2012 DEC 22, then readjust
    count -= MAYAN_CYCLE;
  }

  mdays.tzolkinDay = ((count+TZOLKIN_DAY_OFFSET) % TZOLKIN_YEAR_LENGTH) % TZOLKIN_DAY_LENGTH;
  if (mdays.tzolkinDay == 0)
  {
    mdays.tzolkinDay = TZOLKIN_DAY_LENGTH;
  }

  tzolkinWeekIndex = ((count+TZOLKIN_WEEK_OFFSET) % TZOLKIN_YEAR_LENGTH) % TZOLKIN_WEEK_LENGTH;

  haabCount = (count+HAAB_MONTH_OFFSET) % HAAB_FYEAR_LENGTH;
  if (haabCount < HAAB_YEAR_LENGTH)
  {
    mdays.haabDay   = haabCount % HAAB_DAY_LENGTH;
    haabMonthIndex = Math.floor(haabCount / HAAB_DAY_LENGTH) % HAAB_MONTH_LENGTH;
  }
  else
  {
    // special case for last 5 days of the year
    mdays.haabDay   = haabCount - HAAB_YEAR_LENGTH;
    haabMonthIndex = HAAB_MONTH_LENGTH;
  }

//  mdays.tzolkinWeek = TZOLKIN[tzolkinWeekIndex] + " (" + TZOLKIN_E[tzolkinWeekIndex] + ")";
  mdays.tzolkinWeek = TZOLKIN[tzolkinWeekIndex];
  
//  mdays.haabMonth = HAAB[haabMonthIndex] + " (" + HAAB_E[haabMonthIndex] + ")";
  mdays.haabMonth = HAAB[haabMonthIndex];

  return mdays;
}

-->
