<!--
// Coded by: Golddess
// Version:  3.25 August 3, 2005
// Use:      Displays basic timers for the days of the week and for RSE, Ferry, and Guild schedules.
//           Uses some items from mithratime.js
//
// Feel free to copy and display on your website, but please give credit where credit is due
// by mentioning MithraPride as the source and linking back to http://www.mithrapride.org/
// on the page that displays the clock.
//
// Version Update List:
//   3.25: Split some headers to display on 2 lines.
//         Moved some things over from mithratime.js.
//   3.20: Various tweaks to the functions, hopefully keeping local variables local.
//   3.10: The moon phases are fixed to properly display New/Full moons, as well as fleshed out to display Waxing/Waning Gibbous/Crescent and First/Last Quarter moon.
//   3.00: The MithraPride Clock has been divided into 3 files. This one which handles the day, RSE, Ferry, and Guild tables, mithratimeairship.js which handles just the Airship table, and mithratime.js, which contains constants and functions that both use.
//   2.20: Recoded a few more functions, hopefully making them more efficient, or at least less bulky.
//   2.10: Something got fubar'd. Whatever it was, is now fixed.
//   2.00: The Firefox flashing problem is fixed. (Take that, Pyogenes :P j/k ^^;)
//   1.60: The times for the Airships have been confirmed and added, as well as estimated times of arrival.
//   1.50: Additions...
//          -The ability to filter the days based on elemental type and new/full moons.
//          -Dropdown lists for how many RSE and Ferry schedules you want displayed.
//          -The Marble Bridge Tavern Favortisim is completed.
//   1.20: Several functions have been recoded such that, in my eyes, it is easier to read.
//   1.00: Basically just a copy of the manakun timer, with a few modifications so that it displays the correct time, regardless of your timezone.

//Various variables used in the construction of the page
DayTable    = "";
DayNum      = -1;
DayType     = -1;
RSETable    = "";
RSENum      = -1;
RSERace     = -1;
SMShipTable = "";
SMShipNum   = -1;
VanaDayColor  = new Array("<font color=#DD0000>Firesday</font>", "<font color=#AAAA00>Earthsday</font>", "<font color=#0000DD>Watersday</font>", "<font color=#00AA22>Windsday</font>", "<font color=#7799FF>Iceday</font>", "<font color=#AA00AA>Lightningday</font>", "<font color=#AAAAAA>Lightsday</font>", "<font color=#333333>Darksday</font>");
JeunoTavern   = new Array("Ninjas", "Dragoons", "Summoners", "Humes", "Elvaan", "Tarutaru", "Mithra", "Galka", "Gentlemen", "Ladies", "San d'Oria Citizens", "Bastok Citizens", "Windurst Citizens", "Warriors", "Monks", "White Mages", "Black Mages", "Red Mages", "Thieves", "Paladins", "Dark Knights", "Beastmasters", "Bards", "Rangers", "Samurai");

// RSE Stuff
RSELocation = new Array("Gusgen Mines", "Shakhrami Maze", "Ordelle Caves");
RSEDelay    = 691200;

