// determines how many comics will be displayed in a single row
var comicRow = 5;

//creates an array of comic data objects.  The day format is a simple 7 digit binary number with each digit a flag (M,T,W,Th,F,Sa,Su) or (64,32,16,8,4,2,1) .  For example 0010000 (16) indicates a comic that updates on wednesday
function Comic(title, url, img, imgBorder, description, update) {
	this.title = title;
	this.url = url;
	if( img.length > 0 ) { this.img = "images/comics/" + img; } else { this.img = ""; }
	this.imgBorder = imgBorder;
	this.description = description;
	this.update = update;
	if( update == 0 ) { this.updates = "monthly" }
	if( (update % 256) >= 128 ) { this.updates = "????" }
	if( (update % 128) >= 64 ) { this.updates = "M" }
	if( (update % 64) >= 32 ) { if( this.updates == undefined ) { this.updates = "T" } else { this.updates += "-T" } }
	if( (update % 32) >= 16 ) { if( this.updates == undefined ) { this.updates = "W" } else { this.updates += "-W" } }
	if( (update % 16) >= 8 ) { if( this.updates == undefined ) { this.updates = "Th" } else { this.updates += "-Th" } }
	if( (update % 8) >= 4 ) { if( this.updates == undefined ) { this.updates = "F" } else { this.updates += "-F" } }
	if( (update % 4) >= 2 ) { if( this.updates == undefined ) { this.updates = "Sa" } else { this.updates += "-Sa" } }
	if( (update % 2) == 1 ) { if( this.updates == undefined ) { this.updates = "Su" } else { this.updates += "-Su" } }
	if( update == 127 ) { this.updates = "everyday" }
}

var comic = new Array( new Comic("8-bit Theatre", "http://www.nuklearpower.com/8-bit-theater/", "rMIcon.jpg", true, "", 42) );
comic[comic.length] = new Comic("The Abominable Charles Christopher", "http://www.abominable.cc/", "tacc.jpg", true, "", 16);
comic[comic.length] = new Comic("", "http://www.applegeeks.com/", "appleGeeks.png", false, "", 92);
comic[comic.length] = new Comic("", "http://www.frozenreality.co.uk/comic/bunny/index.php?", "bunny.png", false, "", 127);
comic[comic.length] = new Comic("Dragon Tails", "http://www.dragon-tails.com/", "dragonTails.png", false, "", 1);
comic[comic.length] = new Comic("Dresden Codak", "http://www.dresdencodak.com/", "dresdenCodak.png", false, "", 1);
comic[comic.length] = new Comic("", "http://www.drmcninja.com/", "drMcNinja.png", false, "", 84);
comic[comic.length] = new Comic("", "http://www.errantstory.com/", "errantStory.png", false, "by Michael Poe", 84);
comic[comic.length] = new Comic("", "http://www.comics.com/comics/frazz/", "frazz.png", false, "", 124);
comic[comic.length] = new Comic("", "http://www.girlgeniusonline.com/comic.php", "girlGenius.jpg", true, "by Phil and Kaja Foglio", 84);
comic[comic.length] = new Comic("", "http://www.goats.com", "goats.png", false, "", 84);
comic[comic.length] = new Comic("It's Walky", "http://www.itswalky.com/", "itsWalky.png", true, "by David Willis", 2);
comic[comic.length] = new Comic("Johnny Wander", "http://www.johnnywander.com/", "crow.png", false, "", 40);
comic[comic.length] = new Comic("", "http://www.narbonic.com/", "narbonic.jpg", false, "By Shaenon K. Garrity<br>&ldquo;Director's Cut&rdquo;", 127);
comic[comic.length] = new Comic("", "http://www.nukees.com/", "nukees.png", false, "", 84);
comic[comic.length] = new Comic("The Order of the Stick", "http://www.giantitp.com/comics/oots.html", "oots.png", false, "", 1);
comic[comic.length] = new Comic("", "http://www.comics.com/comics/hedge/", "hedge.png", false, "", 124);
comic[comic.length] = new Comic("Overcompensating", "http://www.overcompensating.com/", "overcompensating.png", false, "Real things that actually happen", 120);
comic[comic.length] = new Comic("", "http://www.ozyandmillie.org/", "oM.gif", true, "", 124);
comic[comic.length] = new Comic("", "http://www.comics.com/comics/pearls/", "pearls.png", false, "", 124);
comic[comic.length] = new Comic("", "http://www.penny-arcade.com/view.php3", "penny.gif", true, "", 84);
comic[comic.length] = new Comic("", "http://www.reallifecomics.com/", "realLife.jpg", true, "by Greg Dead", 124);
comic[comic.length] = new Comic("", "http://sabrina-online.com/thismonth.html", "sabrina.jpg", true, "", 0)
comic[comic.length] = new Comic("Schlock Mercenary", "http://www.schlockmercenary.com/", "schlock.gif", true, "", 127);
comic[comic.length] = new Comic("", "http://www.sinfest.net/", "sinfest.png", false, "", 127);
comic[comic.length] = new Comic("A Softer World", "http://www.asofterworld.com/", "", false, "", 2);
comic[comic.length] = new Comic("Thinking Ape Blues", "http://www.thinkingapeblues.com/", "", false, "", 36);
comic[comic.length] = new Comic("", "http://www.whiteninjacomics.com/comics.shtml", "whiteNinja.png", false, "", 84);
comic[comic.length] = new Comic("WIGU", "http://wigu.com/", "wigu.png", false, "Check out the <a href='http://www.wigu.com/shop/' target='_blank'>T-Shirts</a>", 120);

