

function countos()
{
	var c_today;
	var c_name;
	var tos_v;

	clearTimeout(tos_t)
	el = document.getElementById('tos');
	if('undefined' != el)
   {
   	c_today = new Date();
   	c_name = 'tc_tos_' + c_today.getYear() + c_today.getMonth() + c_today.getDate();
   	tos_v = TC_getCookie( c_name );
   	tos_v++;
      if (tos_v%5 == 0)
      {
   		el.innerHTML = TC_formatTos( tos_v/5 );
   	}
		TC_setCookie(c_name, tos_v, 1);
		tos_t = setTimeout(function() {countos();}, 200);
   }
}

function TC_formatTos( tos )
{
	var tos_str;
	var tos_min_end;

	tos_min = ( tos>0 ? Math.floor( tos / 60 ) : 0 );
	if( tos_min == 0 )
	{
		tos_str = 'Witaj. Zacznij trenować z nami pamięć.'
	}
	else if( tos_min < 5 )
	{
		tos_str = 'Dziś ćwiczysz dopiero ' + tos_min + ' ' + (tos_min == 1 ? 'minutę' : ( tos_min<5 ? 'minuty' : 'minut') );
	}
	else if ( tos_min < 30 )
	{
		tos_str = 'Dziś już ćwiczysz ' + tos_min + ' minut';
		if( tos_min > 21)
		{
			tos_min_end = tos_min % 10;
			if( tos_min_end > 1 && tos_min_end < 5 )
			{
				tos_str += 'y';
			}
		}
	}
	else
	{
		tos_str = 'Dziś już ćwiczysz ' + tos_min + ' minut';
		tos_min_end = tos_min % 10;
		if( tos_min_end > 1 && tos_min_end < 5 )
		{
			tos_str += 'y';
		}
		tos_str += '. Czas na odpoczynek.';
		if( tos_min > 60)
		{
			tos_str += ' Serio.';
		}
	}
	return tos_str;
}

function TC_setCookie( c, v, exp )
{
	var expdate=new Date();
	expdate.setDate( expdate.getDate() + exp );
	document.cookie=c + '=' + escape(v) + ( (exp==null) ? "" : ";expires="+expdate.toGMTString() );
}

function TC_getCookie(c)
{
	if (document.cookie.length>0)
  {
		c_start = document.cookie.indexOf( c + '=' );
		if (c_start!=-1)
		{
			c_start = c_start + c.length + 1;
			c_end = document.cookie.indexOf( ';', c_start );
			if ( c_end==-1 )
			{
				c_end=document.cookie.length;
			}
			return unescape(document.cookie.substring(c_start,c_end));
		}
  }
	return '';
}

