/**
 * @author Andrzej Soroczyński
 */
var timer = {
	count: 0,
	myInterval: 0,
	first: true,
	startTimeMillis: 0,
	parentObj: null,
	refresh: function() {
			if(timer.first) {
				timer.first = false;
				timer.startTimeMillis = (new Date()).getTime();
			}
			timer.count = (new Date()).getTime() - timer.startTimeMillis;
			timer.parentObj.empty().append(timer.getReadable());	
		},
	start: function(id, timeout, clazz) {
			timer.parentObj = jQuery("#"+id).attr("class", clazz);
			timer.myInterval = setInterval(timer.refresh, timeout);
		},
	stop: function() {
			clearInterval(timer.myInterval);
		},
	reset: function() {
			timer.count = 0;
			timer.myInterval = 0;
			timer.first = true;
			timer.startTimeMillis = 0;
			timer.parentObj = null;	
		},
	getReadable: function() {
		var minutes = Math.floor(timer.count / (1000 * 60));
		var seconds = Math.floor(timer.count / 1000) % 60;
		var seconds100 = Math.floor(timer.count / 10) % 100;
		if(minutes < 10 && minutes > 0) {
			minutes = "0" + minutes;
		}
		if(seconds < 10) {
			seconds = "0" + seconds;
		}
		if(seconds100 < 10) {
			seconds100 = "0" + seconds100;
		}
		if(minutes > 0) {
			return minutes + ":" + seconds + ":" + seconds100;
		} else {
			return seconds + ":" + seconds100;
		}
	} 
}