// simpsons.js
//   JavaScript support for The Australian Simpsons Gazette website.

// Normal usage
// ------------
// Add script src line to head.
//
// Start body with a script tag and call simpnavbar().
//
// Anywhere I want an email link, add a script tag and call emaillink() with
// appropriate parameters.
//
// On the Foxtel and Ten pages, call foxnavbar() and tennavbar() with the
// stream name as parameter to generate an appropriate nav bar under each
// stream.
//
// On the monthly archive pages, call monthshortbar() just under the contents,
// then monthnavbar() under each stream, using document.URL and stream labels
// as parameters.
//
// At the end of the page, call simpnavbar() again, between the h-rule and
// suggestion request.  Could use a script for the h-rule, but it won't always
// appear when viewing source.

// Note that part of this structure is predicated on the fact that I can't seem
// to set global variables at the top of the webpage.  Instead, I use global
// variables in this script file.  Otherwise, I should be able to use &{varname};
// directly in the HTML.


// Set current US and Australian seasons.
var ausseason = "simprau18.htm"
var usaseason = "simprus19.htm"

// Set email address globally.
var email1 = "&#112;&#115;&#116;&#101;&#112;&#97;&#110;&#97;&#115;"
var email2 = "&#103;&#109;&#97;&#105;&#108;&#46;&#99;&#111;&#109;"
var mto = "&#109;&#97;&#105;&#108;&#116;&#111;&#58;"


// Generate monthly archive filenames.
/*   Used when monthly pages were updated regularly.
     If reinstated, also modify simpnavbar() and tennavbar()
var date = new Date()
var month = date.getMonth()
var year = date.getYear() % 100

var prevfile = prevmonth(month, year)
var currfile = currmonth(month, year)
var nextfile = nextmonth(month, year)
*/

// Manual settings for archives that are no longer updated.
var prevfile = "simp2006.htm"
var currfile = "simp2007.htm"
var nextfile = "simp2008.htm"


// emaillink(text, subject)
//
function emaillink(text, subject) {
  if (arguments.length < 2) {
    document.write("<A HREF=\"", mto, email1, "&#64;", email2, "\">", text, "</A>")
  } else {
    document.write("<A HREF=\"", mto, email1, "&#64;", email2, "?subject=", subject, "\">", text, "</A>")
  }
}


// simpnavbar()
//
// Outputs top and bottom navbars for Simpsons pages.
// Currently no attempt is made to unlink pointers to the current page.
//
function simpnavbar() {
  document.writeln("<!-- Simpsons Gazette Navbar -->")
  document.writeln("<P><TABLE BORDER=0 WIDTH=420><TR ALIGN=CENTER VALIGN=BOTTOM>")
  document.writeln("<TD WIDTH=70><A HREF=\"simpsons.htm\"><IMG SRC=\"images/simphome.gif\" ALIGN=BOTTOM WIDTH=50 HEIGHT=50 BORDER=0><BR><SMALL>[Simpsons<BR>Home]</SMALL></A></TD>")
  document.writeln("<TD WIDTH=70><A HREF=\"simpnews.htm\"><IMG SRC=\"images/news.gif\" ALIGN=BOTTOM WIDTH=50 HEIGHT=50 BORDER=0><BR><SMALL>[Simpsons<BR>News]</SMALL></A></TD>")
  document.writeln("<TD WIDTH=70><A HREF=\"simpten.htm\"><IMG SRC=\"images/ten.gif\" ALIGN=BOTTOM WIDTH=50 HEIGHT=50 BORDER=0><BR><SMALL>[Simpsons<BR>on Ten]</SMALL></A></TD>")
  document.writeln("<TD WIDTH=70><A HREF=\"simpfox.htm\"><IMG SRC=\"images/foxtel.gif\" ALIGN=BOTTOM WIDTH=50 HEIGHT=50 BORDER=0><BR><SMALL>[Simpsons<BR>on Foxtel]</SMALL></A></TD>")
  document.writeln("<TD WIDTH=70><A HREF=\"simpeps.htm\"><IMG SRC=\"images/list.gif\" ALIGN=BOTTOM WIDTH=50 HEIGHT=50 BORDER=0><BR><SMALL>[Episode<BR>List]</SMALL></A></TD>")
  document.writeln("<TD WIDTH=70><A HREF=\"tvguides.htm\"><IMG SRC=\"images/gazette.gif\" ALIGN=BOTTOM WIDTH=50 HEIGHT=50 BORDER=0><BR><SMALL>[Oz TV<BR>Gazette]</SMALL></A></TD>")
  document.writeln("</TR></TABLE>")
}


