var seconds;


seconds2time = function( seconds )
{
	if( seconds >= 86400 )
	{
		days = Math.floor( seconds / 86400 );
		seconds = seconds - days * 86400;
		
	}
	else
	{
		days = 0;
	}

	res = new Array;
	var v = seconds;
	var k = 60;
	var i = 1;

	while( v >= k)
	{
	   last = v - Math.floor( v / k ) * k;
	   v =  (v - last) / k;
	   res.push( last > 9 ? last :  "0" + last.toString());

	}

	res.push( v > 9 ? v : "0" + v.toString());

	if( res.length == 1) res.push('00');
	res = res.reverse();
	//alert( res );
	day_name = days != 1 ? "dni" : "dzień";
	return days + " " + day_name + ", " + res.join(':');
}

function newTime( time )
{
	time = time2seconds( time );
	if(time == 0) time++;
	time = time - 1;
	time = seconds2time( time );
	return time;
}


function changeCounter()
{
	seconds = seconds - 1;
	$("#opening_counter").html( seconds2time( seconds ) );
}


$(document).ready(function()
{

	if( seconds = $("#opening_counter").html() )
	{
		
		$("#opening_counter").html( seconds2time( $("#opening_counter").html() ) );
		setInterval( changeCounter, 1000 );
	}
});
