function Countdown(appendToElement, dateStr)
{
	this.appendToElement = appendToElement;
	this.date = new Date(dateStr);
	this.currentDate = new Date();
	this.countdown = new Date();
	this.ele = null;
	this.difference = null;
	this.draw();

	var closedRef = this;

	setInterval(function() { closedRef.update() }, 1000);
}

Countdown.prototype.draw = function()
{
	var ele = document.createElement("div");

	ele.innerHTML = this.getOuputDate();
	
	this.ele = ele;
	this.appendToElement.appendChild(ele);
}

Countdown.prototype.getDaysInMonth = function(monthID)
{
	switch(monthID) {
		case 0:	 return 31; 
		case 1:	 return 28; 
		case 2:	 return 31; 
		case 3:	 return 30;
		case 4:	 return 31;
		case 5:	 return 30;
		case 6:	 return 31;
		case 7:	 return 31;
		case 8:	 return 30;
		case 9:	 return 31;
		case 10: return 30;
		case 11: return 31; 
	}

	return false;
}

Countdown.prototype.getOuputDate = function()
{
	this.difference = this.date.getTime() - (new Date()).getTime();
	this.countdown.setTime(this.difference);
	if (this.difference <= 0)
	{
		return "-- -- --";
	}
	var days = (this.countdown.getFullYear() - 1970) * 365;
	
	days += this.countdown.getMonth() * this.getDaysInMonth();
	days += this.countdown.getDate() - 1;

	var hours = this.countdown.getHours();
	
	var minutes = this.countdown.getMinutes() + 1;
	if (minutes < 10) minutes = "0" + minutes;

	var seconds = this.countdown.getSeconds();
	if (seconds < 10) seconds = "0" + seconds;
	
	var dayDays = (days != 1) ? "Days" : "Day";

	return ("SALE ENDS IN<br/>" + days + " " + dayDays + " " + hours + ":" + minutes + ":" + seconds + "s");
}

Countdown.prototype.update = function()
{
	if (this.ele)
	{
		this.ele.innerHTML = this.getOuputDate();
	}
}