function cdtime(container, targetdate, currentdate, displayType, endFunc) {
	if (!document.getElementById || !document.getElementById(container)) {
		return;
	}
	this.containerName = container;
	this.container = document.getElementById(container);
	this.currentTime = new Date(currentdate);
	this.targetdate = new Date(targetdate);
	
	if (displayType == 'image') {
		// days
		if (this.GetTime('day') > 0) {
			for(x = 0; x < this.GetTime('day').toString().length; x++) {
				this.container.innerHTML += '<img id="' + this.containerName + 'day' + x + '"/>';
			}
			this.container.innerHTML += (this.GetTime('day') > 1) ? '<img src="/images/auctions/digital_days.gif"><br/>' : '<img src="/images/auctions/digital_day.gif"><br/>';
		}
		
		// time
		var colon_img = '<img src="/images/auctions/digital_colon.gif">';
		this.container.innerHTML += '<img id="' + this.containerName + 'hrs0"/><img id="' + this.containerName + 'hrs1"/>' + colon_img + '<img id="' + this.containerName + 'min0"/><img id="' + this.containerName + 'min1"/>' + colon_img + '<img id="' + this.containerName + 'sec0"/><img id="' + this.containerName + 'sec1"/>';
		
		// preload digit images
		this.digits = new Array();
		for (x=0; x<=9; x++) {
			this.digits[x] = new Image();
			this.digits[x].src = '/images/auctions/'+x+'b.gif';
		}
	}
	
	this.displayType = displayType;
	this.endFunc = endFunc;
	this.timesup = false;
	this.timer = 0;
	this.updateTime();
}

cdtime.prototype.updateTime=function() {
	var thisobj = this;
	this.currentTime.setSeconds(this.currentTime.getSeconds() + 1);
	if (this.timesup == false) {
		this.timer = setTimeout(function(){thisobj.updateTime()}, 1000) ;
	}
}

cdtime.prototype.displaycountdown=function(baseunit, functionref) {
	this.baseunit = baseunit;
	this.showresults();
}

cdtime.prototype.GetTime=function(timeFormat) {
	var timediff = (this.targetdate-this.currentTime) / 1000;
	var oneMinute = 60;
	var oneHour = 60 * 60;
	var oneDay = 60 * 60 * 24;
	switch(timeFormat) {
		case 'day' :
			return dayfield=Math.floor(timediff/oneDay);
		case 'hour' :
			return hourfield=Math.floor((timediff-dayfield*oneDay)/oneHour);
		case 'minute' :
			return minutefield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour)/oneMinute);
		case 'second' :
			return secondfield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour-minutefield*oneMinute));
	}
}

cdtime.prototype.showresults=function() {
	var thisobj = this;
	var timediff = (this.targetdate-this.currentTime)/1000;
	if (timediff<0) {
		clearTimeout(this.timer);
		this.timesup = true;
		setTimeout(this.endFunc, 0);
		return;
	}

	var dayfield = this.GetTime('day');
	var hourfield = this.GetTime('hour');
	var minutefield = this.GetTime('minute');
	var secondfield = this.GetTime('second');
	
	if (this.baseunit == "hours") { 
		hourfield = dayfield * 24 + hourfield;
		dayfield = "n/a";
	}
	else if (this.baseunit=="minutes") {
		minutefield = dayfield * 24 * 60 + hourfield * 60 + minutefield;
		dayfield = hourfield = "n/a";
	}
	else if (this.baseunit=="seconds") { 
		var secondfield = timediff;
		dayfield = hourfield=minutefield = "n/a";
	}

	// image version
	if (this.displayType == 'image') {
		if (dayfield > 0) {
			day = dayfield.toString();
			for(x = 0; x < day.length; x++) {
				document.getElementById(this.containerName + 'day' + x).src = this.digits[day.charAt(x)].src;
			}
		}
		document.getElementById(this.containerName + 'hrs0').src = this.digits[get_two_digits(hourfield).charAt(0)].src;
		document.getElementById(this.containerName + 'hrs1').src = this.digits[get_two_digits(hourfield).charAt(1)].src;
		document.getElementById(this.containerName + 'min0').src = this.digits[get_two_digits(minutefield).charAt(0)].src;
		document.getElementById(this.containerName + 'min1').src = this.digits[get_two_digits(minutefield).charAt(1)].src;
		document.getElementById(this.containerName + 'sec0').src = this.digits[get_two_digits(secondfield).charAt(0)].src;
		document.getElementById(this.containerName + 'sec1').src = this.digits[get_two_digits(secondfield).charAt(1)].src;
		
	// text version
	} else {
		textDays = (dayfield < 2) ? dayfield + ' day ' : dayfield + ' days ';
		textDays = (dayfield < 1) ? '' : textDays;
		this.container.innerHTML = textDays + get_two_digits(hourfield) + ':' + get_two_digits(minutefield) + ':' + get_two_digits(secondfield);
	}
	
	setTimeout(function(){thisobj.showresults()}, 1000)
}

// deprecated: remove when all calling layers are fixed
function formatresults() {
}

function get_two_digits(digit) {
	var sdigit = digit.toString();
	return (digit<10) ? '0' + sdigit : sdigit;
}