/*
Written by James Baker (jabaker) for CNNSI.com
Copyright 2002
Counts down the time to an event, and then switches over to merely displaying the time of day.
Requires certain form fields exist on the page, and two parameters to be given when started.
version 1.0.3
build 20020212f
*/

var clock = new Object;
var TickTock = null;
clock.id = null;
clock.isRunning = false;

/* StartClock() should be called with a formated date string and timezone offset in hours
	(!! Only non-fractional hours are currently accepted !!)
	e.g.: body onload="StartClock('May 31, 2002 20:30:00 GMT +0900',9);"
 */
function StartClock (zeroHour, destTz) {
	clock.zeroHour = new Date(zeroHour);
	clock.destTz = destTz;
	StopClock();
	UpdateClock();
	return true;
}

function StopClock () {
	if (clock.isRunning) {
		clearTimeout(clock.id);
		clock.isRunning = false;
	}
}

function UpdateClock () {
	if (document.clockface) {
		clock.today = new Date();
		clock.remaining = new Object;
		clock.remaining.counter = (clock.zeroHour.getTime() - clock.today.getTime());
		if (clock.remaining.counter <= 0) {
			var tzDiff = clock.today.getTimezoneOffset()/60;
			var gmt = (clock.today.getHours() + tzDiff);
			var destHrs = Bounds(gmt + clock.destTz);
			clock.today.days = 'Time:';
			clock.today.hours = ZeroFill((destHrs >= 0) ? destHrs : destHrs + 24); // you have to be careful with negative TZ offsets
			clock.today.minutes = ZeroFill(clock.today.getMinutes());
			clock.today.seconds = ZeroFill(clock.today.getSeconds());
			clock.today.formated = Make12Hour(clock.today.hours + ":" + clock.today.minutes + ":" + clock.today.seconds);
			clock.isRunning = true;
		} else if (clock.remaining.counter > 0) {
			var msPerDay = 24 * 60 * 60 * 1000;
			/* divide up the units and round off the fractions for each one */
			var daysLeft = clock.remaining.counter / msPerDay;
			clock.remaining.days = (Math.floor(daysLeft) ? Math.floor(daysLeft) : 0);
			var hrsLeft = (daysLeft - clock.remaining.days)*24;
			clock.remaining.hours = ZeroFill(Math.floor(hrsLeft));
			var minsLeft = (hrsLeft - clock.remaining.hours)*60;
			clock.remaining.minutes = ZeroFill(Math.floor(minsLeft));
			var secsLeft = (minsLeft - clock.remaining.minutes)*60;
			clock.remaining.seconds = ZeroFill(Math.floor(secsLeft));
			clock.remaining.formated = clock.remaining.days + ((clock.remaining.days == 1) ? ' day, ' : ' days, ')
				+ clock.remaining.hours + ':' + clock.remaining.minutes + ':' + clock.remaining.seconds;
			clock.isRunning = true;
		} else {
			clock.isRunning = false;
		}
	} else {
		return false;
	}
	ShowTime();
	clock.id = setTimeout("UpdateClock()",100);
}

function ShowTime() {
	/* put the values into the appropriate (i.e. existing) form field */
	if (document.clockface.hours && document.clockface.minutes && document.clockface.seconds) {
		document.clockface.days.value = ((clock.remaining.counter > 0) ? clock.remaining.days : clock.today.days);
		document.clockface.hours.value = ((clock.remaining.counter > 0) ? clock.remaining.hours : clock.today.hours);
		document.clockface.minutes.value = ((clock.remaining.counter > 0) ? clock.remaining.minutes : clock.today.minutes);
		document.clockface.seconds.value = ((clock.remaining.counter > 0) ? clock.remaining.seconds : clock.today.seconds);
		return true;
	} else if (document.clockface.combo) {
		document.clockface.combo.value = (clock.today.formated ? clock.today.formated : clock.remaining.formated);
		return true;
	} else if (document.clockface.justdays) {
		document.clockface.justdays.value = ((clock.remaining.counter > 0) ? clock.remaining.days : clock.today.days);
	}
	return false;
}

// support functions
function ZeroFill(num) {
	return ((num <= 9) ? ("0" + num) : num);
}

function Bounds(h) {
	return (h >= 24) ? h - 24 : h;
}

function Make12Hour(time) {
	var parts = time.split(":");
	am = true;
	if (parts[0] > 0) {
		parts[0] -= 1; // avoid 12,24 special am/pm cases
		if (parts[0] >= 11) { // that would be 13-23 outside of this -1/+1 trip
			am = false;
			if (parts[0] >= 12) parts[0] -= 12;
		}
		parts[0] += 1; // return to proper hour
	} else {
		parts[0] = 12; // if it's zero, it's 12am
	}
	parts[0] = ZeroFill(parts[0]);
	return parts.join(":") + ((am) ? " am" : " pm");
}