//cycles through all the comics opening each URL
function openAllComics()
{
	var x = 0;
	for (x; x < comic.length; x++)
	{
		window.open(comic[x].url);
	}
    window.open("http://antwrp.gsfc.nasa.gov/apod/astropix.html");
}

//selects a random number within the range of the comic index and opens it.
function openRandomComic()
{
	var z = Math.floor(Math.random() * comics.length);
	window.open(comic[z].url);
}

// uses day parse to compare today to the days on which the comic is updated
function openTodaysComic()
{
	var x = 0;
	for (x; x < comic.length; x++)
	{
		if (dayParser(comic[x].update))
		{
			window.open(comic[x].url);
		}
	}
    window.open("http://antwrp.gsfc.nasa.gov/apod/astropix.html");
}

// determines the day, then determines if the comicDays variable contains that day
function dayParser(comicDays)
{
	var weekdayDate = new Date();
	var weekday = new Array(7);
	weekday[0] = 1;
	weekday[1] = 64;
	weekday[2] = 32;
	weekday[3] = 16;
	weekday[4] = 8;
	weekday[5] = 4;
	weekday[6] = 2;

	var today = weekday[weekdayDate.getDay()];

	// Uses modulus to strip the next higher/earlier days value (all earlier days are multiples of later/lower days)
	switch(today) {
		case 64: if ((comicDays % 128) >= 64) { return true; }
			break;
	
		case 32: if ((comicDays % 64) >= 32) { return true; }
			break;

		case 16: if ((comicDays % 32) >= 16) { return true; }
			break;

		case 8: if ((comicDays % 16) >= 8) { return true; }
			break;

		case 4: if ((comicDays % 8) >= 4) { return true; }
			break;

		case 2: if ((comicDays % 4) >= 2) { return true; }
			break;

		case 1: if ((comicDays % 2) >= 1) { return true; }
			break;
	}			
}

// automates the comic page creation
function makeComics() {
	var comicDisplay = "<div align='center'><div><a href = 'javascript:openAllComics()'>Open All Comics</a> | <a href = 'javascript:openRandomComic()'>Open One Comic</a> | <a href = 'javascript:openTodaysComic()'>Open Today's Comics</a></div>";
	comicDisplay += "<table border='0' align='center'>";
	comicDisplay += "<tr>";

	for(var c=0; c < comic.length; c++) {
		comicDisplay += "<td align='center'><a href='" + comic[c].url + "' target='_blank'>";
		if(comic[c].img.length > 0) {
			comicDisplay += "<img src='" + comic[c].img + "' border = '";
			if(comic[c].imgBorder) { comicDisplay += "1'>"; } else { comicDisplay += "0'>"; }
		}
		if(comic[c].title != "") { comicDisplay += "</a><br><a href='" + comic[c].url + "' target='_blank'>" + comic[c].title; }
		comicDisplay += "</a>";
		if(comic[c].description != "") { comicDisplay += "<br>" + comic[c].description; }
		comicDisplay += "<br>updated: " + comic[c].updates + "</td>";
		if( (c % comicRow) == (comicRow - 1) ) { comicDisplay += "</tr><tr>"; }
	}

	comicDisplay += "</tr></table>";
	comicDisplay += "<div align='center'><a href = 'javascript:openAllComics()'>Open All Comics</a> | <a href = 'javascript:openRandomComic()'>Open One Comic</a> | <a href = 'javascript:openTodaysComic()'>Open Today's Comics</a></div>";

	return comicDisplay;
}