/**
 * Calculates the time at a specific location.
 * Used to show my local time on my website.
 */
var myCity;
var myOffset;
var dst = 0;

/**
 * Determine wether we are currently in Daylight Savings Time or not.
 * Currently configured for Melbourne DST. Needs to be adjusted elsewhere.
 */
function isInDst() {
	var firstSundayApril = new Date;
	firstSundayApril.setMonth(3); // April
	firstSundayApril.setDate(1); // 1st day in April
	var day = firstSundayApril.getDay(); // day of week of 1st
	if (day != 0)
		firstSundayApril.setDate(8 - day); // 1st Sunday

	var firstSundayOctober = new Date;
	firstSundayOctober.setMonth(9); // October
	firstSundayOctober.setDate(1);
	day = firstSundayOctober.getDay();
	if (day != 0)
		firstSundayOctober.setDate(8 - day);

	var utc = new Date;
	if (utc < firstSundayApril || utc >= firstSundayOctober)
		dst = 1;
}

/**
 * Display the time for my specific city in the document
 */
function showTime() {
	// create Date object for current location
	d = new Date();

	// convert to msec  add local time zone offset
	utc = d.getTime() + (d.getTimezoneOffset() * 60000);

	// create new Date object for different city using supplied offset
	nd = new Date(utc + (3600000* (myOffset + dst)));

	// Put the date into the document
	document.getElementById("localtime").innerHTML = myCity + " time is " + nd.toLocaleString();
}

/**
 * Show the time once for the specified city and offset, then set up a timer
 * to redisplay it every second.
 */
function calcTime(city, offset) {
	myCity = city;
	myOffset = offset;
//	isInDst();
	showTime();
	setInterval( "showTime()", 1000);
}

