/**
 * CountDown Class
 *
 * @author        Modificato con alessandro
 *
 * @param    dd   (string) 'mm/dd/aaaa 02:30 PM'
 *
 */
function countDown( conf ) {
    // init target time
    var target            = new Date( conf.targetDate );
    this.targetTime       = target.getTime();
	this.countName        = conf.countName;
	this.finishMsg        = conf.finishMessage;
   
    /**
     * refresh countdown
     */
	
	// funzione per aggiungere uno zero nel caso la data sia minore di due cifre (3 cifre per millisecondi)
	this.format = function(d, len) {
		d = d.toString();
		
		if (d.length < len)
			for(var i=0;i<len-d.length;i++)
				d = '0' + d;
		return d;
		
	}
    this.refresh = function() {
        var today                 = new Date();
        var currentTime           = today.getTime();
        // time left
        this._leftMilliseconds    = (this.targetTime - currentTime);
        this._leftSeconds         = Math.floor( this._leftMilliseconds / 1000 );
        this._leftMinutes         = Math.floor( this._leftSeconds / 60 );
        this._leftHours           = Math.floor( this._leftMinutes / 60 );
        // no module
        this.leftDays             = Math.floor( this._leftHours / 24 );
        // for print
        this.leftMilliseconds     = this._leftMilliseconds % 1000;
        this.leftSeconds          = this._leftSeconds % 60;
        this.leftMinutes          = this._leftMinutes % 60;
        this.leftHours            = this._leftHours % 24;
		
		if (this.leftDays <= 0 && this.leftHours <= 0 && this.leftMinutes <= 0 && this.leftSeconds <= 0 && this.leftMilliseconds <= 0) {
			document.getElementById('days-' + this.countName).innerHTML = this.finishMsg;
			document.getElementById('left-' + this.countName).innerHTML = "siamo in viaggio..";
			clearInterval(eval("intervallo" + conf.countName));
		}
		else {
			document.getElementById('days-' + this.countName).innerHTML = "giorni: " + this.format(this.leftDays,2);
			document.getElementById('count-' + this.countName).innerHTML = "ore: " + this.format(this.leftHours,2) + ":" + this.format(this.leftMinutes,2) + ":" + this.format(this.leftSeconds,2) + ":" + this.format(this.leftMilliseconds,3);
		}
	}

    //this.refresh();
}
