//var timerLength = 60000
var timerLength = 1000

function setElement(id, value) {
	if (value.length < 2)
		value = "0" + value
	window.document.getElementById(id).innerHTML = value
}

function pad(num,size) {
	num = "000000000" + num
	return num.substr(num.length - size)
}

function countdownMain(then) {

	then = new Date(then)
	this.then = then

	function countdown() {
		now  		  = new Date()
		diff		  = new Date(this.then - now)

		seconds_left  = Math.floor(diff.valueOf() / 1000)

		seconds  = pad(Math.floor(seconds_left / 1) % 60, 2)
		minutes  = pad(Math.floor(seconds_left / 60) % 60, 2)
		hours    = Math.floor(seconds_left / 3600) % 24
		days     = Math.floor(seconds_left / 86400) % 86400

		//  if not using days add up hours
		//setElement('countdown-days', days)
		hours += days * 24

		setElement('countdown-hours', hours)
		setElement('countdown-minutes', minutes)
		setElement('countdown-seconds', seconds)

		countdown.timer = setTimeout(countdown, timerLength)
	}

	function start() {
		this.timer = setTimeout(countdown, timerLength)
	}
	start(then)

	countdown()
}
