function countdown(obj) {
	this.obj = obj;
	this.Div = "clock";
	this.TargetDate = "12/31/2020 5:00 AM";
	this.CountActive = true;

	this.TimerID = 0;
	this.LastRecalc = "";
	
	this.Calcage = cd_Calcage;
	this.CountBack = cd_CountBack;
	this.Setup = cd_Setup;
}

function cd_Calcage(secs, num1, num2) {
	s = ((Math.floor(secs/num1))%num2).toString();
	//if (s.length < 2) s = "0" + s;
	return (s);
}

function cd_CountBack(secs) {
	if(secs < 0) {
		document.getElementById(this.Div).innerHTML = "Auction Ended";
	} else {
		var days = this.Calcage(secs,86400,100000);
		var hours = this.Calcage(secs,3600,24);
		var minutes = this.Calcage(secs,60,60);
		var seconds = this.Calcage(secs,1,60);
		
		var newString = "";
		if(days != 0) { 
			var plural = (days > 1) ? "s" : "";
			newString = days + " day" + plural + ", ";
		}
		if((days != 0) || (hours != 0)) {
			var plural = (hours > 1) ? "s" : "";
			if(days > 0) { hours = hours + "+"; }
			newString = newString + hours + " hour" + plural;
		}
		if((days == 0) && (minutes != 0)) {
			var plural = (minutes != 1) ? "s" : "";
			var comma = ((days == 0) && (hours == 0)) ? "" : ", ";
			newString = newString + comma + minutes + " min" + plural;
		}
		if((days == 0) && (hours == 0) && (minutes < 60)) { 
			var comma = (minutes != 0) ? ", " : "";
			newString  = newString + comma + seconds + " secs";
		}
		
		if(document.getElementById(this.Div)) {
			document.getElementById(this.Div).innerHTML = newString;
		} else {
			this.CountActive = false;
		}
		
		if (this.CountActive) { 
			this.TimerID = setTimeout(this.obj +".CountBack(" + (secs-1) + ")", 990);
		}
		
		if(!this.LastRecalc) { this.LastRecalc = secs; }
		else if(this.LastRecalc - secs > 10) { this.LastRecalc = secs; clearTimeout(this.TimerID); this.Setup(); }
	}
}

function cd_Setup() {
	var dthen	= new Date(this.TargetDate);
	var dnow	= new Date();
	ddiff		= new Date(dthen-dnow);
	gsecs		= Math.floor(ddiff.valueOf()/1000);
	this.CountBack(gsecs);
}

