// auto_ride_calendar.js - support functions for automatically setting the
// ride calendar links
//
// This file requires /common/js/util/calendar.js; it must be sourced in a
// separate script tag before this one.  (It would be nice to do this
// automatically, but the lazy+simple way uses jquery, and that means we'd
// require another script tag for that instead, so probably not worth it
// unless we're already using jquery on every page, which at the moment we're
// not).

// Returns a date representing sometime on the first day of the next month.
function nextMonth() {
  var d = new Date()
  d.setDate(1);
  d.setMonth(d.getMonth()+1); // automatically handles rollover of year
  return d;
}

// Is this a pizza ride Wednesday?
function isPizzaRideWednesday(d) {
  if (d.getDay() != 3) return false; // 0-based, Sunday == 0, Wednesday == 3
  var month = d.getMonth();          // 0-based; May == 4, September == 8
  return month >= 4 && month <= 8;
}

// Does d represent one of the last n days of the month?
function lastN(d, n) {
  var year = d.getFullYear(); // 4-digit
  var day = d.getDate();      // day of month (1-based)
  var month = d.getMonth();   // 0-based
  var ndays = cal_util.days_in_month[month];
  if (month == 1 && cal_util.isLeapYear(year)) ndays = 29;
  return ndays-day < n;
}

function getIntValueFromTextInput(name) {
  return parseInt(document.getElementsByName(name)[0].value);
}

// Return a string representation of x, truncated to 2 digits, padded with
// leading 0s if necessary
function format2(x) {
  x = x%100;
  return (x < 10 ? '0' : '') + x;
}

function setRideCalendarLink(a, month, year) {
  a.href = '/ride_calendar/' + year + '/' + format2(month+1) + '.pdf';
}

function set_ride_calendar_section(id, alt_id) {
  d = new Date();
  nmd = nextMonth();
  if (lastN(d, 5)) {
    id = alt_id;
  }
  var e = document.getElementById(id);

  // set links
  var a = e.getElementsByTagName('a');
  for (var i = 0; i < a.length; ++i) {
    var anchor = a[i];
    if (anchor.className == 'this_month') {
      setRideCalendarLink(anchor, d.getMonth(), d.getFullYear());
    } else if (anchor.className == 'next_month') {
      setRideCalendarLink(anchor, nmd.getMonth(), nmd.getFullYear());
    }
  }

  // set month names
  var s = e.getElementsByTagName('span');
  for (var i = 0; i < s.length; ++i) {
    var span = s[i];
    if (span.className == 'this_month') {
      span.innerHTML = cal_util.month_name[d.getMonth()];
    } else if (span.className == 'next_month') {
      span.innerHTML = cal_util.month_name[nmd.getMonth()];
    }
  }

  e.style.display = '';
}

// used to set href for main ride calendar link; switches at noon on the last
// day of the month
function set_ride_calendar_link(id) {
  var d = new Date();
  // On the last day of the month, show the next month's calendar starting at
  // noon.
  if (lastN(d, 1)) {
    if (isPizzaRideWednesday(d)) {
      // Exception: if the last day of the month is a Pizza ride Wednesday,
      // don't change the calendar until 8pm.
      if (d.getHours() >= 20) d = nextMonth();
    } else {
      if (d.getHours() >= 12) d = nextMonth();
    }
  }
  var a = document.getElementById(id);
  setRideCalendarLink(a, d.getMonth(), d.getFullYear());
}

