
var arDayNames = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");

function City(dayID, timeID, offsetHours, offsetMins, altOffsetHours, altOffsetMins, altStartDay, altStartHours, altStartMins)
{
	// IDs of controls that display the day and time
	this.dayID = dayID;
	this.timeID = timeID;

	// this is the primary TZ offset
	this.offsetHours = offsetHours;
	this.offsetMins = offsetMins;

	// the TZ offset if the offset changes during the day
	this.altOffsetHours = altOffsetHours;
	this.altOffsetMins = altOffsetMins;

	// the UTC time that the alt TZ offset should start to be applied
	this.altStartDay = altStartDay;
	this.altStartHours = altStartHours;
	this.altStartMins = altStartMins;

	// the local time
	this.day = 0;
	this.hours = 0;
	this.mins = 0;
}

City.prototype.checkDay = City_checkDay;
City.prototype.checkHour = City_checkHour;
City.prototype.checkMin = City_checkMin;
City.prototype.checkLocalTime = City_checkLocalTime;
City.prototype.calcLoaclTime = City_calcLoaclTime;

City.prototype.localDay = City_localDay;
City.prototype.localHour = City_localHour;
City.prototype.localMin = City_localMin;
City.prototype.localAMPM = City_localAMPM;
City.prototype.localTime = City_localTime;
City.prototype.updateDisplay = City_updateDisplay;

function City_checkDay()
{
	if (this.day > 6)
		this.day -= 7;
	if (this.day < 0)
		this.day += 7;
}

function City_checkHour()
{
	if (this.hours > 23)
	{
		this.day += 1;
		this.hours -= 24;
	}
	if (this.hours < 0)
	{
		this.day -= 1;
		this.hours += 24;
	}
}

function City_checkMin()
{
	if (this.mins > 59)
	{
		this.hours += 1;
		this.mins -= 60;
	}
	if (this.mins < 0)
	{
		this.hours -= 1;
		this.mins += 60;
	}
}

function City_checkLocalTime()
{
	this.checkMin();
	this.checkHour();
	this.checkDay();
}

function City_calcLoaclTime(utcDay, utcHours, utcMins)
{
	if (utcDay == this.altStartDay && utcHours >= this.altStartHours && utcMins >= this.altStartMins)
	{
		this.day = utcDay;
		this.hours = utcHours + this.altOffsetHours;
		this.mins = utcMins + this.altOffsetMins;
	}
	else
	{
		this.day = utcDay;
		this.hours = utcHours + this.offsetHours;
		this.mins = utcMins + this.offsetMins;
	}

	this.checkLocalTime();
}

function City_localDay()
{
	return arDayNames[this.day];
}

function City_localHour(fmt24)
{
	if (fmt24)
	{
		if (this.hours < 10)
			return "0" + this.hours;
		return this.hours;
	}

	// am pm
	if (this.hours == 0)
		return "12";
	else if (this.hours > 12)
		return this.hours - 12;

	return this.hours;
}

function City_localMin()
{
	if (this.mins < 10)
		return "0" + this.mins;
	return this.mins;
}

function City_localAMPM(fmt24)
{
	if (fmt24)
		return "";
	if (this.hours > 11)
		return " PM";
	return " AM";
}

function City_localTime(fmt24)
{
	return this.localHour(fmt24) + ":" + this.localMin() + this.localAMPM(fmt24);
}

function City_updateDisplay(fmt24)
{
	var dayElem = document.getElementById(this.dayID);
	if (!dayElem || !dayElem.innerHTML)
		return;

	var timeElem = document.getElementById(this.timeID);
	if (!timeElem || !timeElem.innerHTML)
		return;

	dayElem.innerHTML = this.localDay();
	timeElem.innerHTML = this.localTime(fmt24);
}