function printTime() {
  // The current Earth time
  Now = new Date();

  // Calculates the current Earth time in milliseconds and the Vana'diel time in seconds
  eTime = Now.getTime();
  vTime = (eTime + Diff) * VanaToEarth;

  // Formats the current Earth and Vana'diel Time for display
  EarthTxt = CalculateEarthTime(Now);
  VanaTxt  = CalculateVanaTime(vTime);

  // Calculates the Conquest update
  ConqLimit = ConqUpdate(vTime);

  // # of Vana'diel seconds into the new Vana'diel day  
  VDSiD  = (vTime - ((Math.floor(vTime / (HourPDay * MinPHour * SecPMin))) * HourPDay * MinPHour * SecPMin));



  /////////////////////////////////////////////////////////////////////////////
  // Day Time Table                                                          //
  /////////////////////////////////////////////////////////////////////////////
  vTempTime1 = Math.floor( vTime            / (SecPMin * MinPHour * HourPDay)) * (SecPMin * MinPHour * HourPDay);
  vTempTime2 = Math.floor((vTime - SecPMin) / (SecPMin * MinPHour * HourPDay)) * (SecPMin * MinPHour * HourPDay);

  if ((eval(document.MithraTimer.DayNumBox.value) != 0 || DayNum != 0) && (DayNum != eval(document.MithraTimer.DayNumBox.value) || DayType != eval(document.MithraTimer.DayTypeBox.value) || vTempTime1 != vTempTime2)) {
    DayNum   = eval(document.MithraTimer.DayNumBox.value);
    DayType  = eval(document.MithraTimer.DayTypeBox.value);
    DayTable = "";
    if (DayNum != 0) {
      DayTable = DayTimeTable(DayNum, DayTable, DayType);
    }

    document.getElementById("DayTableBox").innerHTML = DayTable;
  }
  /////////////////////////////////////////////////////////////////////////////
  // Day Time Table                                                          //
  /////////////////////////////////////////////////////////////////////////////



  /////////////////////////////////////////////////////////////////////////////
  // RSE Time Table                                                          //
  /////////////////////////////////////////////////////////////////////////////
  vTempTime1 = (Math.floor( vTime            / (8 * RSEDelay)) * (8 * RSEDelay)) + (RSEDelay * 2);
  vTempTime2 = (Math.floor((vTime - SecPMin) / (8 * RSEDelay)) * (8 * RSEDelay)) + (RSEDelay * 2);

  if ((eval(document.MithraTimer.RSENumBox.value) != 0 || RSENum != 0) && (RSENum != eval(document.MithraTimer.RSENumBox.value) || RSERace != eval(document.MithraTimer.RSERaceBox.value) || vTempTime1 != vTempTime2)) {
    RSENum   = eval(document.MithraTimer.RSENumBox.value);
    RSERace  = eval(document.MithraTimer.RSERaceBox.value);
    RSETable = "";
    if (RSENum != 0) {
      RSETable = RSETimeTable(RSENum, RSETable, RSERace);
    }

    document.getElementById("RSETableBox").innerHTML = RSETable;
  }
  /////////////////////////////////////////////////////////////////////////////
  // RSE Time Table                                                          //
  /////////////////////////////////////////////////////////////////////////////



  /////////////////////////////////////////////////////////////////////////////
  // Selbina/Mhaura Ship Schedule                                            //
  /////////////////////////////////////////////////////////////////////////////
  vTempTime1 = (Math.floor( vTime            / SelbinaMhaura) * SelbinaMhaura) + SelbinaMhaura;
  vTempTime2 = (Math.floor((vTime - SecPMin) / SelbinaMhaura) * SelbinaMhaura) + SelbinaMhaura;

  vTempTimeA = (Math.floor(vTime / SelbinaMhaura) * SelbinaMhaura) + SelbinaMhaura;
  vTempTimeB = vTempTimeA + (SMShipNum * SelbinaMhaura);

  if ((eval(document.MithraTimer.SMShipNumBox.value) != 0 || SMShipNum != 0) && (SMShipNum != eval(document.MithraTimer.SMShipNumBox.value) || vTempTime1 != vTempTime2)) {
    SMShipNum   = eval(document.MithraTimer.SMShipNumBox.value);
    SMShipTable = "";

    if (SMShipNum != 0) {
      SMShipTable  = "<table border=0 cellpadding=2 cellspacing=0 width=100%>";
      SMShipTable += "<tr><td>Vana'Diel Time</td><td>Earth Time</td><td>Delay</td></tr>";

      for (i = 0; i < SMShipNum; i++) {
        SMShipTable  += "<tr>"
        SMShipTable  += "<td><small>" + CalculateVanaTime2(vTempTimeA) + "</td>";
        SMShipTable  += "<td><small>" + CalculateEarthTime2(new Date(Math.floor(vTempTimeA / VanaToEarth) - Diff)) + "</td>";
        SMShipTable  += "<td><small><span id=" + '"' + "SMShipTable" + (i + 1) + "Box" + '"' + ">&nbsp;</span></td>";
        SMShipTable  += "</tr>"
        vTempTimeA   += SelbinaMhaura;
      }
      SMShipTable += "</table>";
    }
    document.getElementById("SMShipTableBox").innerHTML = SMShipTable;
  }

  switch (SMShipNum)
  {
    case 10: {
      vTempTimeB -= SelbinaMhaura;
      document.getElementById("SMShipTable10Box").innerHTML = CalculateDelay(vTempTimeB - vTime);
    }
    case  9: {
      vTempTimeB -= SelbinaMhaura;
      document.getElementById("SMShipTable9Box" ).innerHTML = CalculateDelay(vTempTimeB - vTime);
    }
    case  8: {
      vTempTimeB -= SelbinaMhaura;
      document.getElementById("SMShipTable8Box" ).innerHTML = CalculateDelay(vTempTimeB - vTime);
    }
    case  7: {
      vTempTimeB -= SelbinaMhaura;
      document.getElementById("SMShipTable7Box" ).innerHTML = CalculateDelay(vTempTimeB - vTime);
    }
    case  6: {
      vTempTimeB -= SelbinaMhaura;
      document.getElementById("SMShipTable6Box" ).innerHTML = CalculateDelay(vTempTimeB - vTime);
    }
    case  5: {
      vTempTimeB -= SelbinaMhaura;
      document.getElementById("SMShipTable5Box" ).innerHTML = CalculateDelay(vTempTimeB - vTime);
    }
    case  4: {
      vTempTimeB -= SelbinaMhaura;
      document.getElementById("SMShipTable4Box" ).innerHTML = CalculateDelay(vTempTimeB - vTime);
    }
    case  3: {
      vTempTimeB -= SelbinaMhaura;
      document.getElementById("SMShipTable3Box" ).innerHTML = CalculateDelay(vTempTimeB - vTime);
    }
    case  2: {
      vTempTimeB -= SelbinaMhaura;
      document.getElementById("SMShipTable2Box" ).innerHTML = CalculateDelay(vTempTimeB - vTime);
    }
    case  1: {
      vTempTimeB -= SelbinaMhaura;
      document.getElementById("SMShipTable1Box" ).innerHTML = CalculateDelay(vTempTimeB - vTime);
    }
    default: {
      break;
    }
  }
  /////////////////////////////////////////////////////////////////////////////
  // Selbina/Mhaura Ship Schedule                                            //
  /////////////////////////////////////////////////////////////////////////////



  /////////////////////////////////////////////////////////////////////////////
  // Guilds Time Table                                                       //
  /////////////////////////////////////////////////////////////////////////////
  Alc = "<font class=holiday>Guild Holiday</font> <font class=closed>(CLOSED)</font>";
  Bla = "<font class=holiday>Guild Holiday</font> <font class=closed>(CLOSED)</font>";
  Bon = "<font class=holiday>Guild Holiday</font> <font class=closed>(CLOSED)</font>";
  Gol = "<font class=holiday>Guild Holiday</font> <font class=closed>(CLOSED)</font>";
  Wea = "<font class=holiday>Guild Holiday</font> <font class=closed>(CLOSED)</font>";
  Car = "<font class=holiday>Guild Holiday</font> <font class=closed>(CLOSED)</font>";
  Tan = "<font class=holiday>Guild Holiday</font> <font class=closed>(CLOSED)</font>";
  Cul = "<font class=holiday>Guild Holiday</font> <font class=closed>(CLOSED)</font>";
  Fis = "<font class=holiday>Guild Holiday</font> <font class=closed>(CLOSED)</font>";

  Alc = CalculateGuildOpen("6", "480", "1380", "1920", Alc, VDSiD);  // Alchemy
  Bla = CalculateGuildOpen("2", "480", "1380", "1920", Bla, VDSiD);  // BlackSmith
  Bon = CalculateGuildOpen("3", "480", "1380", "1920", Bon, VDSiD);  // Bonework
  Gol = CalculateGuildOpen("4", "480", "1380", "1920", Gol, VDSiD);  // GoldSmith
  Wea = CalculateGuildOpen("0", "360", "1260", "1800", Wea, VDSiD);  // Weaver
  Car = CalculateGuildOpen("0", "360", "1260", "1800", Car, VDSiD);  // Carpentry
  Tan = CalculateGuildOpen("4", "180", "1080", "1620", Tan, VDSiD);  // Leatherwork
  Cul = CalculateGuildOpen("7", "300", "1200", "1740", Cul, VDSiD);  // Culinary
  Fis = CalculateGuildOpen("5", "180", "1080", "1620", Fis, VDSiD);  // Fishing

  //bt  = "<font class=holiday>Guild Holiday</font> <font class=closed>(CLOSED)</font>";
  //jt  = "<font class=holiday>Guild Holiday</font> <font class=closed>(CLOSED)</font>";
  //nt  = "<font class=holiday>Guild Holiday</font> <font class=closed>(CLOSED)</font>";
  //if (vDay == 1 ) { bt  = "CLOSE"; }
  //if (vDay == 3 ) { jt  = "CLOSE"; }
  //if (vDay == 7 ) { nt  = "CLOSE"; }
  /////////////////////////////////////////////////////////////////////////////
  // Guilds Time Table                                                       //
  /////////////////////////////////////////////////////////////////////////////



  /////////////////////////////////////////////////////////////////////////////
  // Assigns all other values to the webpage                                 //
  /////////////////////////////////////////////////////////////////////////////
  //document.Times.jt.value        = jt;
  //document.Times.bt.value        = bt;
  //document.Times.nt.value        = nt;
  document.getElementById("VanaBox"       ).innerHTML = VanaTxt;
  document.getElementById("EarthBox"      ).innerHTML = EarthTxt;
  document.getElementById("ConqLimitBox"  ).innerHTML = ConqLimit;

  document.getElementById("AlcBox"        ).innerHTML = Alc;
  document.getElementById("BlaBox"        ).innerHTML = Bla;
  document.getElementById("BonBox"        ).innerHTML = Bon;
  document.getElementById("GolBox"        ).innerHTML = Gol;
  document.getElementById("WeaBox"        ).innerHTML = Wea;
  document.getElementById("CarBox"        ).innerHTML = Car;
  document.getElementById("TanBox"        ).innerHTML = Tan;
  document.getElementById("CulBox"        ).innerHTML = Cul;
  document.getElementById("FisBox"        ).innerHTML = Fis;
  /////////////////////////////////////////////////////////////////////////////
  // Assigns all values to the webpage                                       //
  /////////////////////////////////////////////////////////////////////////////



  setTimeout("printTime()", 1000);
}



