﻿/* Brunner AG, Druck und Medien Kriens */

function randomCounter(endzahl,z) {

	z = z+1;
	anz = 10;
	max = 9999999;
	size = 7;
	num = 0;
	modendzahl = 0;	

	// Endzahl aus CMS Backend lesen
	// var cbz = document.getElementById("counterboxzahl");
	// var endzahl = cbz.getElementsByTagName("p")[0].firstChild.nodeValue;

	if(z < anz){
		endNum = '' + endzahl;
        modNum = '' + endzahl;
		num=''+Math.floor(max*Math.random());
		//
		anzNum = num.length;
		anzNumloops = Math.round(parseInt(anzNum) / 3);
       	anzNumpos = anzNum%3;
       	//
        for (var k=0; k<=anzNumloops; k++) {
			if(k == 0) {
				//substr(Anfangspositon, Anzahl Zeichen)
				modNum = num.substr(k*3,anzNumpos) + ' ';
			} else {
				aktNumpos = anzNumpos + (k-1)*3;
				modNum = modNum + num.substr(aktNumpos,3) + ' ';
			} 
		}
		// document.write(zahl);
        document.getElementById("digit").innerHTML = modNum;
		/* Cufon Fonts Definitions */
		Cufon.set('fontFamily', 'LED');
		Cufon.replace('h6');

		setTimeout('randomCounter('+endzahl+','+z+')',150);
		
	} else {
        // manueller Abstand nach drei Zahlen einbauen
        endzahlneu = '' + endzahl;
        modendzahlneu = '' + endzahl;
       	anzstellen = endzahlneu.length;
       	anzloops = Math.round(parseInt(anzstellen) / 3);
       	anzpos = anzstellen%3;
       	// fix IE Bug by negative substr
        for (var y=0; y <= anzloops; y++) {
			if(y == 0) {
				modendzahlneu = endzahlneu.substr(y*3,anzpos) + ' ';
			} else {
				aktpos = anzpos + (y-1)*3;
				modendzahlneu = modendzahlneu + endzahlneu.substr(aktpos,3) + ' ';
			} 
		}
               
        document.getElementById("digit").innerHTML = modendzahlneu;	
		/* Cufon Fonts Definitions */
		Cufon.set('fontFamily', 'LED');
		Cufon.replace('h6');    
		    	
	}
	
}
