/* js/arts.js */
$('document').ready(function() {
    $('#art-of-day-feed').live('click', function() {
        if($('#art-of-day-feed').attr('checked') == true) {
            $('#status-art-of-day').html('<img style="margin-left: 30px" src="/images/ajax-loader2.gif" />');
            $.post('/ajax.html', {action: 'art-of-day-feed'}, function(result) {
                if(result == '1') {
                    $('#status-art-of-day').html('<p style="font-size: 11px; text-align: center; color: green; font-weight: bold;">Dodano wpis na tablicy.</p>');
                }
                else {
                    $('#status-art-of-day').html(result);
                }
            });
        }
    });
});

function showArtOfDay() {
    if($('#art_of_day_button-min').children().eq(0).attr('id') != 'show-art-of-day')
        $('#art_of_day_button-min').append("<div id='show-art-of-day'><div class='close'><a href='' onclick='$(this).parent().parent().remove(); return false'>X</a></div><p>Ten utwór jest utworem dnia.</p><span id='status-art-of-day'><input id='art-of-day-feed' type='checkbox' />Podziel sie nim ze znajomimy dodając wpis na tablicy!</span></p></div>");
}

function deleteArt(idPost, iUser, obj) {
    //$('#document').html("<img class='ajax-loader' src='images/ajax-loader.gif' />");
    $('#profil_ajax_loader').show();
    if(idPost !== '') {
        $.get('/ajax.html', {action: 'deleteArt', iIdPost: idPost}, function(status) {
            if(status == 'ok') {
                updateProfil(obj);
            }
        });
    }
}

function updateProfil(obj) {
    $('#alert').fadeIn(1000);
    var target = $(obj).parent().parent().parent();
    target.next('div').remove();
    target.remove();
    $('#profil_ajax_loader').hide();

    setInterval(function() {if($('#alert').attr('display') !== 'none') {$('#alert').fadeOut(1000);}}, 3000);

    return true;
}

// iIdUser - numer uzytkownika - wlasciciela profilu
// iPage - numer strony
// iLimit - liczba wynikow wyswietlanych na stronie
// iCount - liczba wszystkich zwroconych wynikow ktore nalezy wyswietlic


function najnowszeArtykuly() {
    $('#document').html("<img class='ajax-loader' src='images/ajax-loader.gif' />");
    $.getJSON('/ajax.html', {action: 'najnowszeArtykuly'}, function(positions) {
        if(positions == null)
            showError();
        else
            updateDocument(positions);
       
        $("#header1").attr('class', 'header1-selected');
        $("#header2").attr('class', 'header2');
        $("#header3").attr('class', 'header3');
    });
}

function najnowszeArtykulyKategoria(iCategory) {
    $('#document').html("<img class='ajax-loader' src='images/ajax-loader.gif' />");
    $.getJSON('/ajax.html', {action: 'najnowszeArtykulyKategoria', category: iCategory}, function(positions) {
        if(positions == null)
            showError();
        else
            updateDocument(positions);

        $("#header1").attr('class', 'header1-selected');
        $("#header2").attr('class', 'header2');
        $("#header3").attr('class', 'header3');
    });
}

function najlepszeArtykulyMiesiac() {
    $('#document').html("<img class='ajax-loader' src='images/ajax-loader.gif' />");
    $.post('/najlepsze-z-miesiaca.html', {action: 'najlepszeArtykulyMiesiac'}, function(positions) {
        if(positions == null)
            showError();
        else {
            //updateDocument(positions);
            $('#document').html(positions);
        }

        $("#header1").attr('class', 'header1');
        $("#header2").attr('class', 'header2');
        $("#header3").attr('class', 'header3-selected');
    });
}

function najlepszeArtykulyMiesiacKategoria(iCategory) {
    $('#document').html("<img class='ajax-loader' src='images/ajax-loader.gif' />");
    $.getJSON('/ajax.html', {action: 'najlepszeArtykulyMiesiacKategoria', category: iCategory}, function(positions) {
        if(positions == null)
            showError();
        else
            updateDocument(positions);

        $("#header1").attr('class', 'header1');
        $("#header2").attr('class', 'header2');
        $("#header3").attr('class', 'header3-selected');
    });
}

function najlepszeArtykulyTydzien() {
    $('#document').html("<img class='ajax-loader' src='images/ajax-loader.gif' />");
    $.post('/najlepsze-z-tygodnia.html', {action: 'najlepszeArtykulyTydzien'}, function(positions) {

        if(positions == null) {
            showError();
        }
        else {
            //updateDocument(positions);
            $('#document').html(positions);
        }

        $("#header1").attr('class', 'header1');
        $("#header2").attr('class', 'header2-selected');
        $("#header3").attr('class', 'header3');
                    return true
    });
}