function CalculateGuildOpen(ClosedDay, OpenTime, ClosedTime, NewOpenTime, GuildStatus, VDSiD) {
  var vDay = Math.floor((vTime / (HourPDay * MinPHour * SecPMin)) % DayPVWeek);

  if (vDay != eval(ClosedDay)) {
    if ((VDSiD / SecPMin) > eval(ClosedTime)) {
      if (((vDay + 1) % DayPVWeek) != eval(ClosedDay)) {
        GuildStatus = "<font class=closed>CLOSED</font> (Opening in " + CalculateDelay((eval(NewOpenTime) * SecPMin) - VDSiD) + ")";
      }
      else {
        GuildStatus = "<font class=closed>CLOSED</font> (Guild Holiday Tomorrow)";
      }
    }
    else if ((VDSiD / SecPMin) > eval(OpenTime)) {
      GuildStatus = "<font class=open>Open</font> (Closing in " + CalculateDelay((eval(ClosedTime) * SecPMin) - VDSiD) + ")";
    }
    else if ((VDSiD / SecPMin) < eval(OpenTime)) {
      GuildStatus = "<font class=closed>CLOSED</font> (Opening in " + CalculateDelay((eval(OpenTime) * SecPMin) - VDSiD) + ")";
    }
  }
  return GuildStatus;
}



