var javascript_countdown = function () {
	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 = '-';
 
	function countdown() {
		if(time_left < 2) {
			keep_counting = 0;
		}
 
		time_left = time_left - 1;
	}
 
	function add_leading_zero(n) {
		if(n.toString().length < 2) {
			return '0' + n;
		} else {
			return n;
		}
	}
 
	function format_output() {
		var days, hours, minutes, seconds, str;
		seconds = time_left % 60;
		minutes = Math.floor(time_left / 60) % 60;
		hours = Math.floor(time_left / 3600);
		days = Math.floor(hours / 24);
		hours = hours - (days*24);
 
		str = "";
		switch(days){
			case 0: break;
			case 1: str = "1 den, "; break;
			case 2:
			case 3:
			case 4: 
				str = days + " dny, "; break;
			default:
				str = days + " dní, "; break;
		}
		
		switch(hours){
			case 1: str = str + "1 hodina, "; break;
			case 2:
			case 3:
			case 4:
				str = str + hours + " hodiny, "; break;
			default:
				str = str + hours + " hodin, "; break;
		}
		
		switch(minutes){
			case 1: str = str + "1 minuta a "; break;
			case 2:
			case 3:
			case 4:
				str = str + minutes + " minuty a "; break;
			default:
				str = str + minutes + " minut a "; break;
		}
		
		switch(seconds){
			case 1: str = str + "1 vteřina"; break;
			case 2:
			case 3:
			case 4:
				str = str + seconds + " vteřiny"; break;
			default:
				str = str + seconds + " vteřin"; break;
		}
		
		
		/*
		seconds = add_leading_zero( seconds );
		minutes = add_leading_zero( minutes );
		hours = add_leading_zero( hours );
		*/
 
		return str;
	}
 
	function show_time_left() {
		$("#" + output_element_id).html(format_output());//time_left;
	}
 
	function no_time_left() {
		$("#" + output_element_id).innerHTML = no_time_left_message;
	}
 
	return {
		count: function () {
			countdown();
			show_time_left();
		},
		timer: function () {
			javascript_countdown.count();
 
			if(keep_counting) {
				setTimeout("javascript_countdown.timer();", 1000);
			} else {
				no_time_left();
			}
		},
		//Kristian Messer requested recalculation of time that is left
		setTimeLeft: function (t) {
			time_left = t;
			if(keep_counting == 0) {
				javascript_countdown.timer();
			}
		},
		init: function (t, element_id) {
			var akt = new Date();
			var trg = new Date(t);
			time_left = Math.floor((trg - akt) / 1000);
			output_element_id = element_id;
			javascript_countdown.timer();
		}
	};
}();

