function showCommentMore(objectId) {
    $j('#'+objectId).show("normal");
}

function hideCommentMore(objectId) {
    $j('#'+objectId).hide("normal");
}

function showComments(sourceId) {
    
    $j('#sourceid').val(sourceId);
    $j('#comtype').val(0);
    $j('#comid').val(0);
    $j('#comment-form').hide();
    $j('div .comments').show();

    getCommentsDataList(sourceId,1,1);

    $j('#myCom').appendTo('#commentBlock_'+sourceId);
    
    $j('#ca1').attr("href","javascript:getCommentsDataList("+sourceId+",1,1);");
    $j('#ca2').attr("href","javascript:getCommentsDataList("+sourceId+",1,2);");
    $j('#ca3').attr("href","javascript:getCommentsDataList("+sourceId+",1,3);");

}

function loadWall(userId) {

    $j('#comtype').val(0);
    $j('div .comments').show();

    getWallDataList(userId,1,1);
}

function commentAdd(sId) {
    $j('#c_Name').html("Dodaj komentarz");
    $j('#c_Submit').val("Dodaj komentarz");

    $j('#comtype').val(0);
    
    $j('#comment-form').show();     
}

function questionAdd(sId) {
    $j('#c_Name').html("Dodaj pytanie");
    $j('#c_Submit').val("Dodaj pytanie");

    $j('#comtype').val(1);
    $j('#comid').val(0);

    $j('#comment-form').show();    
}

function replyAdd(commentId) {
    $j('#c_Name').html("Dodaj odpowiedź");
    $j('#c_Submit').val("Dodaj odpowiedź");

    $j('#comtype').val(2);
    $j('#comid').val(commentId);

    $j('#comment-form').show();    
}

function commentVote_UP(id) {
    $j.ajax({
        type: "GET",
        url: "comment/vote/"+id+"/1",
        success: function(msg){
            alert(msg);
        }
    });
}

function commentVote_DOWN(id) {
    $j.ajax({
        type: "GET",
        url: "comment/vote/"+id+"/2",
        success: function(msg){
            alert(msg);
        }
    });
}
var timer;

function disableCommentForm(iterator) {
    $j('#comment-form-errors').html("<div id='ok_msg'>Poczekaj <b>"+iterator+"</b> sekund by dodać kolejną wiadomość.</div>");
    
    if (iterator<=0) {
        $j('#comment-form textarea').attr('disabled',false);
        $j('#comment-form textarea').val('');
        $j('#comment-form-errors').html('');
        $j('input#c_Submit').attr('disabled',false);
        
        clearTimeout(timer);
    } else {    
        iterator--;
        timer = setTimeout("disableCommentForm("+iterator+")",1000);
    }
}

/*** SUBMIT COMMENT ADD ***/
$j('#comment-form').ajaxForm(function(msg) {
    $j('#comment-form-errors').html('');
    if (msg == 1) {
        
        $j('#comment-form textarea').attr('disabled',true);
        $j('input#c_Submit').attr('disabled',true);
        disableCommentForm(5);

        // Przeladowanie bloku komentarzy
        if ($j('#sourceid').val() == 0) {
            if ($j('#comwallid').val() == 0) { } else
            {
                loadWall($j('#comwallid').val());
            }
        } else { 
            getCommentsDataList($j('#sourceid').val(),1,1);
        }

        if ($j('#comtype').val() == 0) { 
            $j('input#c_Submit').val("Komentarz został dodany");
        }
        else {
            $j('input#c_Submit').val("Pytanie zostało dodane");
        }

    } else {
        var mj = $j.evalJSON(msg);
        var tmpErrorText = '<div id="error_msg">'
        $j.each(mj, function(i,item){
            tmpErrorText += item+'<br />';
        });
        tmpErrorText += '</div>';
        $j('#comment-form-errors').html(tmpErrorText);
    }
});

$j('#commVal').keypress(function() {
    checkFieldLength(this,500,'c_commentField');
});

function getWallDataList(userID,page,listType) {
    $j('div#commentList').html('<tr><td><img src="http://i.szperus.pl/media/loading.gif" /><br /><br />Ładowanie komentarzy...</td></tr>');

    $j.ajax({
        type: "GET",
        url: "comment/getMyData/"+page+"/"+userID+"/4",
        success: function(msg) {
            $j('div#commentList').html('');
            $j('div#commentList').html(msg);
        }
    });
}

function getCommentsDataList(sourceID,page,listType) {
    $j('div#commentList').html('<tr><td><img src="http://i.szperus.pl/media/loading.gif" /><br /><br />Ładowanie komentarzy...</td></tr>');

    switch (listType) {
        case 1: // Wszystkie
            $j('#ca1').css('fontWeight','bold');
            $j('#ca2').css('fontWeight','normal');
            $j('#ca3').css('fontWeight','normal');
            break;
        case 2: //Komentarze
            $j('#ca2').css('fontWeight','bold');
            $j('#ca1').css('fontWeight','normal');
            $j('#ca3').css('fontWeight','normal');
            break;
        case 3: // Pytania
            $j('#ca3').css('fontWeight','bold');
            $j('#ca2').css('fontWeight','normal');
            $j('#ca1').css('fontWeight','normal');
            break;
    }


    $j.ajax({
        type: "GET",
        url: "comment/getMyData/"+page+"/"+sourceID+"/"+listType,
        success: function(msg) {
            $j('div#commentList').html('');
            $j('div#commentList').html(msg);
        }
    });
}