function CalculateMoonPhase(CurrentTime) {
  var vMoon = Math.round(((((Math.floor(CurrentTime / (HourPDay * MinPHour * SecPMin)) + 26) % MoonPhaseMod) - (MoonPhaseMod / 2)) / (MoonPhaseMod / 2)) * 100);

  if (vMoon >= -93 && vMoon <= -62) {
    vMoon = "<font color=#4400BB>" + Math.abs(vMoon) + "% (Waning Gibbous)</font>";
  }
  else if (vMoon >= -43 && vMoon <= -12) {
    vMoon = "<font color=#BB0044>" + Math.abs(vMoon) + "% (Waning Crescent)</font>";
  }
  else if (vMoon >= 7 && vMoon <= 38) {
    vMoon = "<font color=#BB0044>" + Math.abs(vMoon) + "% (Waxing Crescent)</font>";
  }
  else if (vMoon >= 57 && vMoon <= 88) {
    vMoon = "<font color=#4400BB>" + Math.abs(vMoon) + "% (Waxing Gibbous)</font>";
  }

  else if (vMoon >= 90 || vMoon <= -95) {
    vMoon = "<font color=#0000FF>" + Math.abs(vMoon) + "% (Full Moon)</font>";
  }
  else if (vMoon >= -60 && vMoon <= -45) {
    vMoon = "<font color=#770088>" + Math.abs(vMoon) + "% (Last Quarter Moon)</font>";
  }
  else if (vMoon >= 40 && vMoon <= 55) {
    vMoon = "<font color=#880077>" + Math.abs(vMoon) + "% (First Quarter Moon)</font>";
  }
  else {
    vMoon = "<font color=#FF0000>" + Math.abs(vMoon) + "% (New Moon)</font>";
  }

  return vMoon;
}



