/********************************
	 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("days");
	this.h = this.getElement("hours");
	this.m = this.getElement("minutes");
	this.s = this.getElement("seconds");
}

CountDown.prototype.startCountdown = function()
{
	this.stopCountdown();
	this.displayCountdown();
}

CountDown.prototype.stopCountdown = function()
{
	if (this.running) clearTimeout(this.tm);
	this.running = false;
}

CountDown.prototype.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;
		}
}

CountDown.prototype.getTimeOffset = function(city)
{
	if (city == "SYDNEY" || city == "MELBOURNE" || city == "CANBERRA" || city == "HOBART" || city == "BRISBANE") return "UTC+1000";
	else if (city == "PERTH") return "UTC+0800";
	else if (city == "ADELAIDE" || city == "DARWIN") return "UTC+0930";
	else return "UTC+1000";
}

CountDown.prototype.getElement = function(element)
{
	return document.getElementById 
	? document.getElementById(element) : document.all 
	? document.all[element] : document.layers 
	? document.layers[element] : -1;
}