// tennavbar(label)
//
// Outputs navbars for the Ten Simpsons page.
//
function tennavbar(label) {
  if (arguments.length > 0)
    label = "#" + label
  document.writeln("<!-- Ten Navbar -->")
  document.writeln("<P><TABLE BORDER=0 WIDTH=370><TR ALIGN=CENTER VALIGN=TOP>")
  document.writeln("<TD WIDTH=40><IMG SRC=\"images/dot_clr.gif\" ALIGN=BOTTOM WIDTH=1 HEIGHT=1 HSPACE=20></TD>")
  document.writeln("<TD WIDTH=55><A HREF=\"", prevfile, label, "\"><IMG SRC=\"images/simleft.gif\" ALIGN=BOTTOM WIDTH=40 HEIGHT=30 BORDER=0><BR><SMALL>[Previous<BR>Month]</SMALL></A></TD>")
//  document.writeln("<TD WIDTH=55><A HREF=\"", nextfile, label, "\"><IMG SRC=\"images/simright.gif\" ALIGN=BOTTOM WIDTH=40 HEIGHT=30 BORDER=0><BR><SMALL>[Next<BR>Month]</SMALL></A></TD>")
    document.writeln("<TD WIDTH=55><BR></TD>")
  document.writeln("<TD WIDTH=55><A HREF=\"", currfile, "\"><IMG SRC=\"images/simtop.gif\" ALIGN=BOTTOM WIDTH=40 HEIGHT=30 BORDER=0><BR><SMALL>[This<BR>Month]</SMALL></A></TD>")
  document.writeln("<TD WIDTH=55><A HREF=\"", ausseason, "\"><IMG SRC=\"images/simaus.gif\" ALIGN=BOTTOM WIDTH=40 HEIGHT=30 BORDER=0><BR><SMALL>[Oz<BR>Season]</SMALL></A></TD>")
  document.writeln("<TD WIDTH=55><A HREF=\"", usaseason, "\"><IMG SRC=\"images/simusa.gif\" ALIGN=BOTTOM WIDTH=40 HEIGHT=30 BORDER=0><BR><SMALL>[US<BR>Season]</SMALL></A></TD>")
  document.writeln("<TD WIDTH=55><A HREF=\"simpfox.htm\"><IMG SRC=\"images/foxsmall.gif\" ALIGN=BOTTOM WIDTH=40 HEIGHT=30 BORDER=0><BR><SMALL>[On<BR>Foxtel]</SMALL></A></TD>")
  document.writeln("</TR></TABLE>")
  document.writeln("<P><BR>")
}


// foxnavbar()
//
// Outputs navbars for the Foxtel Simpsons page.
//
function foxnavbar(label) {
  if (arguments.length > 0)
    label = "#" + label
  document.writeln("<!-- Foxtel Navbar -->")
  document.writeln("<P><TABLE BORDER=0 WIDTH=260><TR ALIGN=CENTER VALIGN=TOP>")
  document.writeln("<TD WIDTH=40><IMG SRC=\"images/dot_clr.gif\" ALIGN=BOTTOM WIDTH=1 HEIGHT=1 HSPACE=20></TD>")
  document.writeln("<TD WIDTH=55><A HREF=\"", prevfile, label, "\"><IMG SRC=\"images/simleft.gif\" ALIGN=BOTTOM WIDTH=40 HEIGHT=30 BORDER=0><BR><SMALL>[Previous<BR>Month]</SMALL></A></TD>")
//  document.writeln("<TD WIDTH=55><A HREF=\"", nextfile, label, "\"><IMG SRC=\"images/simright.gif\" ALIGN=BOTTOM WIDTH=40 HEIGHT=30 BORDER=0><BR><SMALL>[Next<BR>Month]</SMALL></A></TD>")
    document.writeln("<TD WIDTH=55><BR></TD>")
  document.writeln("<TD WIDTH=55><A HREF=\"", currfile, "\"><IMG SRC=\"images/simtop.gif\" ALIGN=BOTTOM WIDTH=40 HEIGHT=30 BORDER=0><BR><SMALL>[This<BR>Month]</SMALL></A></TD>")
  document.writeln("<TD WIDTH=55><A HREF=\"simpten.htm\"><IMG SRC=\"images/tensmall.gif\" ALIGN=BOTTOM WIDTH=40 HEIGHT=30 BORDER=0><BR><SMALL>[On<BR>Ten]</SMALL></A></TD>")
  document.writeln("</TR></TABLE>")
}


