/**
* Source.........: $HeadURL: http://webextra.startcms.nl/svn/regma2/trunk/htdocs/templates/jscripts/auctiontimer.js $
* Project........: REGMA
* Last Modified..: $LastChangedDate: 2011-07-15 00:00:53 +0200 (vr, 15 jul 2011) $
* SVN Revision...: $LastChangedRevision: 537 $
*/
/*******************************************************************************
 * AuctionTimer class
 *******************************************************************************/
var AuctionTimer = Class.create();
AuctionTimer.prototype = {
	initialize: function(element, year,month,day,hour,minutes, timedifference) {
		this.timerRunning = false;
		this.timeDifference = timedifference;
		this.endTime = new Date(year,month,day,hour,minutes);
		this.endTimeInMilliSeconds = this.endTime.getTime();
		this.element = $(element);
		this.startClock();
		this.onAuctionClosed = null;
		this.options = arguments.length > 7 ? arguments[7] : {};
	}
	, show: function(){
		var now = new Date();
		var nowMilliSecs = now.getTime();
		this.remainingSeconds = Math.floor((this.endTimeInMilliSeconds-nowMilliSecs+this.timeDifference)/1000);

		// check time left
		if(this.remainingSeconds <= 0){
//			this.stopClock();
			this.element.update('Deze veiling is gesloten');
			if(this.onAuctionClosed){
				this.onAuctionClosed.call();
			}
		} else {
			var nDays = Math.floor(this.remainingSeconds / 86400);
			var nHours = Math.floor(this.remainingSeconds / 3600)%24;
			var nMinutes = Math.floor(this.remainingSeconds / 60)%60;
			var nSeconds = this.remainingSeconds % 60;
			var sTime = '';
			if(nDays>0){
				if(this.options.shortnotation){
					sTime = nDays + (nDays==1 ? ' dag ' : ' dagen ') + (nHours>0 ? nHours+'u' : '');
				} else {
					sTime = nDays + (nDays==1 ? ' dag ' : ' dagen ') + (nHours>0 ? nHours+' uur' : '');
				}
			} else if(nDays==0 && nHours>0){
				sTime = nHours+'u '+this.twoDigits(nMinutes)+'m';
			} else {
				sTime = this.twoDigits(nMinutes)+'m '+this.twoDigits(nSeconds)+'s';
			}
			this.element.update(sTime);
		}
	}
	, showTime: function(){
		if(this.timerRunning){
			this.show();
		}
	}
	, startClock: function(){
		this.timerRunning = true;
		this.updater = new PeriodicalExecuter(this.show.bind(this), 1);
	}
	, stopClock: function(){
		this.updater.stop();
		this.timerRunning = false;
	}
	// add a '0'
	, twoDigits: function(nr){
		if(nr>-10 && nr<0){
			return String('- 0'+ Math.abs(nr));
		} else if(nr>=0 && nr<10) {
			return String('0'+ nr);
		} else {
			return String(nr);
		}
	}


}

