/*
 * CountDown.js
 * $Author: Samuel Sirois (samuel@sirois.info)$
 * $Date: 06 avril, 2005$
 *
 * This script permits to display a countdown 'till a specified date on 
 * a webpage.
 * Use example : setTimeout(this.selfRef()+".méthode()",1000);
 *
 * Depends : SelfReferencedObjects.js
 */
 
 /*
  * Default constructor
  */
 	function CountDown() {}
	
/*
 * Constructor with initialization of the final date
 */
 	function CountDown(dteDate) {
		this.setDate(dteDate);
	}

/**************/
/* Constantes */
/**************/

/* Representation of the different time units in milliseconds */
	CountDown.prototype.ONE_DAY = 1000 * 60 * 60 * 24;
	CountDown.prototype.ONE_HOUR = 1000 * 60 * 60;
	CountDown.prototype.ONE_MINUTE = 1000 * 60;
	CountDown.prototype.ONE_SECOND = 1000;
	CountDown.prototype.ONE_MILLISECOND = 1;
	
/* Suffix of the values */
	CountDown.prototype.DAY_SUFFIX = 'jour |';
	CountDown.prototype.DAYS_SUFFIX = 'jours |';
	CountDown.prototype.HOUR_SUFFIX = 'h |';
	CountDown.prototype.HOURS_SUFFIX = 'h |';
	CountDown.prototype.MINUTE_SUFFIX = 'min ';
	CountDown.prototype.MINUTES_SUFFIX = 'min ';
	CountDown.prototype.SECOND_SUFFIX = 'sec ';
	CountDown.prototype.SECONDS_SUFFIX = 'secs ';
	CountDown.prototype.MILLISECOND_SUFFIX = 'msec ';
	CountDown.prototype.MILLISECONDS_SUFFIX = 'msecs ';
	
/*************/
/* Variables */
/*************/
	CountDown.prototype.dteEvent; //The date from whitch we must calc the countdown
	
/************/	
/* Methods */
/************/

/*
 * This method sets the date of the event
 */
 	CountDown.prototype.setDate = function(dteDate) {
		this.dteEvent = dteDate;
	}
 
/*
 * This method starts the countdown
 */
	CountDown.prototype.start = function() {
	//Calls the method refresh every seconds (need SelfReferencedObjects.js for the selfRef() method)
		window.setInterval(this.selfRef() + ".refresh()", 1000);
	//Calls the method refreshMilliseconds every milliseconds (needed so that the entire countdown doesn't lack because of the refresh rate)
		window.setInterval(this.selfRef() + ".refreshMilliseconds()", 1);
	}
	