// monthnavbar(monthurl, labelp, labeln)
//
// Outputs navbars for monthly archive pages.
// The parameter monthurl should be document.URL
//
// If only one label is specified, it is used for both previous and next month.
// If a label consists of just a hash, that button is not drawn.
//
function monthnavbar(monthurl, labelp, labeln) {
  var mmyy = url2mmyy(monthurl)
  var amonth = eval(mmyy.substring(0, 2)) - 1
  var ayear = eval(mmyy.substring(2, 4))
  if (arguments.length < 3)
    labeln = labelp
  if (arguments.length > 1) {
    labelp = "#" + labelp
    labeln = "#" + labeln
  }

  document.writeln("<!-- Monthly Nav Bar -->")
  document.writeln("<P><TABLE BORDER=0 WIDTH=205><TR ALIGN=CENTER VALIGN=BOTTOM>")
  document.writeln("<TD WIDTH=40><IMG SRC=\"images/dot_clr.gif\" ALIGN=BOTTOM WIDTH=1 HEIGHT=1 HSPACE=20></TD>")
  if (labelp == "##")
    document.writeln("<TD WIDTH=55><BR></TD>")
  else
    document.writeln("<TD WIDTH=55><A HREF=\"", prevmonth(amonth, ayear), labelp, "\"><IMG SRC=\"images/simleft.gif\" ALIGN=BOTTOM WIDTH=40 HEIGHT=30 BORDER=0><BR><SMALL>[Previous<BR>Month]</SMALL></A></TD>")
  if (labeln == "##")
    document.writeln("<TD WIDTH=55><BR></TD>")
  else
    document.writeln("<TD WIDTH=55><A HREF=\"", nextmonth(amonth, ayear), labeln, "\"><IMG SRC=\"images/simright.gif\" ALIGN=BOTTOM WIDTH=40 HEIGHT=30 BORDER=0><BR><SMALL>[Next<BR>Month]</SMALL></A></TD>")
  document.writeln("<TD WIDTH=55><A HREF=\"#\"><IMG SRC=\"images/simtop.gif\" ALIGN=BOTTOM WIDTH=40 HEIGHT=30 BORDER=0><BR><SMALL>[This<BR>Month]</SMALL></A></TD>")
  document.writeln("</TR></TABLE>")
}


// monthshortbar(monthurl)
//
// Outputs a short navbar for near the top of a monthly archive page.
// The parameter should be document.URL
//
// Note that amonth needs to be 0-based.
//
function monthshortbar(monthurl) {
  var mmyy = url2mmyy(monthurl)
  var amonth = eval(mmyy.substring(0, 2)) - 1
  var ayear = eval(mmyy.substring(2, 4))
  document.writeln("<!-- Monthly Short Nav Bar -->")
  document.writeln("<P><TABLE BORDER=0 WIDTH=160><TR ALIGN=CENTER VALIGN=BOTTOM>")
  document.writeln("<TD WIDTH=60><IMG SRC=\"images/dot_clr.gif\" ALIGN=BOTTOM WIDTH=1 HEIGHT=1 HSPACE=30></TD>")
  document.writeln("<TD WIDTH=50><A HREF=\"", prevmonth(amonth, ayear), "\"><IMG SRC=\"images/simleft.gif\" ALIGN=BOTTOM WIDTH=40 HEIGHT=30 BORDER=0><BR><SMALL>[Previous<BR>Month]</SMALL></A></TD>")
  document.writeln("<TD WIDTH=50><A HREF=\"", nextmonth(amonth, ayear), "\"><IMG SRC=\"images/simright.gif\" ALIGN=BOTTOM WIDTH=40 HEIGHT=30 BORDER=0><BR><SMALL>[Next<BR>Month]</SMALL></A></TD>")
  document.writeln("</TR></TABLE>")
}


// prevmonth(month, year)
//
// Converts a 0-based month and a 0-99 year to a filename (simpMMYY.htm) for
// the previous month.
//
function prevmonth(month, year) {

  // Decrement one month and adjust offset
  if (month == 0) {
    month = 12
    year -= 1
    if (year < 0)
      year = 99
  }

  return month2fname(month, year)
}


// nextmonth(month, year)
//
// Converts a 0-based month and a 0-99 year to a filename (simpMMYY.htm) for
// the next month.
//
function nextmonth(month, year) {

  // Increment one month and adjust offset
  if (month == 11) {
    month = 1
    year += 1
    if (year > 99)
      year = 0
  }
  else
    month += 2

  return month2fname(month, year)
}


