/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
$(function() {
    $('h2').each(function() {
        $(this).append( '<span class="titleSh">' + $( '.title', this).text() + '</span>' );
    });
    $('.niceSelect').sSelect();

    $('#tabBottom .help, #tabBottom .help2, #topMenu .help, .helplink').click(function() {

        var href = $(this).attr('href');
        window.open(href, 'Pomoc', 'width=580,height=' + (  Math.ceil( (screen.height-50) * 0.75 )  ) + ',toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,directories=no,status=no');
        return false;
    });

    $('#adv a, #bigAdv a, #ad > a').click( function() {
        $( this ).attr( 'target', 'bla' );
    } );
   
});
$(function() {
   $('#tabBottom a').hover(function() {
       if(!$(this).hasClass('active'))
         $(this).css('z-index', 2).css('backgroundPosition', '0px 40px');
   }, function() {
        if(!$(this).hasClass('active'))
        $(this).css('z-index', 1).css('backgroundPosition', '0px 0px');
   }).each(function() {
      if(!$(this).hasClass('active'))
        $(this).css('z-index', 1);
   });
});
$(function() {
   $('#topMenu a').hover(function() {
       if(!$(this).hasClass('active'))
         $(this).css('z-index', 4).css('backgroundPosition', '0px -35px');
   }, function() {
        if(!$(this).hasClass('active'))
        $(this).css('z-index', 1).css('backgroundPosition', '0px 0px');
   }).each(function() {
      if(!$(this).hasClass('active'))
        $(this).css('z-index', 1);
   });
});

$(function() {
    todayIs();
    setInterval(todayIs, 500);
});
function todayIs() {
    var d = new Date();
    $('#todayIs').text( 'Dzisiaj: ' + ( d.getDate() < 10 ? '0' : '' ) + d.getDate() + '-' + ( d.getMonth() < 9 ? '0' : '' ) + ( d.getMonth() + 1 ) + '-' + d.getFullYear() + ' ' + ( d.getHours() < 10 ? '0' + d.getHours() : d.getHours()  ) + ':' +  ( d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes()  ) + ':' + ( d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds()  ) );
}

/*
 * Target=_BLANK ;)
 */
$( function() {
    $('.external, #rek a, #bigAdv a').click( function() {
        this.target = 'bla';
    } );
} );

/**
 * time to end
 */
$(function() {
    $( '.end[time]' ).addClass('redTime').each( function() {
        var el = $(this);
        var arr = el.attr('time').split(',');

        var toEnd = new Date( parseInt(arr[0]),
                              parseInt(arr[1]) - 1,
                              parseInt(arr[2]),
                              parseInt(arr[3]),
                              parseInt(arr[4]),
                              parseInt(arr[5])
                            );

        el.attr('date', toEnd);
    } );
    setEnd();
    setInterval( setEnd, 1000 );
});

function setEnd() {
    var now =  new Date();
    $( '.end[time]' ).each(function() {
        el = $(this);

        var diff = now - new Date(el.attr('date') );
        if(diff >= 0) {
            el.text('zakończona');
        } else {
            diff *= (-1);
            //var milliseconds=Math.floor(diff % 1000);
                diff=diff/1000;
            var seconds=Math.floor(diff % 60);
               diff=diff/60;
            var minutes=Math.floor(diff % 60);
               diff=diff/60;
            var hours=Math.floor(diff % 24);
               diff=diff/24;
            el.text( ( hours < 10 ? '0' + hours : hours ) + ':' + ( minutes < 10 ? '0' + minutes : minutes ) + ':' + ( seconds < 10 ? '0' + seconds : seconds ));
        }


    });
}
