
var Chrono = function(sec,id,format) {
	// attributs
	this.decalage = 0;
	this.sec = sec;
	this.obj = document.getElementById(id);
	this.format = format;
	this.end = new Date();
	this.end.setTime(this.end.getTime()+sec*1000);
	this.Init();
}

Chrono.prototype = {
	Init: function() {
		this.Lancer();
	},
	// lance
	Lancer: function() {
		this.Affiche();
		// il reste plus de decompte que de temps = décalage
		c = new Date();
		if(this.sec > (Math.floor(this.end.getTime()/1000)-Math.floor(c.getTime()/1000)+1)) {
			this.sec = Math.floor(this.end.getTime()/1000)-Math.floor(c.getTime()/1000)+1; // on reini
		}
		if(this.sec > 0) {
			this.sec--;
			var courant = this;
			setTimeout(function() {courant.Lancer();},1000);
		}
	},
	// affiche
	Affiche: function() {
		// recup du span
		span = this.obj;
		// pas fini
		if(this.sec > 0) {
			// calculs
			minu = Math.floor(this.sec/60);
			sec = this.sec - minu*60;
			heur = Math.floor(minu/60);
			minu = minu - heur*60;
			jour = Math.floor(heur/24);
			heur = heur - jour*24;
			// creation de l'affichage
			chr = '';
			// si des jours
			if(jour > 0) { chr = chr+Chrono.Digit(jour)+this.format[3]; }
			// si des heures
			if(heur > 0) { chr = chr+Chrono.Digit(heur)+this.format[2]; }
			// si des minutes
			if(minu > 0) { chr = chr+Chrono.Digit(minu)+this.format[1]; }
			// si des secondes
			if(sec > 0) { chr = chr+Chrono.Digit(sec)+this.format[0]; }
			// on affiche
			span.innerHTML = chr;
		}
		else {
			span.innerHTML = this.format[4];
			var courant = this;
			setTimeout(function() {courant.Actualise();},10000);
            
		}
	},
    // actualise
	Actualise: function() {
        window.history.go(0); 
    }
}

Chrono.Digit = function(nb) {
	if(nb <= 9 && nb >= 0) {
		return '0'+nb;
	}
	else {
		return nb;
	}
}