// currmonth(month, year)
//
// Converts a 0-based month and a 0-99 year to a filename (simpMMYY.htm) for
// the current month.
//
function currmonth(month, year) {
  // Adjust month offset first
  month += 1
  return month2fname(month, year)
}


// month2fname(month,year)
//
// Converts a 1-based month and a 0-99 year to a filename (simpMMYY.htm).
//
function month2fname (month, year) {
  return "simp" + ((month < 10) ? "0" + month : month) + ((year < 10) ? "0" + year : year) + ".htm"
}


// url2mmyy(monthurl)
//
// Extracts an MMYY code from a full URL with file simpMMYY.htm
//
function url2mmyy (monthurl) {
  var i = monthurl.lastIndexOf("#")
  if (i == -1)
    i = monthurl.length - 1
  i = monthurl.lastIndexOf("simp", i) + 4
  return monthurl.substring(i, i+4)
}


// yearnavbar(yearurl, labelp, labeln)
//
// Outputs navbars for yearly archive pages.
// The parameter yearurl should be document.URL
//
// If only one label is specified, it is used for both previous and next year.
// If a label consists of just a hash, that button is not drawn.
//
function yearnavbar(yearurl, labelp, labeln) {
  var yyyy = url2mmyy(yearurl)
  var pyear = "simp" + (parseInt(yyyy) - 1) + ".htm"
  var nyear = "simp" + (parseInt(yyyy) + 1) + ".htm"
  if (arguments.length < 3)
    labeln = labelp
  if (arguments.length > 1) {
    labelp = "#" + labelp
    labeln = "#" + labeln
  }

  document.writeln("<!-- Yearly Nav Bar -->")
  document.writeln("<P><TABLE BORDER=0 WIDTH=205><TR ALIGN=CENTER VALIGN=BOTTOM>")
  document.writeln("<TD WIDTH=40><IMG SRC=\"images/dot_clr.gif\" ALIGN=BOTTOM WIDTH=1 HEIGHT=1 HSPACE=20></TD>")
  if (labelp == "##")
    document.writeln("<TD WIDTH=55><BR></TD>")
  else
    document.writeln("<TD WIDTH=55><A HREF=\"", pyear, labelp, "\"><IMG SRC=\"images/simleft.gif\" ALIGN=BOTTOM WIDTH=40 HEIGHT=30 BORDER=0><BR><SMALL>[Previous<BR>Year]</SMALL></A></TD>")
  if (labeln == "##")
    document.writeln("<TD WIDTH=55><BR></TD>")
  else
    document.writeln("<TD WIDTH=55><A HREF=\"", nyear, labeln, "\"><IMG SRC=\"images/simright.gif\" ALIGN=BOTTOM WIDTH=40 HEIGHT=30 BORDER=0><BR><SMALL>[Next<BR>Year]</SMALL></A></TD>")
  document.writeln("<TD WIDTH=55><A HREF=\"#\"><IMG SRC=\"images/simtop.gif\" ALIGN=BOTTOM WIDTH=40 HEIGHT=30 BORDER=0><BR><SMALL>[This<BR>Year]</SMALL></A></TD>")
  document.writeln("</TR></TABLE>")
}


// yearshortbar(yearurl)
//
// Outputs a short navbar for near the top of a yearly archive page.
// The parameter should be document.URL
//
function yearshortbar(yearurl) {
  var yyyy = url2mmyy(yearurl)
  var pyear = "simp" + (parseInt(yyyy) - 1) + ".htm"
  var nyear = "simp" + (parseInt(yyyy) + 1) + ".htm"

  document.writeln("<!-- Yearly Short Nav Bar -->")
  document.writeln("<P><TABLE BORDER=0 WIDTH=160><TR ALIGN=CENTER VALIGN=BOTTOM>")
  document.writeln("<TD WIDTH=60><IMG SRC=\"images/dot_clr.gif\" ALIGN=BOTTOM WIDTH=1 HEIGHT=1 HSPACE=30></TD>")
  document.writeln("<TD WIDTH=50><A HREF=\"", pyear, "\"><IMG SRC=\"images/simleft.gif\" ALIGN=BOTTOM WIDTH=40 HEIGHT=30 BORDER=0><BR><SMALL>[Previous<BR>Year]</SMALL></A></TD>")
  document.writeln("<TD WIDTH=50><A HREF=\"", nyear, "\"><IMG SRC=\"images/simright.gif\" ALIGN=BOTTOM WIDTH=40 HEIGHT=30 BORDER=0><BR><SMALL>[Next<BR>Year]</SMALL></A></TD>")
  document.writeln("</TR></TABLE>")
}

