// JavaScript Document

var cDiv;
var curTime;
var lastCalc;
var tID;

var UTCOffset = 8;

function getTimeArray()
{
	var curDate = new Date();
	var theTime = new Array();

	theTime[0] = 31-curDate.getDate();
	theTime[1] = 22-curDate.getHours();
	theTime[2] = 59-curDate.getMinutes();
	theTime[3] = 59-curDate.getSeconds();
	
	<!-- Correct for Time Zone -->
	
	theTime[1] -= curDate.getTimezoneOffset()/60;
	theTime[1] += UTCOffset;
	
	if (theTime[1] >= 24) {
		theTime[1] -= 24;
		theTime[0] += 1;
	}
	
	<!-- Check minuses -->
	if (theTime[3] < 0) {
			theTime[3] = 59;
			theTime[2] -= 1;
	}
	if (theTime[2] < 0) {
				theTime[2] = 59;
				theTime[1] -= 1;
	}
	if (theTime[1] < 0) {
					theTime[1] = 23;
					theTime[0] -= 1;
	}
	if (theTime[0] < 0) {
						itsEweek();
						return;
	}

	if (curDate.getMonth() > 1) theTime[0] = -1;
	return theTime;
}

function writeTime()
{
	var doneOnly = 0;
	var timeString = ""
	if (curTime[0] >= 1) {
		timeString += "Only " + curTime[0] + " Days, ";
		doneOnly = 1;
	}
	
	if ((curTime[1] >= 1) || (curTime[0] >= 1)) {
		if (doneOnly == 0) {
			timeString += "Only ";
			doneOnly = 1;
		}
		timeString += curTime[1] + " Hours, ";
	}
	
	if ((curTime[2] >= 1) || ((curTime[0] >= 1) || (curTime[1] >= 1))) {
		if (doneOnly == 0) {
			timeString += "Only ";
			doneOnly = 1;
		}
		timeString += curTime[2] + " Minutes, and ";
	}
	
	if ((curTime[3] >= 1) || (((curTime[0] >= 1) || (curTime[1] >= 1)) || curTime[2] >= 1)) {
		if (doneOnly == 0) {
			timeString += "Only ";
			doneOnly = 1;
		}
		timeString += curTime[3] + " Seconds until E-Week!";
	}
	timeString = "E-Week is Here!";
	cDiv.innerHTML = timeString;
}

function setupTimer()
{
	<!-- curTime[0] => days, curTime[1] => hours, curTime[2] => mins, curTime[3] => secs -->
	curTime = getTimeArray();
	lastCalc = 0;
	
	cDiv = document.getElementById("countdown");
	
	writeTime();
	tID = setInterval(updateTimer, 1000);
	return; 
}

function itsEweek()
{
	clearInterval(tID);
}

function updateTimer()
{
	if (lastCalc > (5*60)) {
		curTime = getTimeArray();
		if (curTime[0] < 0) {
			itsEweek();
			return;
		}
		lastCalc = 0;
	} else {
		curTime[3] -= 1;
		if (curTime[3] < 0) {
			curTime[3] = 59;
			curTime[2] -= 1;
			if (curTime[2] < 0) {
				curTime[2] = 59;
				curTime[1] -= 1;
				if (curTime[1] < 0) {
					curTime[1] = 23;
					curTime[0] -= 1;
					if (curTime[0] < 0) {
						itsEweek();
						return;
					}
				}
			}
		}
		lastCalc += 1;
	}
	writeTime();
}