﻿// JScript File
var targetMonth = '11';
var targetDay = '8';
var targetDow = 0;
var targetHour = 13;
var targetTimezone = 0;
var countdownOutputID = 'countdown';

function UpdateCountdown() 
{
	var effectiveMonth = targetMonth;
	if (targetMonth == '*')
	{
		effectiveMonth = 0;
	}
	var timeDiff = CalcTimeDiff(effectiveMonth, targetDay, targetHour, targetTimezone);
	if (targetMonth == '*' && timeDiff < 0)
	{
		timeDiff = CalcTimeDiff('*', targetDay, targetHour, targetTimezone);
	}
	DisplayCountdown(timeDiff);
}

function CalcTimeDiff(targetMonth, targetDay, targetHour, targetTimezone) 
{
	var toDate = new Date();
	if (targetDay.substr(0, 1) == '+') 
	{
		var day1 = parseInt(targetDay.substr(1));
		toDate.setDate(toDate.getDate() + day1);
	}
	else
	{
		toDate.setDate(targetDay);
	}
	if (targetMonth == '*')
	{
		toDate.setMonth(toDate.getMonth() + 1);
	}
	else if (targetMonth > 0) 
	{
		if (targetMonth <= toDate.getMonth())
		{
			toDate.setYear(toDate.getYear() + 1);
		}
		toDate.setMonth(targetMonth - 1);
	}
	if (targetDow > 0)
	{
		toDate.setDate(toDate.getDate() + (targetDow - 1 - toDate.getDay()) % 7);
	}
	toDate.setHours(targetHour);
	toDate.setMinutes(0 - (targetTimezone * 60));
	toDate.setSeconds(0);
	var fromDate = new Date();
	fromDate.setMinutes(fromDate.getMinutes() + fromDate.getTimezoneOffset());
	var diffDate = new Date(0);
	diffDate.setMilliseconds(toDate - fromDate);
	return Math.floor(diffDate.valueOf() / 1000);
}

function DisplayCountdown(countdn) 
{
	if (countdn > 0)
	{
		var secs = countdn % 60;
		var countdn1 = (countdn - secs) / 60;
		var mins = countdn1 % 60;
		countdn1 = (countdn1 - mins) / 60;
		var hours = countdn1 % 24;
		var days = (countdn1 - hours) / 24;
		var text = "The story begins in";
		if (days > 0)
		{
			text += " " + days + " days";
		}
		if (hours > 0)
		{
			text += " " + hours + " hours";
		}
		if (mins > 0)
		{
			text += " " + mins + " minutes";
		}
		if (secs > 0)
		{
			text += " " + secs + " seconds";
		}
		text += " &#0133;";
		document.getElementById(countdownOutputID).innerHTML = text;
	}
}

function StartCountdown() 
{
	UpdateCountdown();
	setInterval('UpdateCountdown();', 1000);
}

