var strip;
var first;
var prev;
var next;
var last;

function init() {
	strip=document.getElementById("strip");
	first=document.getElementById("first");
	prev=document.getElementById("prev");
	next=document.getElementById("next");
	last=document.getElementById("last");
	if (prev) prev.onclick=prevstrip;
	if (next) next.onclick=nextstrip;
	if (first) first.onclick=firststrip;
	if (last) last.onclick=laststrip;

	showlatest();

	// Write the archive links
	var archivediv = document.getElementById("archive");
	var itmDate;
	if (archivediv) {
		for (var i in list) {
			itmDate = file2date(list[i]);
			if (itmDate === 0) throw 'Invalid Date';
			var link=document.createElement("a");
			if (strip) {
				link.setAttribute("href","javascript:setstrip('"+i+"')");
			} else {
				link.setAttribute("href",list[i]);
				link.setAttribute("target","comic_view");
			}
			link.innerHTML=itmDate.getFullYear() + "/" + (parseInt(itmDate.getMonth())+1) + "/" + itmDate.getDate();

			archivediv.appendChild(link);
			archivediv.appendChild(document.createElement("br"));
		}
	}
}

function setstrip(i) {
	if (i >= 0 && i < list.length) {
		if (strip) {
			strip.num=i;
			strip.src=list[i];
			if (i == 0) {
				if (prev) prev.className="off";
				if (first) first.className="off";
			} else {
				if (prev) prev.className="on";
				if (first) first.className="on";
			}
			if (i == list.length-1) {
				if (next) next.className="off";
				if (last) last.className="off";
			} else {
				if (next) next.className="on";
				if (last) last.className="on";
			}
		} else {
			document.location=list[i];
		}
	}
}

function showlatest () {
	// Write the current comic
	if (strip) setstrip(list.length-1);
}

function firststrip() {
	setstrip(0);
}

function laststrip() {
	setstrip(list.length-1);
}

function prevstrip() {
	if (!strip) alert("Webmaster: no <img id=\"strip\"> was found!");
	else if (strip.num > 0) {
		strip.num--;
		setstrip(strip.num);
	}
}

function nextstrip() {
	if (!strip) alert("Webmaster: no <img id=\"strip\"> was found!");
	else if (strip.num < list.length-1) {
		strip.num++;
		setstrip(strip.num);
	}
}

function file2date (str) {
	var itm = str.split("/");
	if (itm[0] < 1000) itm[0] = parseInt(itm[0], 10) + 2000;
	try {
		var filename = itm[2].split("."); //if itm[2] doesn't exist, it's likely an invalid date entry
		var itmDate = new Date();
		itmDate.setFullYear(itm[0], itm[1] - 1, filename[0]);
		itmDate.setHours(0);
	} catch (err) { //typically happens when the file has a trailing newline
		//alert('Caught Error: ' + err);
		itmDate = 0;
	}
	return itmDate;
}


var curDate = new Date();

var oRequest;
if (window.XMLHttpRequest) { //not IE
	oRequest = new XMLHttpRequest();
} else { //IE
	oRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
//var sURL  = "http://"+self.location.hostname+"/comic/comiclist.txt";
var datafile;
var list = [];
var itmDate = new Date();

oRequest.open("GET","comiclist.txt",false);
oRequest.setRequestHeader("User-Agent",navigator.userAgent);
oRequest.send(null)

if (oRequest.status==200) {
	datafile = oRequest.responseText;
	list = datafile.split("\n");
	list.length -= 1;
	for (var i in list) {
		itmDate = file2date(list[i]);
		if ((itmDate === 0) || (itmDate > curDate)) {
			list.length = parseInt(i);
		}
	}
}
else alert("Error executing XMLHttpRequest call!");

window.onload=init;
