// Countdown
maxDigits = (vDayLen==3)?9:8;

AnzS=''; for(i=0; i<maxDigits; i++) AnzS +=' ';
Leer=''; for(i=0; i<maxDigits; i++) Leer +='0';

n = new Array(10);
for(i=0; i<10; i++) {
	n[i] = new Image(vWidth,vHeight);
	n[i].src = imgPath+'c'+i+'.png';
}


function addZero(n) {
	str = '';
	if (n<10) str += '0';
	return str+n.toString();
}


function showNums(w) {
	var AnzW=Math.floor(Math.abs(w));
	var we=''+Leer+AnzW;
	we = we.substring(we.length-maxDigits,we.length);
	for(var i=maxDigits-1; i>=0; i-=1) {
		if(AnzS.charAt(i)!= we.charAt(i)) document.images['b'+i].src = imgPath+'c'+we.charAt(i)+'.png';
	}
	AnzS = we;
}


function afficheLien(idPage) {
	jQuery(document).ready(function() { document.getElementById('compteur').innerHTML = '<p class="lienLIVE fond02"><a href="'+idPage+'" target="_blank">Regardez le culte en direct</a></p>'; });
}


function setServerDate(value) {
	if(isNaN(value)){
		string = String(value);
		newNumb = string.split(",");
		newDate   = new Date(newNumb[0],newNumb[1],newNumb[2],newNumb[3],newNumb[4],newNumb[5]);
		return newDate;
	}else{
		newDate   = new Date(value);
		return newDate;
	}
}


function buildCounter() {
	// we calculate the difference of Unix time between the event and the current date, and set the interval to 1 second
	vDiff     = Math.floor((evtDate.getTime()-newDate.getTime())/1000);
	vInterval = 1001-(newDate.getTime()%1000);

	// (3) if the difference comes down to 0 we set all number values to 0
	if (vDiff<0) {
		vDays  = (vDayLen==3)?000:00;
		vHours = 00;
		vMins  = 00;
		vSecs  = 00;
		afficheLien(idLien);
		return;
	}
	
	// we put all the digits values into an array
	arrDig = new Array();

	vSecs  = addZero(vDiff%60);
	arrDig['s1'] = vSecs.substr(1,1);
	arrDig['s0'] = vSecs.substr(0,1);

	vDiff  = Math.floor(vDiff/60);
	vMins  = addZero(vDiff%60);
	arrDig['m1'] = vMins.substr(1,1);
	arrDig['m0'] = vMins.substr(0,1);

	vDiff  = Math.floor(vDiff/60);
	vHours = addZero(vDiff%24);
	arrDig['h1'] = vHours.substr(1,1);
	arrDig['h0'] = vHours.substr(0,1);

	vDiff  = Math.floor(vDiff/24);
	vDays  = vDiff.toString();

	if (vDayLen==3) {
		arrDig['d2'] = vDays.substr(2,1);
		arrDig['d1'] = vDays.substr(1,1);
		arrDig['d0'] = vDays.substr(0,1);
	} else {
		arrDig['d1'] = vDays.substr(1,1);
		arrDig['d0'] = vDays.substr(0,1);
	}
	
	// we build the digits from the array
	newNum = (vDayLen==3)?''+arrDig['d0']+arrDig['d1']+arrDig['d2']+arrDig['h0']+arrDig['h1']+arrDig['m0']+arrDig['m1']+arrDig['s0']+arrDig['s1']:''+arrDig['d0']+arrDig['d1']+arrDig['h0']+arrDig['h1']+arrDig['m0']+arrDig['m1']+arrDig['s0']+arrDig['s1'];
	showNums(newNum);
	
	// we loop the function every second and add one second to the current time
	serverDate = newDate.setSeconds(secs+1);
	setTimeout('countDown()',vInterval);
}


