function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


function javascript_countdown(){
	var time_left = 10; //number of seconds for countdown
	var output_element_id = 'javascript_countdown_time';
	var keep_counting = 1;
	var no_time_left_message = '<span>Getting new data <img src="wp/wp-content/themes/omega/images/loading.gif" class="loading"></span>';
	var callback=null;
  this.timeoutID=null;
  this.callbackID=null;
	var countdown=function() {
		if(time_left < 2) {
			keep_counting = 0;
		}
 
		time_left = time_left - 1;
	}
 
	var add_leading_zero=function(n) {
		if(n.toString().length < 2) {
			return '0' + n;
		} else {
			return n;
		}
	}
 
	var format_output=function() {
		var hours, minutes, seconds;
		seconds = time_left % 60;
		minutes = Math.floor(time_left / 60) % 60;
 
		seconds = add_leading_zero( seconds );
		minutes = add_leading_zero( minutes );
    
		return minutes + ':' + seconds;
	}
 
	var show_time_left=function() {
		document.getElementById(output_element_id).innerHTML = "Reloading in "+ format_output();//time_left;
	}
 
	var no_time_left=function() {
		document.getElementById(output_element_id).innerHTML = no_time_left_message;
		//callback();
	}
  
	
		var count=function () {
			countdown();
			show_time_left();
		};
		var timer=function () {
			count();
 
			if(keep_counting) {
				timeoutID=setTimeout(timer, 1000);
			
				
			} else {
				no_time_left();
				callbackID=setTimeout(callback, 1000);
			}
		};
		var setTimeLeft=function (t) {
			time_left = t;
			if(keep_counting == 0) {
				this.timer();
			}
		};
		this.init=function (t, element_id,c) {
		  timeoutID=null
		  callbackID=null;
		  //console.log("");
		  //console.log("init");
		  //console.log(timeoutID);
		  callback=c;
			time_left = t;
			output_element_id = element_id;
			timer();
		};

};
