/*
	Function: getStateOffset
	Arguments:
		s: state code
	Return values:
		On success: returns GMT offset of state with DST taken into account.
		On failure: returns 0.
	
	Date: 03/28/08
	Author: John Peloquin, W. Hardy Interactive (john@whardy.com)
			Based on code by Stephen Chapman
	
	Description: Gives the GMT offset of a state, taking into account DST.
	Notes:
		- This function was not written to support all states, but can be extended.
		- This function depends on the client's date/time being correct for its locale.
		- This function checks whether it is DST timeframe using the client's
			locale information, hence on the days when DST state changes (twice per
			year), this will temporarily give incorrect results for a client not in
			the target state, when DST timeframe has begun/ended in the target state
			but not yet in the client's timezone (or vice versa). The results will
			be correct once the client's timezone and the target state are both
			in/out of the DST timeframe locally.
	
	Revisions:
		- [03/28/08 - JMP] created.
*/
function getStateOffset(s) {
	var states = {
		GA: [-5,1],
		MA: [-5,1],
		IL: [-6,1],
		TX: [-6,1],
		CA: [-8,1],
		NY: [-5,1],
		NJ: [-5,1],
		FL: [-5,1],
		PA: [-5,1],
		AZ: [-7,0]
		// state: [GMT offset, 0 = does not observe DST or 1 = does observe DST]
	};
	
	if(!states[s]) {
		return 0;
	}

	var dst_on = new Date();
	var dst_off = new Date();
	var day, n;
	
	dst_on.setMonth(2);
	dst_on.setDate(1);
	dst_on.setHours(1, 59, 59, 999);
	day = dst_on.getDay();
	n = (7 - day) % 7 + 7;
	dst_on.setDate(1+n); // second Sunday in March
	
	dst_off.setMonth(10);
	dst_off.setDate(1);
	dst_off.setHours(1, 59, 59, 999);
	day = dst_off.getDay();
	n = (7 - day) % 7;
	dst_off.setDate(1+n); // first Sunday in November
	
	var date = new Date();
	var dst = (dst_on < date && date <= dst_off) ? 1 : 0;
	
	return states[s][0] + dst * states[s][1];
}

/*
	Function: getUKOffset
	Arguments:
		c (string) - location/city
	Returns:
		On success, returns GMT offset of location taking BST into account
		On failure, returns 0
	
	Notes:
		Same limitations as getStateOffset above on the days that BST state changes.
*/
function getUKOffset(c) {
	var locations = {
		london: [-0,1],
		wimbledon: [-0,1]
		// location: [GMT offset , 0 = BST not observed or 1 = BST observed]
	}
	
	if(!locations[c]) {
		return 0;
	}
	
	var bst_on = new Date();
	var bst_off = new Date();
	
	bst_on.setMonth(2);
	bst_on.setDate(31);
	bst_on.setHours(0, 59, 59, 999);
	bst_on.setDate(31 - bst_on.getDay()); // last Sunday in March
	
	bst_off.setMonth(9);
	bst_off.setDate(31);
	bst_off.setHours(0, 59, 59, 999);
	bst_off.setDate(31 - bst_off.getDay()); // last Sunday in October
	
	var date = new Date();
	var bst = (bst_on < date && date <= bst_off) ? 1 : 0;
	
	return locations[c][0] + bst * locations[c][1];
}

/*
	Functions for printing time
*/
function printRemoteTime(s, fnOffset) {
	var local = new Date();
	var remote = new Date();
	
	var local_offset = (-1) * local.getTimezoneOffset() * 60 * 1000;
	var remote_offset = fnOffset(s) * 60 * 60 * 1000;
	
	remote.setTime(local.getTime() - local_offset + remote_offset);
	
	var hours = remote.getHours();
	var minutes = remote.getMinutes();
	
	var hours_print = (hours + 12 - 1) % 12 + 1;
	var minutes_print = (minutes < 10) ? '0' + minutes : minutes;
	var suffix = (hours < 12) ? 'AM' : 'PM';
	
	document.write('<div>' + hours_print + ':' + minutes_print + ' ' + suffix + '</div>');
}

function printStateTime(s) {
	printRemoteTime(s, getStateOffset);
}

function printUKTime(s) {
	printRemoteTime(s, getUKOffset);
}
