function Auction() {
	
	this.products = new Array();
	
}

Auction.prototype.products = null;
Auction.prototype.timerInterval = null;

Auction.prototype.registerProduct = function(element, time) {
	
	this.products[this.products.length] = {'element': element,
											'time': time};
	
}

Auction.prototype.start = function() {
	
	// make sure no other timer is running
	if (this.timerInterval === null) {
	
		var _obj = this;
		
		// set new interval
		this.timerInterval = setInterval(function() {
			_obj.onUpdateTime();
		},1000);
	}
	
}

Auction.prototype.stop = function() {
	
	// check whether the timer is set
	if (this.timerInterval !== null) {
		clearInterval(this.timerInterval);
		this.timerInterval = null;
	}
	
}

Auction.prototype.onUpdateTime = function() {
	
	// update the time of all products
	for (var index in this.products) {
		
		var product = this.products[index]; 
		
		try {

			// decrement time
			this.products[index]['time']--;
			
			if (this.products[index]['time'] < 0)
				product['element'].innerHTML = '<strong>Deze veiling is afgelopen</strong>';
			else
				product['element'].innerHTML = this.formatTime(product['time']);
			
		} catch (e) {
		}
		
	}
	
}

Auction.prototype.formatTime = function(time) {
	
	// days
	var timeStr = '' + Math.floor(time / (24*3600)) + 'd';
	
	// hours
	timeStr += ' ' + Math.floor((time % (24*3600)) / 3600) + 'h';
	
	// minutes
	timeStr += ' ' + Math.floor((time % 3600) / 60) + 'm';
	
	// seconds
	timeStr += ' ' + Math.floor(time % 60) + 's';
		
	return timeStr;
	
}

// initialize
var auction = new Auction();
auction.start();
