function CountDownTimer(seconds) {
	this.timeout;  // timeout
	this.values;   // array
	this.renderer; // class

	this.setCountdown = function(seconds) {
		this.values = Array();
		this.values['days']    = Math.floor(seconds / 86400);
		seconds -= this.values['days'] * 86400;
		this.values['hours']   = Math.floor(seconds / 3600);
		seconds -= this.values['hours'] * 3600;
		this.values['minutes'] = Math.floor(seconds / 60);
		seconds -= this.values['minutes'] * 60;
		this.values['seconds'] = seconds;

		if (this.isPositive()) {
			this.timeout = setTimeout(runWithContext(this, this.eventHandler), 1000);
		}
	}

	this.setRenderer = function(renderer) {
		this.renderer = renderer;
		this.redraw();
	}

	this.isPositive = function() {
		return (
			this.values['seconds'] > 00 ||
			this.values['minutes'] > 00 ||
			this.values['hours'] > 00 ||
			this.values['days'] > 00
		);
	}

	this.decrement = function() {
		this.values['seconds']--;

		this.carryCheck('seconds', 'minutes', 60);
		this.carryCheck('minutes', 'hours', 60);
		this.carryCheck('hours', 'days', 24);
	}

	this.eventHandler = function() {
		this.decrement();
		this.redraw();

		if (this.isPositive()) {
			this.timeout = setTimeout(runWithContext(this, this.eventHandler), 1000);
		}
	}

	this.carryCheck = function(lesser, higher, modulus) {
		if (this.values[lesser] == -01) {
			this.values[higher]--;
			this.values[lesser] = modulus -1;
		}
	}

	this.redraw = function() {
		if (this.renderer == null) {
			return;
		}

		this.renderer.update(this.values);
	}

	this.setCountdown(seconds); // TODO: move me and make me proper.
}

function CountDownDrawHtml(elm) {
	this.elm = elm;
	this.values;

	this.update = function(values) {
		this.values = values;
		this.redraw();
	}

	this.redraw = function() {
		this.elm.innerHTML = this.values['days'] + "d " + this.values['hours'] + "h " + this.values['minutes'] + "m " + this.values['seconds'] + "s";
	}
}

