// version 0.1
var mooCountdown = new Class({
	options: {
		'countActive': true,
		'countStepper': 1,
		'diffServer':0,
		'display': '<span class="chiffres">%%D%%</span> jrs, <span class="chiffres">%%H%%</span> h <span class="chiffres">%%M%%</span> mins <span class="chiffres">%%S%%</span> sec'
	},
	
	initialize: function(countdown, options){
		this.count = 0;
		this.countDown = $(countdown);
		this.countDown.setStyle('display', 'none');
		this.targetDate = Date.parse(this.countDown.getText());
		this.setOptions(options);
		
		this.options.countStepper = Math.ceil(this.options.countStepper);
		if (this.options.countStepper == 0)
			this.options.countActive = false;
		var setTimeOutPeriod = (Math.abs(this.options.countStepper))*1000;
		
		var dnow = new Date();
		var ddiff = new Date(this.targetDate-dnow-this.options.diffServer);
		this.gSecs = Math.floor(ddiff.valueOf()/1000);
		
		delete dnow, ddiff;
		
		this.countback();
		this.countDown.setStyle('display', '');
		this.countback.periodical(1000, this);
	},
	
	countback: function(){
		this.gSecs = this.gSecs-1;
		this.countDown.setHTML(this.display());
	},
		
	display: function(){
		return this.options.display
			.replace(/%%D%%/g, this.calcage(this.gSecs, 86400, 100000))
			.replace(/%%H%%/g, this.calcage(this.gSecs, 3600, 24))
			.replace(/%%M%%/g, this.calcage(this.gSecs, 60, 60))
			.replace(/%%S%%/g, this.calcage(this.gSecs, 1, 60));
	},
	
	calcage: function(secs, num1, num2) {
	  s = ((Math.floor(secs/num1))%num2).toString();
	  if (s.length < 2)
	    s = '0' + s;
	  return s;
	}
});
mooCountdown.implement(new Events, new Options);