
/***********************************************
* Dynamic Countdown script- � Dynamic Drive (http://www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
***********************************************/

var CompteARebours = new Class({

	options : {
	langue : null,
	container : null,
	currentTime : null,
	targetdate : null,
	timesup : null,
	baseunit : null,
        periodicalShow : null,
        periodicalUpdate : null

        },
	
	
	initialize : function (opts){
            
            this.setOptions(opts);
            this.options.currentTime = new Date();
            this.options.timesup = false;
            this.updateTime();
	},

	updateTime : function(){
		this.options.currentTime.setSeconds(this.options.currentTime.getSeconds()+1);
                if(this.options.periodicalUpdate == null)
                    this.options.periodicalUpdate = this.updateTime.periodical(1000, this) ;//update time every second
	},

	displaycountdown : function(baseunit){
		this.options.baseunit = baseunit;
		this.showresults();
	},

	showresults : function(){

                var timediff = (this.options.targetdate - this.options.currentTime) / 1000; //difference btw target date and current date, in seconds
		if (timediff < 0){ //if time is up
			this.options.timesup = true;
			this.options.container.innerHTML = this.displayCountDown([]);
			return;
		}
		var oneMinute = 60; //minute unit in seconds
		var oneHour = 60 * 60; //hour unit in seconds
		var oneDay = 60 * 60 * 24; //day unit in seconds
		var dayfield = Math.floor( timediff/oneDay );
		var hourfield = Math.floor( (timediff - dayfield * oneDay) / oneHour);
		var minutefield = Math.floor( (timediff - dayfield * oneDay -hourfield * oneHour) / oneMinute);
		var secondfield = Math.floor( (timediff - dayfield * oneDay - hourfield * oneHour - minutefield * oneMinute) );
		if (this.options.baseunit == "hours"){ //if base unit is hours, set "hourfield" to be topmost level
			hourfield = dayfield * 24 + hourfield;
			dayfield = "n/a";
		}
		else 
			if (this.options.baseunit == "minutes"){ //if base unit is minutes, set "minutefield" to be topmost level
				minutefield = dayfield * 24 * 60 + hourfield * 60 + minutefield;
				dayfield=hourfield = "n/a";
			}
			else 
				if (this.options.baseunit == "seconds"){ //if base unit is seconds, set "secondfield" to be topmost level
					secondfield = timediff;
					dayfield = hourfield = minutefield="n/a";
				}
		this.options.container.innerHTML = this.displayCountDown([dayfield, hourfield, minutefield, secondfield]);
                if(this.options.periodicalShow == null)
                    this.options.periodicalShow = this.showresults.periodical(1000, this); //update results every second
	},

/////CUSTOM FORMAT OUTPUT FUNCTIONS BELOW//////////////////////////////

//Create your own custom format function to pass into cdtime.displaycountdown()
//Use arguments[0] to access "Days" left
//Use arguments[1] to access "Hours" left
//Use arguments[2] to access "Minutes" left
//Use arguments[3] to access "Seconds" left

//The values of these arguments may change depending on the "baseunit" parameter of cdtime.displaycountdown()
//For example, if "baseunit" is set to "hours", arguments[0] becomes meaningless and contains "n/a"
//For example, if "baseunit" is set to "minutes", arguments[0] and arguments[1] become meaningless etc


	displayCountDown : function(arguments){
            var displaystring = '';
		if (this.options.timesup == false){ //if target date/time not yet met
			if(this.options.langue=='FR')displaystring="<span class='count_down'>" + arguments[0] + " <sup>jours</sup> " + arguments[1] + " <sup>heures</sup> " + arguments[2] + " <sup>minutes</sup> " + arguments[3] + " <sup>secondes</sup></span>";
			if(this.options.langue=='ES')displaystring="<span class='count_down'>" + arguments[0] + " <sup>d&iacute;a</sup> " + arguments[1] + " <sup>hora</sup> " + arguments[2] + " <sup>minuto</sup> " + arguments[3] + " <sup>segundo</sup></span>";
			else displaystring="<span class='count_down'>" + arguments[0] + " <sup>days</sup> " + arguments[1] + " <sup>hours</sup> " + arguments[2] + " <sup>minutes</sup> " + arguments[3] + " <sup>seconds</sup></span>";
		}
		return displaystring;
	}
});


CompteARebours.implement(new Options);