var daysOfWeek = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var monthsOfYear = ["January","February","March","April","May","June","July","August","September","October","November","December"];
var lastDayOfMonth = [31,28,31,30,31,30,31,31,30,31,30];

var today = new Date();
var d = today.getDate();
var m = today.getMonth()+1;
var y = today.getFullYear();
if (m < 10) m = "0" + m;
if (d < 10) d = "0" + d;
today = "" + y + m + d;

var oneMinute = 60 * 1000  // milliseconds in a minute
var oneHour = oneMinute * 60
var oneDay = oneHour * 24
var oneMonth = oneDay * 30

// http://siteownersforums.com/showthread/t-2128.html
function QSHandler() 
{
	var qs = location.search.substr(1).split("&");
	this.data = [];
	for(var i = 0; i < qs.length; i++) this.data[qs[i].split("=")[0]] = qs[i].split("=")[1];

	this.QueryString = function(x) 
	{
		if( document.URL.indexOf('?') == -1 )
			return "";
		else
			return this.data[x];
	};
} var Request = new QSHandler();

function write_date_selection_box()
{
	document.write("		<FORM NAME=\"jump\">\n" +
					   "				<SELECT NAME=\"menu\" onChange=\"location=document.jump.menu.options[document.jump.menu.selectedIndex].value;\" value=\"GO\">\n" +
					   "				<OPTION VALUE=\"comics.html\">Choose a Date</OPTION>\n");

	var currDay = new Date();
	var oldestDay = new Date();
	oldestDay.setFullYear(2007);
	oldestDay.setMonth(0); // Month (0-11)
	oldestDay.setDate(1);

	while ( currDay > oldestDay )
	{
		var d = currDay.getDate();
		var dayOfWeek = currDay.getDay();
		var m = currDay.getMonth()+1;
		var y = currDay.getFullYear();

		if (m < 10) m = "0" + m;
		if (d < 10) d = "0" + d;

		document.write("        <OPTION VALUE=\"comics.html?date=" + y + m + d + "\"> " + daysOfWeek[dayOfWeek] + ", " + monthsOfYear[m-1] + " " + d + ", " + y + "</OPTION>\n");

		currDay = new Date(currDay - oneDay);
	}

	document.write("		    </SELECT>\n" +
                  "		</FORM>\n" +
                  "		<BR>");
}

function Comic( fullname, shortname, url )
{
  this.fullname = fullname;
  this.shortname = shortname;
  this.url = url;
}

