// CURRENT EVENTS
	
jQuery(document).ready(
	function() {
		vartti_currentevent.init();
	}
);
	
var vartti_currentevent = {
	_counters: {},
	_frequency: 1, // in seconds: 60 for minute, 1 for second
	
	systemtime: null,
	isRunning: false,
	
	init: function() {
		if(!this.isRunning) {
			this.initCounterLoop();	
			this.isRunning = true;
		}
	},
	
	Counter: function(articleid, articletime) {
		this.articletime = articletime;
		this.articleid = articleid;
		this.domobj = jQuery('#'+articleid+'_counter');		
		this.counter = (this.articletime-vartti_currentevent.systemtime);		

		this.getRemaining = function() {
			var ms = parseFloat(this.counter)/1000;

			var days = Math.floor(ms / 86400); 
			var hours = Math.floor((ms - (days * 86400 ))/3600)
			var minutes = Math.floor((ms - (days * 86400 ) - (hours *3600 ))/60)
			var secs = Math.floor((ms - (days * 86400 ) - (hours *3600 ) - (minutes*60)))
			
			var str;
			
			if(days>0) {
				str = days + " pv " + hours + " h " + minutes + " min " + secs + " s"
			}
			else if(hours>0) {
				str = hours + " h " + minutes + " min " + secs + " s"
			}
			else if(minutes>0) {
				str = minutes + " min " + secs + " s"
			}
			else {
				str = secs + " s"
			}
			
			return str;
		}
		
		this.advance = function() {
			this.counter = this.counter-(vartti_currentevent._frequency*1000);
		}
		
		this.render = function() {
			if(this.counter>0) {
				this.domobj.html(this.getRemaining());
			}
			else {
				jQuery(this.articleid).remove();	
			}
		}
	},
	
	newCounter: function(articleid, systemtime, articletime) {
		if(!vartti_currentevent.systemtime) {
			vartti_currentevent.systemtime = systemtime;
		}
		
		var counter = new this.Counter(articleid, articletime);
		this._counters[articleid] = counter;
		return this._counters[articleid];
	},
	
	getCounter: function(articleid) {
		return this._counters[articleid];
	},
	
	counterLoop: function() {
		for(cnt in this._counters) {
			var counter = this.getCounter(cnt);
			counter.advance();
			counter.render();
		}

		window.setTimeout("vartti_currentevent.counterLoop()", vartti_currentevent._frequency*1000);
	},
	
	initCounterLoop: function() {
		this.counterLoop();
	}
}