$(document).ready(function() {
	var oldTime = [-1,-1,-1,-1];
	
	$('#countdown .hodStr').html('hod');
	$('#countdown .minStr').html('min');
	
	$("#countdown").countdown({
		date:"1/1/2012 00:00:00 AM",
		interval: 1000,
		delayStart:1,
		onTick:function(control, day, hrs, min, sec) {
			var id = '#'+control.attr('id');
			var startColor = 'red';
			var endColor = '#820000';
			if( oldTime[0]== -1 ){
				$(id+' .secStr').html('sec');
			}
			
			if( oldTime[2] != min ){
				if( oldTime[1] != hrs ){
					if( oldTime[0] != day ){
						$(id+' .den')
							.animate(
								{
									backgroundColor: startColor
								}
								,100
							)
							.html(day)
							.animate(
								{
									backgroundColor: endColor
								}
								,200
							)
						;
						var denStr = 'dní';
						if( day == 1 ) denStr = 'den';
						if( day >1 && day < 5 ) denStr = 'dny';
						$(id+' .denStr').html(denStr);
						
					}
					$(id+' .hod')
						.animate(
							{
								backgroundColor: startColor
							}
							,100
						)
						.html(hrs)
						.animate(
							{
								backgroundColor: endColor
							}
							,200
						)
					;
				}
				$(id+' .min')
					.animate(
						{
							backgroundColor: startColor
						}
						,100
					)
					.html(min)
					.animate(
						{
							backgroundColor: endColor
						}
						,200
					)
				;
			}else{
				$('.casTimer').css('background-color', endColor);
			}			
			$(id+' .sec')
				.animate(
					{
						backgroundColor: startColor
					}
					,100
				)
				.html(sec)
				.animate(
					{
						backgroundColor: endColor
					}
					,200
				)
			;
			
			oldTime[0] = day;
			oldTime[1] = hrs;
			oldTime[2] = min;
			oldTime[3] = sec;
		},
		onFinish:function(control, day, hrs, min, sec) {
			$('#nadpisNovyRok').remove();
			control.html("<h2>Šťastný nový rok!</h2>");
		} 
	});
});
