/*******************************************************************
 * Function: checkTimeZone
 *
 * Description: Takes the client's local time, converts it to GMT
 * time, and determines if the client is in daylight savings or
 * not.  Once that is determined, the correct hour offset for PST
 * is returned.  
 *
 * Returns: -8 (no daylight savings)
 *          -7 (daylight savings)
 *******************************************************************/
function checkTimeZone()
{
	var rightNow = new Date();
	var date1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);
	var date2 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0);
	var temp = date1.toGMTString();
	var date3 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
	var temp = date2.toGMTString();
	var date4 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
	var hoursDiffStdTime = (date1 - date3) / (1000 * 60 * 60);
	var hoursDiffDaylightTime = (date2 - date4) / (1000 * 60 * 60);
   
	if (hoursDiffDaylightTime == hoursDiffStdTime)
	{
		// Daylight Saving Time is NOT observed here
		return -8;
	}
	else
	{
		// Daylight Saving Time is observed here
		return -7;
	}
}

/*********************************************************************
 * Function: getTimeDiff
 *
 * Description: Gets the client's local time, converts it to PST time, 
 * then calculates the difference between that and midnight.  
 *
 * Returns: time difference in milliseconds
 *********************************************************************/
function getTimeDiff()
{
	// need to calculate the PST time relative to the client's time
	var d = new Date();
	var localTime = d.getTime();
	// obtain local UTC offset and convert to msec
	var localOffset = d.getTimezoneOffset() * 60000;
	// obtain UTC time in msec
	var utc = localTime + localOffset;
	// obtain and add destination's UTC time offset
	offset = checkTimeZone();
	var san_diego = utc + (3600000 * offset);
	// calculate the target date
	var target = new Date(san_diego);
	// this date is in the client's local time.  Must convert to PST
	target.setDate(target.getDate() + 1);
	target.setHours(0);
	target.setMinutes(0);
	target.setSeconds(0);
	target = target.getTime();
	// get todays date
	var today = (new Date(san_diego)).getTime();
	// get the difference
	var time = target - today;
	// return the difference
	return time;
}

/***************************************************************************
 * Function: repeat
 *
 * Description: Function to call the down() function repeatedly every 1000
 * milliseconds (1 second).  
 ***************************************************************************/
function repeat()
{
	down();
	setTimeout("repeat()",1000);
}

/***************************************************************************
 * Function: down
 *
 * Description: Function to subtract 1 second from the time and display the
 * updated time on the site.  
 ***************************************************************************/
function down()
{
	var out = '';
	var time_text = '';
	// update time by subtracting 1 second
	var temp_time = document.getElementById('time').childNodes[0].nodeValue;
	temp_time = temp_time - 1000;

	// check to see if temp_time is 0 or negative
	if (temp_time <= 0)
	{
		// just passed midnight, must recalculate
		temp_time = getTimeDiff();
	}

	// remove old value
	var element = document.getElementById("time");
	while (element.firstChild)
	{
		element.removeChild(element.firstChild);
	}
	// update with new value
	var temp = document.createTextNode(temp_time)
	document.getElementById('time').appendChild(temp);
	// get the time
	var time = document.getElementById('time').childNodes[0].nodeValue;

	var days = (time - (time % 86400000)) / 86400000;
	time = time - (days * 86400000);
	var hours = (time - (time % 3600000)) / 3600000;
	time = time - (hours * 3600000);
	var mins = (time - (time % 60000)) / 60000;
	time = time - (mins * 60000);
	var secs = (time - (time % 1000)) / 1000;
	
	if (hours < 10)
		out = out + "0";
	out = out + hours + ":";
	if (mins < 10)
		out = out + "0";
	out = out + mins + ":";
	if (secs < 10)
		out = out + "0";
	out = out + secs;
	if (days + hours + mins + secs > 1) 
	{
		// remove old time
		var element1 = document.getElementById("countdown");
		while (element1.firstChild)
		{
			element1.removeChild(element1.firstChild);
		}
		time_text = document.createTextNode(out);
		document.getElementById('countdown').appendChild(time_text);
	}
	else 
	{
		// remove old time
		var element1 = document.getElementById("countdown");
		while (element1.firstChild)
		{
			element1.removeChild(element1.firstChild);
		}
		time_text = document.createTextNode('00:00:00');
		document.getElementById('countdown').appendChild(time_text);
	}
}
// initialization code
var time = getTimeDiff();
var temp = document.createTextNode(time);
document.getElementById('time').appendChild(temp);
repeat();