/*
 * This method refresh the value of the countdown
 */
 	CountDown.prototype.refresh = function() {
		var dteNow = new Date();
		var dteDiff = this.dteEvent.getTime() - dteNow.getTime();

	//Calc the new countdown values
		var iDays = (dteDiff - (dteDiff % this.ONE_DAY)) / this.ONE_DAY;
		dteDiff = dteDiff - (iDays * this.ONE_DAY); //Eliminates what's already been calculated
		var iHours = (dteDiff - (dteDiff % this.ONE_HOUR)) / this.ONE_HOUR;
		dteDiff = dteDiff - (iHours * this.ONE_HOUR); //Eliminates what's already been calculated
		var iMinutes = (dteDiff - (dteDiff % this.ONE_MINUTE)) / this.ONE_MINUTE;
		dteDiff = dteDiff - (iMinutes * this.ONE_MINUTE); //Eliminates what's already been calculated
		var iSeconds = (dteDiff - (dteDiff % this.ONE_SECOND)) / this.ONE_SECOND;
		dteDiff = dteDiff - (iSeconds * this.ONE_SECOND); //Eliminates what's already been calculated.
		var iMilliseconds = (dteDiff - (dteDiff % this.ONE_MILLISECOND)) / this.ONE_MILLISECOND;
		
	//Format the values
		var innerHTML = '<span id="cdDays" class="cdDays">' + iDays + '</span>';
		innerHTML += '<span id="cdDaysSuffix" class="cdDaysSuffix">';
		innerHTML += iDays < 1 ? this.DAY_SUFFIX : this.DAYS_SUFFIX;
		innerHTML += '</span>';
		innerHTML += '<span id="cdHours" class="cdHours">';
		innerHTML += iHours < 10 ? '0' + iHours : iHours;
		innerHTML += '</span>';
		innerHTML += '<span id="cdHoursSuffix" class="cdHoursSuffix">';
		innerHTML += iHours < 1 ? this.HOUR_SUFFIX : this.HOURS_SUFFIX;
		innerHTML += '</span>';
		innerHTML += '<span id="cdMinutes" class="cdMinutes">';
		innerHTML += iMinutes < 10 ? '0' + iMinutes : iMinutes;
		innerHTML += '</span>';
		innerHTML += '<span id="cdMinutesSuffix" class="cdMinutesSuffix">';
		innerHTML += iMinutes < 1 ? this.MINUTE_SUFFIX : this.MINUTES_SUFFIX;
		innerHTML += '</span>';
		/*innerHTML += '<span id="cdSeconds" class="cdSeconds">';
		innerHTML += iSeconds < 10 ? '0' + iSeconds : iSeconds;
		innerHTML += '</span>';
		innerHTML += '<span id="cdSecondsSuffix" class="cdSecondsSuffix">';
		innerHTML += iSeconds < 1 ? this.SECOND_SUFFIX : this.SECONDS_SUFFIX;
		innerHTML += '</span>';
		//Just put the frame of the milliseconds, will be refreshed by refreshMilliseconds
		innerHTML += '<span id="cdMilliseconds" class="cdMilliseconds">';
		innerHTML += '</span>';
		innerHTML += '<span id="cdMillisecondsSuffix" class="cdMillisecondsSuffix">';
		innerHTML += '</span>';*/
		
	//Display the values
		document.getElementById("CountDown").innerHTML = innerHTML;
	}
	
/*
 * This method refresh the value of the milliseconds
 */
	CountDown.prototype.refreshMilliseconds = function() {
		//Must look if element has been created before happening something to it
		if (document.getElementById("cdMilliseconds")) {
			var dteNow = new Date();
			var dteDiff = this.dteEvent.getTime() - dteNow.getTime();
	
		//Calc the new countdown values
			var iDays = (dteDiff - (dteDiff % this.ONE_DAY)) / this.ONE_DAY;
			dteDiff = dteDiff - (iDays * this.ONE_DAY); //Eliminates what's already been calculated
			var iHours = (dteDiff - (dteDiff % this.ONE_HOUR)) / this.ONE_HOUR;
			dteDiff = dteDiff - (iHours * this.ONE_HOUR); //Eliminates what's already been calculated
			var iMinutes = (dteDiff - (dteDiff % this.ONE_MINUTE)) / this.ONE_MINUTE;
			dteDiff = dteDiff - (iMinutes * this.ONE_MINUTE); //Eliminates what's already been calculated
			var iSeconds = (dteDiff - (dteDiff % this.ONE_SECOND)) / this.ONE_SECOND;
			dteDiff = dteDiff - (iSeconds * this.ONE_SECOND); //Eliminates what's already been calculated.
			var iMilliseconds = (dteDiff - (dteDiff % this.ONE_MILLISECOND)) / this.ONE_MILLISECOND;
			
		//Display the milliseconds
			document.getElementById("cdMilliseconds").innerHTML = iMilliseconds < 10 ? '00' + iMilliseconds : iMilliseconds < 100 ? '0' + iMilliseconds : iMilliseconds;
			document.getElementById("cdMillisecondsSuffix").innerHTML = iMilliseconds < 1 ? this.MILLISECOND_SUFFIX : this.MILLISECONDS_SUFFIX;
		}
	}
	
/*
 * This method inserts the countdown into a webpage
 */
 	CountDown.prototype.toString = function() {
		var str = '<div id="CountDown" class="CountDown"></div>';
			
		return str;
 	}