function DayTimeTable(DayNum, DayTable, DayType) {
  var i          = 0;
  var DayMod     = 0;
  var vMoonCheck = 0;
  var vMoonTemp  = 0;
  var MoonMod    = 0;
  var vTempTime  = Math.floor(vTime / (SecPMin * MinPHour * HourPDay)) * (SecPMin * MinPHour * HourPDay);
  var vTempDay   = 0;
  var vTavern    = 0;
  var vMoon      = 0;

  DayTable  = "<table border=0 cellpadding=2 cellspacing=0 width=100%>";
  DayTable += "<tr><td valign=bottom>Starting</td><td valign=bottom>Ending</td><td valign=bottom>Day Type</td><td valign=bottom>Moon Phase</td><td valign=bottom>Marble Bridge<br />Favoritism</td></tr>";

  if (DayType < 8) {
    if (DayType < Math.floor((vTempTime / (HourPDay * MinPHour * SecPMin)) % DayPVWeek)) {
      vTempTime = vTempTime + (SecPMin * MinPHour * HourPDay * (DayPVWeek - 1));
    }
    vTempTime  = vTempTime - (Math.floor((vTempTime / (HourPDay * MinPHour * SecPMin)) % DayPVWeek) * (SecPMin * MinPHour * HourPDay));
    vTempTime  = vTempTime + (DayType * (SecPMin * MinPHour * HourPDay));
    DayMod     = DayPVWeek - 1;
    vMoonCheck = 0;
  }
  else if (DayType == 11) {
    vMoonTemp  = (((Math.floor(vTempTime / (HourPDay * MinPHour * SecPMin)) + 26) % MoonPhaseMod) - (MoonPhaseMod / 2));
    DayMod     = 0;
    vMoonCheck = 7;
    if (vMoonTemp < -4 || vMoonTemp > 2) {
      MoonMod = (MoonPhaseMod) - (vMoonTemp + 4);
      if (vMoonTemp < -4) {
        MoonMod -= MoonPhaseMod;
      }
      vTempTime = vTempTime + (MoonMod * HourPDay * MinPHour * SecPMin);
    }
    else {
      vMoonCheck = 3 - vMoonTemp;
    }
  }
  else if (DayType == 12) {
    vMoonTemp  = (((Math.floor(vTempTime / (HourPDay * MinPHour * SecPMin)) + 26) % MoonPhaseMod) - (MoonPhaseMod / 2));
    DayMod     = 0;
    vMoonCheck = 7;
    if (vMoonTemp > -40 && vMoonTemp < 38) {
      MoonMod = (MoonPhaseMod) - (vMoonTemp + 46);
      vTempTime = vTempTime + (MoonMod * HourPDay * MinPHour * SecPMin);
    }
    else {
      vMoonCheck = Math.abs((((vMoonTemp - 38) + MoonPhaseMod) % MoonPhaseMod) - 7);
    }
  }
  else {
    DayMod     = 0;
    vMoonCheck = 0;
  }

  do {
    do {
      vTempDay  = Math.floor(vTempTime / (HourPDay * MinPHour * SecPMin)) % DayPVWeek;
      vTavern   = Math.floor(vTempTime / (HourPDay * MinPHour * SecPMin)) % TavernMod;
      vMoon     = CalculateMoonPhase(vTempTime);

      DayTable += "<tr>"
      DayTable += "<td><small>" + CalculateEarthTime2(new Date(Math.floor(vTempTime / VanaToEarth) - Diff)) + "</td>";

      vTempTime = vTempTime + (SecPMin * MinPHour * HourPDay);
      DayTable += "<td><small>" + CalculateEarthTime2(new Date(Math.floor(vTempTime / VanaToEarth) - Diff)) + "</td>";
      DayTable += "<td><small>" + VanaDayColor[vTempDay] + "</td>";
      DayTable += "<td><small>" + vMoon + "</td>";
      DayTable += "<td><small>" + JeunoTavern[vTavern] + "</td>";
      DayTable += "</tr>"
      vTempTime = vTempTime + (SecPMin * MinPHour * HourPDay * DayMod);

      i++;
      vMoonCheck--;
    } while (i < DayNum && vMoonCheck != 0);
    vMoonCheck = 7;
    vTempTime = vTempTime + (77 * HourPDay * MinPHour * SecPMin);
  } while (i < DayNum);

  DayTable += "</table>";

  return DayTable;
}