function countDown(turn) {
	
	// we take the current date and time of the server
	// setServerDate(serverDate);
	if(isNaN(serverDate)){
		string = String(serverDate);
		newNumb = string.split(",");
		newDate   = new Date(newNumb[0],newNumb[1],newNumb[2],newNumb[3],newNumb[4],newNumb[5]);
	}else{
		newDate   = new Date(serverDate);
	}
	
	var secs = newDate.getSeconds(newDate);
	if(secs == 0 || turn) {
		jQuery.get("refreshtime.php",function(data) {
			// (1 bis) after one minuteor on the first run we refresh the whole to be accurate with the current date and time of the server
			// setServerDate(data);
			if(isNaN(data)){
				string = String(data);
				newNumb = string.split(",");
				newDate   = new Date(newNumb[0],newNumb[1],newNumb[2],newNumb[3],newNumb[4],0);
			}else{
				newDate   = new Date(data);
			}
			
			// buildCounter()
			// (2) we calculate the difference of Unix time between the event and the current date, and set the interval to 1 second
			vDiff     = Math.floor((evtDate.getTime()-newDate.getTime())/1000);
			vInterval = 1001-(newDate.getTime()%1000);
		
			// (3) if the difference comes down to 0 we set all number values to 0
			if (vDiff<0) {
				vDays  = (vDayLen==3)?000:00;
				vHours = 00;
				vMins  = 00;
				vSecs  = 00;
				afficheLien(idLien);
				return;
			}
			
			// (4) we put all the digits values into an array
			arrDig = new Array();
		
			vSecs  = addZero(vDiff%60);
			arrDig['s1'] = vSecs.substr(1,1);
			arrDig['s0'] = vSecs.substr(0,1);
		
			vDiff  = Math.floor(vDiff/60);
			vMins  = addZero(vDiff%60);
			arrDig['m1'] = vMins.substr(1,1);
			arrDig['m0'] = vMins.substr(0,1);
		
			vDiff  = Math.floor(vDiff/60);
			vHours = addZero(vDiff%24);
			arrDig['h1'] = vHours.substr(1,1);
			arrDig['h0'] = vHours.substr(0,1);
		
			vDiff  = Math.floor(vDiff/24);
			vDays  = vDiff.toString();
		
			if (vDayLen==3) {
				arrDig['d2'] = vDays.substr(2,1);
				arrDig['d1'] = vDays.substr(1,1);
				arrDig['d0'] = vDays.substr(0,1);
			} else {
				arrDig['d1'] = vDays.substr(1,1);
				arrDig['d0'] = vDays.substr(0,1);
			}
			
			// (5) we build the digits from the array
			newNum = (vDayLen==3)?''+arrDig['d0']+arrDig['d1']+arrDig['d2']+arrDig['h0']+arrDig['h1']+arrDig['m0']+arrDig['m1']+arrDig['s0']+arrDig['s1']:''+arrDig['d0']+arrDig['d1']+arrDig['h0']+arrDig['h1']+arrDig['m0']+arrDig['m1']+arrDig['s0']+arrDig['s1'];
			showNums(newNum);
			
			// (6) we loop the function every second and add one second to the current time
			serverDate = newDate.setSeconds(secs+1);
			setTimeout('countDown()',vInterval);
			
		});
		
	}else{
		
		// buildCounter();
		// (2) we calculate the difference of Unix time between the event and the current date, and set the interval to 1 second
		vDiff     = Math.floor((evtDate.getTime()-newDate.getTime())/1000);
		vInterval = 1001-(newDate.getTime()%1000);
	
		// (3) if the difference comes down to 0 we set all number values to 0
		if (vDiff<0) {
			vDays  = (vDayLen==3)?000:00;
			vHours = 00;
			vMins  = 00;
			vSecs  = 00;
			afficheLien(idLien);
			return;
		}
		
		// (4) we put all the digits values into an array
		arrDig = new Array();
	
		vSecs  = addZero(vDiff%60);
		arrDig['s1'] = vSecs.substr(1,1);
		arrDig['s0'] = vSecs.substr(0,1);
	
		vDiff  = Math.floor(vDiff/60);
		vMins  = addZero(vDiff%60);
		arrDig['m1'] = vMins.substr(1,1);
		arrDig['m0'] = vMins.substr(0,1);
	
		vDiff  = Math.floor(vDiff/60);
		vHours = addZero(vDiff%24);
		arrDig['h1'] = vHours.substr(1,1);
		arrDig['h0'] = vHours.substr(0,1);
	
		vDiff  = Math.floor(vDiff/24);
		vDays  = vDiff.toString();
	
		if (vDayLen==3) {
			arrDig['d2'] = vDays.substr(2,1);
			arrDig['d1'] = vDays.substr(1,1);
			arrDig['d0'] = vDays.substr(0,1);
		} else {
			arrDig['d1'] = vDays.substr(1,1);
			arrDig['d0'] = vDays.substr(0,1);
		}
		
		// (5) we build the digits from the array
		newNum = (vDayLen==3)?''+arrDig['d0']+arrDig['d1']+arrDig['d2']+arrDig['h0']+arrDig['h1']+arrDig['m0']+arrDig['m1']+arrDig['s0']+arrDig['s1']:''+arrDig['d0']+arrDig['d1']+arrDig['h0']+arrDig['h1']+arrDig['m0']+arrDig['m1']+arrDig['s0']+arrDig['s1'];
		showNums(newNum);
		
		// (6) we loop the function every second and add one second to the current time
		serverDate = newDate.setSeconds(secs+1);
		setTimeout('countDown()',vInterval);
	
	}
	
}


