function computeProgress(value,come) {
	var time = parseInt(new Date().getTime() / 1000);
	var res =  value*(time-come);
	// aby to bolo ako retazec, lebo inak nefunguje split
	res = "" + res;
	// niekedy to dava desatinne cislo s vela nulami a na konci jednotka, takze malicky hack
	index = res.indexOf('.');
	if (index != -1) {
		res = res.slice(0,index+3);
		// ak je len jedno desatinne miesto, pridame tam nulu
		if (res.length == index + 2)
			res = res + "0";
	}
	else {
		index = res.length;
	}
	res = res.split("");
	var formatRes = "";
	var count = 1;
	for (i=res.length-1;i>=0;i--) {
		if (index + 3 > i) {
			formatRes = res[i] + formatRes;
		}
		if (count % 3 == 0) {
			formatRes = "&nbsp;" + formatRes;
		}
		if (i < index)
			count++;
	}
	$('timerResult').innerHTML = formatRes;
}

var EnergiaTimer = Class.create({
	
	configFile: 'typo3temp/timerConfig.json',
	data: null,
	type: 0,
	cookies: null,
	come: null,
	
	initialize: function(){
		
		new Ajax.Request(this.configFile,{
			method: 'get',
			parameters: {},
			onSuccess: function(transport)
			{
				this.data = transport.responseText.evalJSON(true);
				this.cookies = new CookieJar({path: '/'});
				this.type = this.cookies.get('tt');
				//console.log(this.type);
				//console.log(this.cookies.get('tt'));
				if(this.type<1 || this.type==null) {this.type = 6;}
				//console.log(this.type);
				//console.log(this.data);
				if (this.data && this.type > 0 ) {
					$('timerDescription').innerHTML = this.data[this.type].description;
					$('timerUnit').innerHTML = this.data[this.type].unit;
					$('timerCommodity').innerHTML = this.data[this.type].commodity;
				}
				this.come = this.cookies.get('tc');
				//console.log(this.cookies.get('tc'));
				try {
					if (this.come > 0)
						new PeriodicalExecuter(computeProgress.bind(this,this.data[this.type].value,this.come), 1);
				} catch(e){
					//console.log(e);
					//alert('error period');
				}
			},
			onFailure: function() {alert("Chyba !");}
		});
	},
});
