// Carl Irving 2010
var weekday = new Date();
weekday.setHours(weekday.getHours() - 3);
var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var shortDays = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
var shortMonths = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
function dateprint(datevar) {
	return datevar.getFullYear() + "-" + (datevar.getMonth() + 1) + "-" + datevar.getDate();
}
function datefull(datevar) {
	return shortDays[datevar.getDay()] + ". " + shortMonths[datevar.getMonth()] + " " + datevar.getDate();
}
function getAJAX(href) {
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.open("GET",href,false);
	xmlhttp.send(null);
	return xmlhttp.responseText;
}

function updatePanel(panelid, datekey) {
	localStorage.setItem(datekey, getAJAX("day.php?date=" + datekey));
	document.getElementById(panelid).innerHTML = window.localStorage.getItem(datekey);
}

function loaddays() {
	daycount = new Date();
	daycount.setTime(weekday.valueOf());
	for (i = 0; i < 7; i++) {
		document.getElementById("link" + i).innerHTML = "<a href=\"#panel" + i + "\">" + dayNames[daycount.getDay()] + "</a>";
		document.getElementById("panel" + i).innerHTML = window.localStorage.getItem(dateprint(daycount));
		document.getElementById("panel" + i).title = datefull(daycount);
		daycount.setDate(daycount.getDate() + 1);
	}
	if (navigator.onLine) {
		daycount.setTime(weekday.valueOf());
		daycount.setDate(daycount.getDate() + 6);
		if (window.localStorage.getItem(dateprint(daycount)) == null) {
			daycount.setTime(weekday.valueOf());
			localStorage.clear();
			for (i = 0; i < 7; i++) {
				setTimeout("updatePanel(\"panel" + i + "\", \"" + dateprint(daycount) + "\")", 10);
				daycount.setDate(daycount.getDate() + 1);
			}
		}
	}
}
addEventListener("load", loaddays, false);