var CountDown = new Class({

	Implements: [Options,Events],
	options: {
		element: 'countdown',
		endDate: new Date(new Date().getTime() + (30 * 1000)),
		onComplete: function(){},
		frequency: 1000
	},
	initialize: function(options) {

		
		this.setOptions(options);
		while (this.options.endDate.getTime() <= new Date().getTime()) {
			this.options.endDate.setTime(this.options.endDate.getTime() + (7 * 24 * 60 * 60 * 1000));
		}

	},
	start: function() {
		this.anim();
	},
	anim: function() {

		var milisegundos = Math.max(0, this.options.endDate.getTime() - new Date().getTime());
		var segundos = Math.floor(milisegundos / 1000);
		segundosTotales = segundos;

		var dias = Math.floor(segundos / (60 * 60 * 24));
		segundos %= (60 * 60 * 24);

		var horas = Math.floor(segundos / (60 * 60));
		segundos %= (60 * 60);

		var minutos = Math.floor(segundos / 60);

		segundos = segundos % 60;


		this.options.element.set('text', "Sólo quedan " + dias + " días, " + horas + " h, " + minutos + " min, " + segundos + " seg");
		var fx = new Fx.Tween(this.options.element, {
			duration: this.options.frequency,
			link: 'ignore',
			onComplete: function() {
				if (segundosTotales > 0) {
					this.anim();
				} else {
					this.fireEvent('complete');
				}
			}.bind(this)
		}).start('font-size', this.options.startFont, this.options.endFont);
	}
});
