/********************************
	 Conference Countdown
 
	 (c) 2006 MJ Bytes Ltd.
	 Written by Miro Jurik

	 JavaScript 1.0 and higher
*********************************/

// Usage: conf = new CountDown("April 29, 2006", "14:00:00", "PERTH"); 


function CountDown(cDate, cTime, cCity)
{
	this.conference = new Date(cDate + " " + cTime + " " + this.getTimeOffset(cCity));
	this.multi_x = 60 * 1000;
	this.multi_y = 60 * this.multi_x;
	this.running = false;
	this.tm = 0;
	this.d = this.getElement("cd_d");
	this.h = this.getElement("cd_h");
	this.m = this.getElement("cd_m");
	this.s = this.getElement("cd_s");
}

CountDown.prototype = 
{
	startCountdown: function()
	{
		this.stopCountdown();
		this.displayCountdown();
	},

	stopCountdown: function()
	{
		if (this.running) clearTimeout(this.tm);
		this.running = false;
	},

	displayCountdown: function()
	{
		var self = this;
		var now = new Date();
		var togo = self.conference.getTime() - now.getTime();
		var days = Math.floor(togo / (24 * this.multi_y));
		var hours = Math.floor((togo / (this.multi_y)) - days * 24);
	 	var minutes = Math.floor((togo - (hours * this.multi_y + days * 24 * this.multi_y)) / this.multi_x);
		var seconds = Math.floor((togo - (minutes * this.multi_x + hours * this.multi_y + days * 24 * this.multi_y)) / 1000);
		
		if (days < 0)
		{
			this.stopCountdown();
		}
		else
		{
			self.d.innerHTML = days;
			self.h.innerHTML = hours < 10 ? "0" + hours : hours;
			self.m.innerHTML = minutes < 10 ? "0" + minutes: minutes;
			self.s.innerHTML = seconds < 10 ? "0" + seconds : seconds;
		
			this.tm = setTimeout(function(){self.displayCountdown()}, 1000);
			this.running = true;
		}
	},

	getTimeOffset: function(city)
	{
		switch (city)
		{
			case "SYDNEY":
			case "MELBOURNE":
			case "CANBERRA":
			case "HOBART":
			case "BRISBANE":
				return "UTC+1000";

			case "PERTH":
				return "UTC+0800";

			case "ADELAIDE":
			case "DARWIN":
				return "UTC+0930";

			default:
				return "UTC+1000";
		}
	},

	getElement: function(element)
	{
		return document.getElementById 
			? document.getElementById(element) : (document.all 
			? document.all[element] : (document.layers 
			? document.layers[element] : -1));
	}
}