function najlepszeArtykulyTydzienKategoria(iCategory) {
    $('#document').html("<img class='ajax-loader' src='images/ajax-loader.gif' />");
    $.getJSON('/ajax.html', {action: 'najlepszeArtykulyTydzienKategoria',category: iCategory}, function(positions) {
        if(positions == null) {
            showError();
        }
        else
            updateDocument(positions);

        $("#header1").attr('class', 'header1');
        $("#header2").attr('class', 'header2-selected');
        $("#header3").attr('class', 'header3');
    });
}

function showError() {
            $('#document').empty();
             $('#document').append('<div>');
                          
             oDiv = $('#document').children('div').eq(0);
             oDiv.attr('id', 'showMessage');
             oDiv.css('padding', '20px 0px 0px 70px');

             oDiv.append('<div>');
             oMsg = oDiv.children('div').eq(0);
             oMsg.attr('id', 'msgInfo2');
             oMsg.append('<div>');
             oMsg.append('<div>');
             oMsg.append('<div>');

             oMsg.children('div').eq(0).addClass('msg_up');
             oMsg.children('div').eq(1).addClass('msg_center');
             oMsg.children('div').eq(2).addClass('msg_down');

             oCenter = oMsg.children('div').eq(1);
             oCenter.append('<div>');
             oCenter.append('<div>');

             oLeft = oCenter.children('div').eq(0);
             oLeft.addClass('left');
             oLeft.append('<img>');
             oLeft.children('img').attr('alt', 'znaczek info');
             oLeft.children('img').attr('src', '/images/znaczek.png');


             oRight = oCenter.children('div').eq(1);
             oRight.addClass('right');
             oRight.append('<p>');
             oRight.children('p').css('color', 'red');
             oRight.children('p').text('Brak artykułów w serwisie poezje.pl');

             $('#document').append('<div>');
             $('#document').children('div').eq(1).addClass('position-down');
}

function updateDocument(positions) {
    if(positions.length > 0) {
            $('#document').empty();
            for(i=0; i<positions.length; i++) {
                $('#document').append('<div>');
                var oPosition = $('#document').children('div').last();
                oPosition.addClass('position');
                oPosition.attr('id', positions[i].sMotyw+'-mini');
                
                oPosition.append('<div>');
                oPosition.append('<div>');

                var oBackground = oPosition.children('div').eq(1);
                oBackground.addClass('background');
                oBackground.append('<div>');
                oBackground.append('<div>');
                oBackground.append('<div>');

                var oPositionLeft = oPosition.children('div').eq(0);
                oPositionLeft.addClass('points_left');

                var oPoints = oBackground.children('div').eq(0);
                oPoints.addClass('add_point');
                oPoints.attr('id', positions[i].iId);
                oPoints.append('<p>');
                oPoints.append('<a id="glos_minus" href=""><img src="images/minus.png" /></a>');
                oPoints.append('<a id="glos_plus" href=""><img src="images/plus.png" /></a>');

                oPoints.children('p').text(positions[i].punkty);

                var oTitle = oBackground.children('div').eq(1);
                oTitle.addClass('title');

                oTitle.append('<p>');
                oTitle.append('<p>');

                oTitle.children('p').eq(0).append('<a href="/utwor,'+positions[i].iId+','+positions[i].sTytulUrl+'.html">'+positions[i].sTytul+'</a>');

                
                oTitle.children('p').eq(1).append('<span><i>Autor: <a href="/profil,'+positions[i].userId+','+positions[i].autor+'.html">'+positions[i].autor+'</a></i></span>');
                oTitle.children('p').eq(1).append('<span><i>Kategoria: <a href="/utwory,'+positions[i].kategoriaUrl+'.html">'+positions[i].kategoria+'</a></i></span>');
                oTitle.children('p').eq(1).append('<span><i>Komentarze: <a href="utwor,'+positions[i].iId+','+positions[i].sTytulUrl+'.html#comments">'+positions[i].iLiczbaKomentarzy+'</a></i></span>');
 
                //oTitle.children('p').eq(1).children('span').eq(2).children('i').text('Komentarzy:');
                //oTitle.children('p').eq(1).children('span').eq(2).children('a').text(positions[i].iLiczbaKomentarzy);
                //oTitle.children('p').eq(1).children('span').eq(2).children('a').attr('href', '/utwor,'+positions[i].iId+','+positions[i].sTytulUrl+'.html');

                var oDate = oBackground.children('div').eq(2);
                oDate.addClass('date');
                oDate.html('<span>'+positions[i].day+'</span>'+positions[i].date);

                $('#document').append('<div>');
                $('#document').children('div').last().attr('id', 'status');
                $('#document').children('div').last().attr('style', 'margin: -60px 0px 0px 60px; position: absolute; width: 81px; height: 35px; background: url("/images/chmura.png"); *margin: 0px 0px 0px 0px; display: none; padding: 8px 5px 5px 15px; font-size: 11px; color: #fff;');

                $('#document').append('<div>');
                $('#document').children('div').last().addClass('bar');
            }

            $('#document').append('<div>');
            $('#document').children('div').last().addClass('position-down');
        }
        return true;
}

