var Countdown = Class.create();
Countdown.prototype = 
{
	units:
	{
		second: 'sekunda',
		seconds: 'sekúnd',
		seconds2: 'sekundy',
		minute: 'minúta',
		minutes: 'minút',
		minutes2: 'minúty',
		hour: 'hodina',
		hours: 'hodín',
		hours2: 'hodiny',
		day: 'deň',
		days: 'dní',
		days2: 'dni'
	},
	initialize: function(milliseconds)
	{
		this.milliseconds = milliseconds;
	},
	toString: function() {
		var days = Math.floor(this.milliseconds / 86400000);
		var remainder = this.milliseconds - days * 86400000;
		var hours = Math.floor(remainder / 3600000);
		remainder -= hours * 3600000;
		var minutes = Math.floor(remainder / 60000);
		remainder -= minutes * 60000;
		var seconds = Math.floor(remainder / 1000);
		remainder -= seconds * 1000;
		var buffer = [];
		var unit = '';
		if (days >= 0) {
			if (days == 1) {
				unit = this.units.day;
			} else if (days >= 2 && days <= 4) {
				unit = this.units.days2;
			} else {
				unit = this.units.days;
			}
			buffer.push('<span class="days">' + days + ' ' + unit + '</span>');
		}
		if (hours >= 0) {
			if (hours == 1) {
				unit = this.units.hour;
			} else if (hours >= 2 && hours <= 4) {
				unit = this.units.hours2;
			} else {
				unit = this.units.hours;
			}
			buffer.push('<span class="hours">' + hours + ' ' + unit + '</span>');
		}
		if (minutes >= 0) {
			if (minutes == 1) {
				unit = this.units.minute;
			} else if (minutes >= 2 && minutes <= 4) {
				unit = this.units.minutes2;
			} else {
				unit = this.units.minutes;
			}
			buffer.push('<span class="minutes">' + minutes + ' ' + unit + '</span>');
		}
		if (seconds >= 0) {
			if (seconds == 1) {
				unit = this.units.second;
			} else if (seconds >= 2 && seconds <= 4) {
				unit = this.units.seconds2;
			} else {
				unit = this.units.seconds;
			}
			buffer.push('<span class="seconds">' + seconds + ' ' + unit + '</span>');
		}
		return '<span class="header">Volíme o</span> <span class="frame" id="countdownInner">' + buffer.join('') + '</span>';
	}
}
Countdown.countdown = function(element, date) {
	element = $(element);
	var refresh = function() {
		var milliseconds = new Date(2009, 2, 21, 7) - new Date();
		if (milliseconds <= 0) {
			milliseconds = new Date(2009, 2, 21, 22) - new Date();
			if (milliseconds > 0) {
				element.innerHTML = '<span class="header">Voľby práve prebiehajú</span>';
				milliseconds = 0;
			} else {
				milliseconds = new Date(2009, 3, 4, 7) - new Date();
				if (milliseconds <= 0) {
					milliseconds = new Date(2009, 3, 4, 22) - new Date();
					if (milliseconds > 0) {
						element.innerHTML = '<span class="header">Voľby práve prebiehajú</span>';
						milliseconds = 0;
					} else {
						element.innerHTML = '';
						milliseconds = -1;
					}
				}
			}
		}
		if (milliseconds > 0) {
			var duration = new Countdown(milliseconds);
			element.innerHTML = duration.toString();
		}
		if (milliseconds >= 0) {
			window.setTimeout(refresh, 1000);
		}
	}
	refresh();
}