﻿$(document).ready( function() {
    $('.box-naglowek a').click( function() {
        var aktywny = $(this).parent('div').parent('div').children('.box-aktywny');
        var box = $(this).parent().parent().parent();
        var p = $('span#'+$(this).attr('name')+'-tresc');
        aktywny.removeClass();
        aktywny.addClass('box-nieaktywny');
        box.children('.box-tresc').children('span:visible').not(p).slideUp('normal', function () {
            $(p).slideDown('normal');
        });
        $(this).parent('div').removeClass();
        $(this).parent('div').addClass('box-aktywny');
        if($(this).attr('name') == 'shoutbox')
            sb_get();
        if($(this).attr('name') == 'tabela')
            tl_get();
    });
    $('button#sb_get').click( function() {
        sb_get();
    });
    $('input#sb_wyslij').click( function() {
        $.post('shoutbox.php?action=dodaj_wpis',
        {tresc: $('input#sb_tresc').val()},
        function() { sb_get(); });
        $('input#sb_tresc').val('');
        return false;
    });
    sb_get();
    tl_get();
    wczytaj();
    $('#aktualizuj_odswiez').click(function() {
        wczytaj();
    });
});
function sb_get() {
    $('#shoutbox-tresc div').load('./shoutbox.php', function() {
        $('#shoutbox a').click(function() {
            $.get('./shoutbox.php',
            { action: 'usun_wpis',
            id: $(this).attr('id') },
            function() {
                get_sb();
            });
            return false;
        });
    });
}
function tl_get() {
    $('#tabela-tresc div').load('./tabela.php');
}
function wczytaj() {
    $("#loading").ajaxStart(function(){
        $(this).show();
    });
    $("#loading").ajaxComplete(function(){
        $(this).hide();
    });
    $('#aktualizacja').load('./aktualizuj.php', function() {
        $('a').click( function() {
            var id = $(this).attr('id').split('-');        
            $.get('./aktualizuj.php',
            {
            typ: id[0],
            id: id[1],
            dzialanie: id[2]
            },
            function() {
                wczytaj();
            });
        });
    });
}
