if ('undefined' == typeof jQuery)
{ 
    var c = document.getElementsByTagName("head")[0];
    var s = document.createElement('script');
        s.type = 'text/javascript';
        s.src = 'http://tools.zlotemysli.pl/js/jquery.js';
        c.appendChild(s);
}

function timer_update()
{
    $('.timer').each(function()
    {
        var d = $(this).attr('timer_to').split('-');
        var h = $(this).attr('timer_hour');
        var f = $(this).attr('timer_format');
        var t = undefined;
        
        if (h)
        {
            h = h.split(':');
            t = _timer_logic(new Date(d[0], d[1] - 1, d[2], h[0], h[1], h[2]).getTime(), new Date().getTime());
        }
        else
        {
            t = _timer_logic(new Date(d[0], d[1] - 1, d[2], 23, 59, 59).getTime(), new Date().getTime());
        }

        if ('short' == f)
        {
                t[1] = t[1] + 24 * t[0];
            var s = ((t[1] < 10) ? '0' : '') +
                    Math.max(0, t[1]) +
                    ':' +
                    ((t[2] < 10) ? '0' : '') +
                    Math.max(0, t[2]) +
                    ':' +
                    ((t[3] < 10) ? '0' : '') +
                    Math.max(0, t[3]);
        }
				else if ('split' == f)
				{
								t[1] = t[1] + 24 * t[0];
								t[1] = (99 < t[1]) ? 99 : t[1];
						var s = "<span class='digits hours'>" +
										((t[1] < 10) ? '0' : '') +
		                Math.max(0, t[1]) + "</span>" +
										"<span class='digits minutes'>" + 
										((t[2] < 10) ? '0' : '') +
                    Math.max(0, t[2]) + "</span>" +
										"<span class='digits seconds'>" +
										((t[3] < 10) ? '0' : '') +
                    Math.max(0, t[3]) + "</span>";
				}
        else
        {
            var s = Math.max(0, t[0]) +
                    ((1 == Math.max(0, t[0])) ? ' dzień ' : ' dni ') +
                    ((t[1] < 10) ? '0' : '') +
                    Math.max(0, t[1]) +
                    ':' +
                    ((t[2] < 10) ? '0' : '') +
                    Math.max(0, t[2]) +
                    ':' +
                    ((t[3]<10) ? '0' : '') +
                    Math.max(0, t[3]);
        }

        $(this).html(s);
    });
}

function _timer_logic(d,o,t,x)
{
    return [x =~~ (t=(d-o)/864e5), x =~~ (t=(t-x)*24), x =~~ (t=(t-x)*60), ~~((t-x)*60)];
}


$(document).ready(function() {
    timer_update();
    setInterval("timer_update()", 1000);
});

