
// cf. /home/wt/perllib/EclipseList.pm

function _date(yr, mo, day, hr, min) {
  return new Date(Date.UTC(yr, mo - 1, day, hr, min));
}

//var eclipse_static_path  = 'http://s3.amazonaws.com/heywtwww1/planisphere';
var eclipse_static_path  = 'http://www.heywhatsthat.com/kml';
var eclipse_dynamic_path = 'http://www.heywhatsthat.com/bin/planisphere.kmz';

var fmonths = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

function eclipse__url(e, max, tz, id, lat, lon) {
  var o = new Object;

  if (max && !eclipse_has_max(e))
    return null;

  o.eclipse_type  = e.type;

  o.n             = e.n? e.n : 40;
  o.interval      = e.interval? e.interval : e.solar? 360 : 600;
  o.time          = e.date.getTime()/1000 - o.n * o.interval / 2;

  if (id)
    o.id          = id;

  if (max) {
    lat           = e.max_lat;
    lon           = e.max_lon;
  }

  if (lat != null && lon != null) {
    o.lat         = lat;
    o.lon         = lon;
  }

  if (tz)
    o.tz          = tz;

  if (max)
    o.eclipse_max = e.max_location;

  if (e.solar) {
    o.flyto       = 'Sun';
    o.flytofov    = 5;
    o.nophases    = 1;
  } else {
    o.flyto       = 'Earth shadow';
    o.flytofov    = 10;
  }

  var s = '';
  for (var k in o) {
    s += '&' + k + '=' + encodeURIComponent(o[k]);
  }
  return eclipse_dynamic_path + '?' + s.substr(1);
}

function eclipse_url(e, tz, id, lat, lon) {
  return eclipse__url(e, 0, tz, id, lat, lon);
}

//function eclipse_url_max_dynamic(e, tz, id, lat, lon) {
//  return eclipse__url(e, 1, tz, id, lat, lon);
//}

function eclipse_url_max_static(e) {
  return eclipse_static_path + '/' + eclipse_ymd(e) + '-solar-eclipse-at-maximum.kmz';
}


function eclipse_has_max(e) {
  return e.solar && e.max_location && e.max_lat != '' && e.max_lon != ''? 1 : 0;
}

function eclipse_ymd(e) {
  return '' + e.year + twodigits(e.date.getUTCMonth() + 1) + twodigits(e.date.getUTCDate());
}


function _name(e) {
  return '&nbsp;' + e.type.replace(/ /g, '&nbsp;') + '&nbsp;eclipse';
}

function eclipse_name_m(e) {
  return fmonths[e.date.getUTCMonth()] + _name(e);
}

function eclipse_name_md(e) {
  return fmonths[e.date.getUTCMonth()] + '&nbsp;' + e.date.getUTCDate() + _name(e);
}

function eclipse_name_mdy(e) {
  return fmonths[e.date.getUTCMonth()] + '&nbsp;' + e.date.getUTCDate() + ',&nbsp;' + e.date.getUTCFullYear() + _name(e);
}

function eclipse_name_mdy_non_html(e) {
  return eclipse_name_mdy(e).replace(/&nbsp;/g, ' ');
}

function eclipse_name_mo(e) {
  var d = e.date.getUTCDate();
  return   fmonths[e.date.getUTCMonth()] + '&nbsp;'
         + d
	 + (d >= 4 && d <= 20? 'th' : d % 10 == 1? 'st' : d % 10 == 2? 'nd' : d % 10 == 3? 'rd' : 'th')
         + _name(e);
}

function eclipse_name_mo_non_html(e) {
  return eclipse_name_mo(e).replace(/&nbsp;/g, ' ');
}


function capitalize_all(s) {
  var ss = '';
  s.split('&nbsp;')._foreach(function(t) { ss += '&nbsp;' + t.substr(0,1).toUpperCase() + t.substr(1); });
  return ss.substr(6);
}

/******
sub name_mdo {  //e.g. January 15th, 2010
    _name "%B %o", @_;
}
*******/


