
Countdown = function(expireMillis, countdownDivId, expireCallback) {
	this.expireMillis = expireMillis;
	this.countdownDivId = countdownDivId;
	this.expireCallback = arguments.length > 2 ? expireCallback : null;
};

Countdown.prototype = {

	initialize: function()  {
		this.countdownDiv = $(this.countdownDivId);
		this.onTimeout();
	},
	
	onTimeout: function() {
		var date = new Date();
		var time = date.getTime();
		if(time >= this.expireMillis) {
			this.expireCallback(this.countdownDivId);
		} else {
			var timeDiff = this.expireMillis - time;
			var days = Math.floor(timeDiff / 86400000);
			var hours = Math.floor((timeDiff % 86400000) / 3600000);
			var minutes = Math.floor((timeDiff % 3600000) / 60000);
			var seconds = Math.floor((timeDiff % 60000) / 1000);
			var tenthSeconds = Math.floor((timeDiff % 1000) / 100);
			
			var content = '';
			if(days == 0 && hours == 0 && minutes == 0) {
				this.showTenths = true;
				content = seconds + '.' + tenthSeconds;
			} else {
				if(days > 0) 
					content += days + ':';
				content += hours < 10 && days > 0 ? '0' + hours : hours;
				content += ':';
				content += minutes < 10 && (hours > 0  || days > 0) ? '0' + minutes : minutes;
				content += ':';
				content += seconds < 10 ? '0' + seconds : seconds;
				this.showTenths = false;
			}
			this.countdownDiv.innerHTML = content;
			this.setTimer();
		}
	}, 
	
	setTimer: function() {
		this.timeoutId = setTimeout(this.onTimeout.bind(this), this.showTenths ? 100 : 1000);
	}
	
};