function RSETimeTable(RSENum, RSETable, RSERace) {
  var i       = 0;
  var RSEMod  = (Math.floor(vTime / (8 * RSEDelay)) * (8 * RSEDelay));
  RSEMod     += (RSERace * RSEDelay);

  RSETable  = "<table border=0 cellpadding=2 cellspacing=0 width=100%>";
  RSETable += "<tr><td>Starting</td><td>Ending</td><td>Location</td></tr>";

  if (vTime > (RSEMod + RSEDelay)) {
    RSEMod   += (8 * RSEDelay);
  }

  for (i = 0; i < RSENum; i++) {
    RSETable += "<tr>"

    RSETable += "<td><small>" + CalculateEarthTime2(new Date(Math.floor(RSEMod / VanaToEarth) - Diff)) + "</small></td>";
    RSEMod   += RSEDelay;

    RSETable += "<td><small>" + CalculateEarthTime2(new Date(Math.floor(RSEMod / VanaToEarth) - Diff)) + "</small></td>";
    RSEMod   += (7 * RSEDelay);

    RSETable += "<td><small>" + RSELocation[(Math.floor(RSEMod / RSEDelay) % 3)] + "</small></td>";
    RSETable += "</tr>"
  }

  RSETable += "</table>";

  return RSETable;
}
// -->