list_of_comics = [ new Comic("Get Fuzzy", "getfuzzy", "http://www.comics.com/comics/getfuzzy"),
                   new Comic("Calvin and Hobbes", "calvinandhobbes", "http://www.ucomics.com/calvinandhobbes/viewft.htm"),
                   new Comic("Sherman's Lagoon", "shermanslagoon", "http://www.slagoon.com"),
                   new Comic("Foxtrot", "foxtrot", "http://www.foxtrot.com"),
                   new Comic("For Better or For Worse", "forbetterorworse", "http://www.ucomics.com/forbetterorforworse"),
                   new Comic("Sally Forth", "sallyforth", "http://www.kingfeatures.com/features/comics/sforth/about.htm"),
                   new Comic("Non Sequitur", "nonsequitur", "http://www.ucomics.com/nonsequitur/viewnq.htm"),
                   new Comic("Zits", "zits", "http://www.kingfeatures.com/features/comics/zits/about.htm"),
                   new Comic("Frazz", "frazz", "http://www.kingfeatures.com/features/comics/frazz/about.htm"),
                   new Comic("Funky Winkerbean", "funkywinkerbean", "http://www.kingfeatures.com/features/comics/fwinker/about.htm"),
                   new Comic("B.C.", "bc", "http://www.comics.com/creators/bc"),
                   new Comic("Wizard of Id", "wizardofid", "http://www.comics.com/creators/wizardofid"),
                   new Comic("Grin and Bear It", "grinandbearit", "http://www.kingfeatures.com/features/comics/grinbear/about.htm"),
                   new Comic("Pearls Before Swine", "pearlsbeforeswine", "http://www.comics.com/comics/pearls"),
                   new Comic("Over the Hedge", "overthehedge", "http://www.comics.com/comics/hedge/index.html"),
                   new Comic("The Duplex", "theduplex", "http://www.ucomics.com/duplex/viewdp.htm"),
                   new Comic("Baby Blues", "babyblues", "http://www.kingfeatures.com/features/comics/babyblue/about.htm"),
                   new Comic("Rose is Rose", "roseisrose", "http://www.comics.com/roseisrose/"),
                   new Comic("Hagar the Horrible", "hagarthehorrible", "http://www.kingfeatures.com/features/comics/hagar/about.htm"),
                   new Comic("Mother Goose and Grimm", "mothergooseandgrimm", "http://www.kingfeatures.com/features/comics/grimmy/about.htm"),
                   new Comic("Garfield", "garfield", "http://www.ucomics.com/garfield/viewdb.htm"),
                   new Comic("Peanuts", "peanuts", "http://www.comics.com/comics/peanuts"),
                   new Comic("Dennis the Menace", "dennisthemenace", "http://www.kingfeatures.com/features/comics/dennis/about.htm"),
                   new Comic("Mutts", "mutts", "http://www.kingfeatures.com/features/comics/mutts/about.htm"),
                   new Comic("Tiger", "tiger", "http://www.kingfeatures.com/features/comics/tiger/about.htm"),
                   new Comic("Shoe", "shoe", "http://www.ucomics.com/shoe/viewdb.htm"),
                   new Comic("Curtis", "curtis", "http://www.kingfeatures.com/features/comics/curtis/about.htm"),
                   new Comic("Ziggy", "ziggy", "http://www.ucomics.com/ziggy/viewdb.htm"),
                   new Comic("Buckles", "buckles", "http://www.kingfeatures.com/features/comics/buckles/about.htm"),
                   new Comic("9 Chickweed Lane", "9chickweedlane", "http://www.comics.com/comics/chickweed"),
                   new Comic("Rhymes with Orange", "rhymeswithorange", "http://www.kingfeatures.com/features/comics/orange/about.htm"),
                   new Comic("The Other Coast", "theothercoast", "http://www.comics.com/comics/theothercoast"),
                   new Comic("Strange Brew", "strangebrew", "http://www.comics.com/comics/strangebrew"),
                   new Comic("Speed Bump", "speedbump", "http://www.comics.com/creators/speedbump/"),
                   new Comic("Bizarro", "bizarro", "http://www.kingfeatures.com/features/comics/bizarro/about.htm"),
                   new Comic("Doonesbury", "doonesbury", "http://www.doonesbury.com"),
                   new Comic("Mallard Fillmore", "mallardfillmore", "http://www.kingfeatures.com/features/comics/mallard/about.htm"),
                   new Comic("Dilbert", "dilbert", "http://www.dilbert.com/"),
                 ];

// Update - B.C., 9 Chickweed Lane

function ignore_drag_click()
{
  if ( is_dragging )
  {
    is_dragging = false;
    return false;
  }
  else
    return true;
}

function write_comics(the_date)
{
  var d = the_date.getDate();
  var m = the_date.getMonth()+1;
  var y = the_date.getFullYear();
  if (m < 10) m = "0" + m;
  if (d < 10) d = "0" + d;
  date_str = "" + y + m + d;

  one_month_ago = new Date(the_date - oneMonth);
  var d = one_month_ago.getDate();
  var m = one_month_ago.getMonth()+1;
  var y = one_month_ago.getFullYear();
  if (m < 10) m = "0" + m;
  if (d < 10) d = "0" + d;
  one_month_ago = "" + y + m + d;

  if ( daysOfWeek[the_date.getDay()] == "Sunday" )
    document.write("<ul id=\"comics\" class=\"sortable boxy clickable sunday\">");
  else
    document.write("<ul id=\"comics\" class=\"sortable boxy clickable\">");
  for( i = 0; i < list_of_comics.length; i++ )
  {
    the_comic = list_of_comics[i];
	 // Skip Foxtrot except on Sunday.
	 if ( the_comic.shortname == "foxtrot" && daysOfWeek[the_date.getDay()] != "Sunday" )
	   continue;
    document.write("<li itemID=\"" + i + "\">");
    document.write( "<A HREF=\"" + the_comic.url + "\" CLASS=comics onClick=\"return false;\"><STRONG>" + the_comic.fullname + "</STRONG><BR></A>\n" );
    document.write( "<A HREF=\"comic_range.html?comic=" + the_comic.shortname + "&begin=" + one_month_ago + "&end=" + date_str + "\" CLASS=comics onClick=\"return false;\"><IMG SRC=\"comics/" + the_comic.shortname + "/" + the_comic.shortname + "-" + date_str + ".gif\" BORDER=0></A>\n" );
    // Write a dummy link which will absorb the "onClick" event that gets fired when doing drag 'n drop.
    document.write( "<A HREF=\"\" CLASS=comics onclick=\"return false;\"></A>\n" );
    document.write("</li>\n");
  }
  document.write("</ul>\n");
}
