/* js/comments.js  */
function showMessage(text) {
            $('.showMsg').empty();
            $('.showMsg').append('<div>');
            msgInfo2 = $('.showMsg').children('div');
            msgInfo2.attr('id', 'msgInfo2');
            msgInfo2.append('<div>');
            msgInfo2.append('<div>');
            msgInfo2.append('<div>');

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

            center = msgInfo2.children('div').eq(1);

            center.append('<div>');
            center.append('<div>');

            center.children('div').eq(0).addClass('left');
            center.children('div').eq(1).addClass('right');

            center.children('div').eq(0).html('<img src="/images/znaczek.png" alt="znaczek info" />');
            center.children('div').eq(1).html(text);
            $('.showMsg').fadeIn();

            setTimeout(function() {$('.showMsg').fadeOut();}, 4000);
}

function editComment(obj, iId, sTime) {
    sText = $(obj).parent().parent().children('.text').text();
    $(obj).hide();
    $(obj).parent().parent().children('.text').html('<table class="comment-edit"><tr><td><textarea id="sNewText">'+sText+'</textarea></td></tr><tr><td><input type="button" onclick="saveComment(this, \''+iId+'\', \''+sTime+'\'); return false;" value="" style="background-image: url(\'/images/zapisz.jpg\'); background-repeat: no-repeat; width: 61px; height: 21px; border-width: 0px" /></td></tr></table>');

}

function saveComment(obj, iId, sTime) {
    var Today = new Date();
    var Month = Today.getMonth();
    var Day = Today.getDate();
    if(Day<=9)
        Day = '0'+Day;
    var Year = Today.getFullYear();
    var Hour = Today.getHours();
    if(Hour<=9)
        Hour = '0'+Hour;
    var Minutes = Today.getMinutes();
    if(Minutes<=9)
        Minutes = '0'+Minutes;
    var Seconds = Today.getSeconds();
    if(Seconds<=9)
        Seconds = '0'+Seconds;

    $(obj).parent().parent().parent().parent().parent('.text').parent('.text').children().eq(0).children().eq(1).show();
    $(obj).parent().parent().parent().parent().parent('.text').parent('.text').children().eq(1).html('('+sTime+')'+'<span class="commentUpdate">Ostatnia aktualizacja: '+Hour+':'+Minutes+':'+Seconds+' '+Day+'.'+Month+'.'+Year+'</span>');
    $(obj).parent().parent().parent().parent().parent('.text').parent('.text').children().eq(0).children().eq(1).css('display', 'inline');
    sText = $(obj).parent().parent().parent().children().eq(0).children().children().val();

    $.post('/ajax.html', {action: 'updateComment', id: iId, text: sText}, function(status) {
        if(status == 'ok') {
             $(obj).parent().parent().parent().parent().parent('.text').parent('.text').prepend('<p id="status-comment" style="color: green; text-align: center;">Komentarz został zapisany</p>');

             $(obj).parent().parent().parent().children().eq(0).children().html('<p class="text">'+sText+'</p>');
             $(obj).remove();

             setTimeout(function() {$('#status-comment').remove();}, 3000);
        }
    });
}

function addCommentForArt(user_id, post_id, user_name, avatar) {
    text = $('#sCommentText').val();
    userId = user_id;
    postId = post_id;
    userName = user_name;
    if(userId == '') {
        showMessage('<p style="color: red">Musisz się zalogwać aby dodać komentarz</p>');
            return false;
    }
    $.post('/ajax.html', {action: 'addCommentForArt', sText: text, iIdUser: userId, sUser: userName, iIdPost: postId}, function(status) {
        if(status!='Nie wpisałeś treści komentarza!') {
            //getCommentsForArt(post_id);
            showMessage('<p style="color: green">Twój komentarz został dodany.</p>');

            last_div = $('#comments').children().eq(0);
    
            if(last_div.attr('class') == 'comment-white') {
                sClass = 'comment';
            }
            else {
                sClass = 'comment-white'
            }

            var currentTime = new Date()
            var month = currentTime.getMonth() + 1
            var day = currentTime.getDate()
            var year = currentTime.getFullYear()
            var hours = currentTime.getHours()
            var minutes = currentTime.getMinutes()
            var seconds = currentTime.getSeconds()

            var sDate = hours+':'+minutes+':'+seconds+', '+day+'.'+month+'.'+year+' ';

            if(avatar == '')
                avatar = 'domyslny.png';
           
           
            $('#comments').append('<div class="'+sClass+'"><div class="left"><a href="/profil,'+user_id+','+user_name+'.html"><img style="width: 44px; height: 44px;" src="'+avatar+'" /></a></div><div class="text"><p class="name"><span>'+user_name+'</span><a class="editComment" href="" onclick="editComment(this, \''+status+'\', \''+sDate+'\');return false;"><img src="/images/edytuj_.png" /></a></p><p class="comment_date">('+sDate+')</p><p class="text">'+text+'</p></div><div class="pen"></div></div>');
            $('#commentsError').hide();
            $('#sCommentText').val('');
            if($('.comment-header').css('display')=='none')
                $('.comment-header').show();

            if($('#comments').css('display')=='none')
                $('#comments').show();
            //$('.showForm').hide();
        }
        else {
            showMessage('<p style="color: red">'+status+'</p>');
        }
                return false;
    });
}

function addCommentForUser(user_id, post_id, user_name, autor_id, avatar) {
    text = $('#sCommentText').val();
    $.post('/ajax.html', {action: 'addCommentForUser', sText: text, iIdAutor: user_id, sUser: user_name, iIdPost: post_id, iIdUser: autor_id}, function(status) {
        if(status!='Nie wpisałeś treści komentarza!') {
            //getCommentsForUser(post_id);
            $('#commentError').children('#msgInfo2').children('.msg_center').children('.right').html('<p style="color: green;">Twój komentarz został dodany.</p>');
            $('#commentError').fadeIn(1000);

            last_div = $('#comments').children().eq(0);

            if(last_div.attr('class') == 'comment-white') {
                sClass = 'comment';
            }
            else {
                sClass = 'comment-white'
            }

            var currentTime = new Date()
            var month = currentTime.getMonth() + 1
            var day = currentTime.getDate()
            var year = currentTime.getFullYear()
            var hours = currentTime.getHours()
            var minutes = currentTime.getMinutes()
            var seconds = currentTime.getSeconds()

            var sDate = hours+':'+minutes+':'+seconds+', '+day+'.'+month+'.'+year;

            if(avatar == '')
                avatar = 'domyslny.png';

            $('#comments').append('<div class="'+sClass+'"><div class="left"><a href="/profil,'+user_id+','+user_name+'.html"><img style="width: 44px; height: 44px;" src="'+avatar+'" /></a></div><div class="text"><p class="name"><span>'+user_name+'</span><a class="editComment" href="" onclick="editComment(this, \''+status+'\', \''+sDate+'\');return false;"><img src="/images/edytuj_.png" /></a></p><p class="comment_date">('+sDate+')</p><p class="text">'+text+'</p></div><div class="pen"></div></div>');
            $('#sCommentText').val("");
            $('#commentsError').hide();

            if($('#comment-header').css('display')=='none')
                $('#comment-header').show();

            if($('#comments').css('display')=='none')
                $('#comments').show();

            setInterval(function() {if($('#commentError').attr('display') !== 'none') {$('#commentError').fadeOut(1000);}}, 3000);
        }
        else {
            $('#commentError').children('#msgInfo2').children('.msg_center').children('.right').html('<p style="color: green;">'+status+'</p>');

            $('#commentError').fadeIn(1000);
            $('#sCommentText').val("");

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


