Countdown = new Class({
	Implements : Options,
	options : {
		days : '%days% days ',
		hours : '%hours%:',
		min : '%min%:',
		sec : '%sec%',
		over : '--',
		startDate : null,
		endDate : null,
		doubleDigits : true
	},
	initialize : function(id, options) {
		this.id = id;
		this.setOptions(options);
		if (!this.options.endDate) {
			return;
		}
		var now = ($pick(this.options.startDate, new Date())).getTime();
		this.difference = Math.round((this.options.endDate.getTime() - now)/1000);
		//$log('difference = '+this.difference);
		this.time = {};
		if (this.difference <=0) this.time == null;
		window.addEvent('domready', this._tick.bind(this));
	},
	_tick : function() {
		if (!this.element) this.element = $(this.id);
		this.difference --;
		if(this.difference <=0) {
			this.timer = null;
		}
		this.display();
		if (this.difference > 0) this._tick.delay(1000, this);
	},
	write : function() {
		document.write(this.draw());
	},
	draw : function() {
		if (this.time == null) return this.options.over;
		else {
			var s = '';
			this.setTime();
			if (this.options.doubleDigits) ['days','hours','min','sec'].each(function(item) {
				var val = this.time[item];
				if (val < 10) this.time[item] = '0'+val;
			}, this);
			return this.options.days.replace('%days%', 
			this.time.days)+this.options.hours.replace('%hours%', 
			this.time.hours)+this.options.min.replace('%min%', 
			this.time.min)+this.options.sec.replace('%sec%', 
			this.time.sec);
		}
	},
	setTime : function() {

		var day = 86400;
		var hour = 3600;
		var minute = 60;

		//this.remaining =  this.target - Math.round($time() / 1000);

		var days = Math.floor(this.difference/day);
		var hours = Math.floor((this.difference - (days*day))/hour);
		var min = Math.floor((this.difference - (days*day) - (hours*hour))/minute);
		var sec = this.difference - (days*day) - (hours*hour) - (min*minute);

		this.time = {
			days: days,
			hours: hours,
			min: min,
			sec: sec
		};
	},
	display: function() {
		this.element.set('html', this.draw());
	}
});