var format_num = function(d) { d = parseInt(d); return (d<10) ? '0'+d : d; };
var secondesToTime = function(secs) { var hours = Math.floor(secs / 3600), minutes = Math.floor((secs - hours*3600)/ 60), seconds = Math.ceil(secs - hours*3600 - minutes*60); return {"h": hours, "m": minutes, "s": seconds}; };

if (!Date.now) {  
	Date.now = function now() {  
		return +new Date();  
	};  
}  

jQuery(function($){
	var $now = new Date();
	var $time_end; 
	regress = function() {
		if ($time_end==null || ($time_end.getTime() - Date.now() < 0)) {
			var $now = new Date();
			$time_end = new Date($now.getFullYear(), $now.getMonth(), $now.getDate(), $now.getHours(), $now.getMinutes()+30, $now.getSeconds());
		}
		var interval = Math.floor(($time_end.getTime() - Date.now())/1000);
		secs = secondesToTime(interval);
		jQuery('.timer').html(format_num(secs.h)+':'+format_num(secs.m)+':'+format_num(secs.s));
		setTimeout(regress, 1E3);
	};
	regress();
});
