/**
 * @author Eric Mantoani <eu@eric.com.br>
 * 
 */

 
Pedagiometro = function(){
	
	// init vars, default = 0;
	var startValue = 0;
	var valuePerSecond = 0;
	var currentValue = 0;
	
	// config vars
	var updatingTime = 100; // tempo de atualização em ms
	var updatingTimeFraction = 1000/updatingTime; // fração
	
	
	var _initHandler = function(){
		
		_setStartValue();
		_setValuePerSecond();
		
		var iterationFirst = true;
		
		setInterval(function(){
			if(_getStartValue() > 0 && _getValuePerSecond() > 0){
				if(iterationFirst){
					currentValue = _getStartValue();
					addingValue = _getValuePerSecond() / updatingTimeFraction;
					iterationFirst = false;
				}
				currentValue = currentValue + addingValue;
				//$('#pedagiometro').html(Math.round(currentValue*100)/100);
				$('#pedagiometro').html(numberFormat(currentValue, 2, ',', '.', 'R$'));
			}
		}
		,updatingTime);
		
	}
	this.initHandler = _initHandler;
	
	
	// dynamic sets
	var _setValuePerSecond = function(){
		$.ajax({
			url: 'pedagiometro.php?result=valuePerSecond',
			data: '',
			success: function(response){
				valuePerSecond = new Number(response);
				return true;
			}
		});
	}
	this.setValuePerSecond = _setValuePerSecond;
	
	var _setStartValue = function(){
		$.ajax({
			url: 'pedagiometro.php',
			data: '',
			success: function(response){
				startValue = new Number(response);
				return true;
			}
		});
	}
	this.setStartValue = _setStartValue;
	
	
	// gets
	var _getStartValue = function(){
		return startValue;
	}
	this.getStartValue = _getStartValue;
	
	var _getValuePerSecond = function(){
		return valuePerSecond;
	}
	this.getValuePerSecond = _getValuePerSecond;
	
	function numberFormat(number, decimals, dec_point, thousands_sep, prepend) {
		// Formats a number with grouped thousands
		// discuss at: http://phpjs.org/functions/number_format
		
		// Eric Mantoani: com relação ao original, incluí "prepend"
		
		var n = number, prec = decimals;
		
		var toFixedFix = function (n,prec) {
		    var k = Math.pow(10,prec);
		    return (Math.round(n*k)/k).toString();
		};
		
		n = !isFinite(+n) ? 0 : +n;
		prec = !isFinite(+prec) ? 0 : Math.abs(prec);
		var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
		var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;
		
		var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;
		
		var abs = toFixedFix(Math.abs(n), prec);
		var _, i;
		
		if (abs >= 1000) {
		    _ = abs.split(/\D/);
		    i = _[0].length % 3 || 3;
		
		    _[0] = s.slice(0,i + (n < 0)) +
		          _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
		    s = _.join(dec);
		} else {
		    s = s.replace('.', dec);
		}
		
		var decPos = s.indexOf(dec);
		if (prec >= 1 && decPos !== -1 && (s.length-decPos-1) < prec) {
		    s += new Array(prec-(s.length-decPos-1)).join(0)+'0';
		}
		else if (prec >= 1 && decPos === -1) {
		    s += dec+new Array(prec).join(0)+'0';
		}
		return prepend+' '+s; 
	}



}