function eclipse_after(after, just_solar) {
		// expect eclipses[] to be sorted by date
  for (i = 0; i < eclipses.length && (eclipses[i].time < after || (just_solar && !eclipses[i].solar)); i++) ;
  if (i >= eclipses.length)
    return null;

/****
		// jump ahead to the one with the max
  var e = eclipses[i];
  if (   eclipses[i].solar
      && !eclipses[i].eclipse_max
      && i < eclipses.length - 1
      && eclipses[i+1].time == eclipses[i + 1].time)
    i++;
****/
  return eclipses[i];
}



var eclipses = [

//{ type: 'lunar', solar: 0, year: 2008, date: _date(2008, 2, 21, 3, 26) },

{ type: 'annular solar', solar: 1, year: 2009, date: _date(2009,  1, 26, 7, 59),
  max_location: 'in the Atlantic Ocean', max_lat: -(34 + 4.2/60), max_lon: 70 + 14/60 },
{ type: 'penumbral lunar', solar: 0, year: 2009, date: _date(2009,  2,  9, 14, 38) },
{ type: 'penumbral lunar', solar: 0, year: 2009, date: _date(2009,  7,  7, 9, 38), details: '(not visible to naked eye)' },
{ type: 'total solar', solar: 1, year: 2009, date: _date(2009,  7, 22, 2, 35),
  max_location: 'in the South Pacific', max_lat: 24 + 12.6/60, max_lon: 144 + 6.4/60 },
{ type: 'penumbral lunar', solar: 0, year: 2009, date: _date(2009,  8,  6, 0, 39), details: '(not visible to naked eye)' },
{ type: 'partial lunar', solar: 0, year: 2009, date: _date(2009, 12, 31, 19, 23) },

{ type: 'annular solar', solar: 1, year: 2010, date: _date(2010, 1, 15, 7, 7), n: 50,
  max_location: 'in the Indian Ocean', max_lat: (1 + 37.4/60), max_lon: 69 + 17.4/60 },
{ type: 'partial lunar', solar: 0, year: 2010, date: _date(2010, 6, 26, 11, 38) },
{ type: 'total solar', solar: 1, year: 2010, date: _date(2010,  7, 11, 19, 33),
  max_location: 'in the South Pacific', max_lat: -(19 + 44.9/60), max_lon: -(121 + 52.5/60) },
{ type: 'lunar', solar: 0, year: 2010, date: _date(2010, 12, 21, 8, 17) },

	// 2011 solar eclipses are all partial, so no eclipse_max
{ type: 'partial solar', solar: 1, year: 2011, date: _date(2011,1,4,8,51) },
{ type: 'partial solar', solar: 1, year: 2011, date: _date(2011,6,1,21,16) },
{ type: 'lunar', solar: 0, year: 2011, date: _date(2011,6,15,20,13) },
{ type: 'partial solar', solar: 1, year: 2011, date: _date(2011,7,1,8,38) },
{ type: 'partial solar', solar: 1, year: 2011, date: _date(2011,11,25,6,20) },
{ type: 'lunar', solar: 0, year: 2011, date: _date(2011,12,10,14,32) },

{ type: 'annular solar', solar: 1, year: 2012, date: _date(2012,5,20,23,53),
  max_location: 'in the Bering Sea', max_lat: (49 + 5.3/60), max_lon: (176 + 16.8/60) },
{ type: 'partial lunar', solar: 0, year: 2012, date: _date(2012,6,4,11,3) },
{ type: 'total solar', solar: 1, year: 2012, date: _date(2012,11,13,22,12),
  max_location: 'in the South Pacific', max_lat: -(39 + 56.9/60), max_lon: -(161 + 19.8/60) },
{ type: 'penumbral lunar', solar: 0, year: 2012, date: _date(2012,12,28,14,33) }

];



for (var i = 0; i < eclipses.length; i++) {
  eclipses[i].time = eclipses[i].date.getTime()/1000;
}
