
function figureMoon() {

  // Facts (for 2008-2009)

  var first = new Date(2007,12,9); // First new moon
  var last = new Date(2010,1,15); // Last new moon
  var count = 25; // Number of new moons in between

  // Calculations

  var cycles = count + 1; // Number of lunar cycles
  var timespan = last - first; // Time between first and last (ms)
  var period = timespan / cycles; // Lunar period (ms)

  var today = new Date();
  var y = today.getYear();

  if (y >=2008 && y<=2009) {

    var todspan = today - first; // Time between today and first (ms)
    var todcyc = todspan / period; // Cycles between today and first
    var phase = Math.round((todcyc - Math.floor(todcyc)) * 16);
    if (phase >= 16) {phase = 0;}

    showMoon(phase);

  } else {

    showMoon(0);

  }

}

function showMoon(num) {
  switch(num) {
    case 0:
      document.moon.src="images/moon0.jpg";
      document.moon.alt="New Moon";
      break; 
    case 1:
    case 2:
    case 3:
      document.moon.src="images/moon1.jpg";
      document.moon.alt="Waxing Crescent";
      break;
    case 4:
      document.moon.src="images/moon2.jpg";
      document.moon.alt="First Quarter";
      break;
    case 5:
    case 6:
    case 7:
      document.moon.src="images/moon3.jpg";
      document.moon.alt="Waxing Gibbous";
      break;
    case 8:
      document.moon.src="images/moon4.jpg";
      document.moon.alt="Full moon";
      break;
    case 9:
    case 10:
    case 11:
      document.moon.src="images/moon5.jpg";
      document.moon.alt="Waning Gibbous";
      break;
    case 12:
      document.moon.src="images/moon6.jpg";
      document.moon.alt="Last Quarter";
      break;
    case 13:
    case 14:
    case 15:
      document.moon.src="images/moon7.jpg";
      document.moon.alt="Waning Crescent";
      break;
  }
}