function getCommentsPage(page,entry_id){
    //$('#loading').css("display","block");
    $.ajax({
        url: global_path+'pobierz_komentarze,'+page+','+entry_id+'.plain',
        //type: "POST",
        //data:{'name':$('#login').val()},
        dataType: "plain",
        timeout: 5000,
        error: function() {
            //$('#loading').css("display","none");
            $('#comments').html("Wystąpił błąd podczas łączenia z serwerem, spróbuj ponownie.");
        },
        success: function(data) {
            $('#comments').html(data);
            //$('#loading').css("display","none");
        }
    });
}

function getCommentsPageModerated(page,entry_id,blog_name){
    //$('#loading').css("display","block");
    $.ajax({
        url: global_path+'pobierz_komentarze_zmoderowane,'+page+','+entry_id+','+blog_name+'.plain',
        //type: "POST",
        //data:{'name':$('#login').val()},
        dataType: "plain",
        timeout: 5000,
        error: function() {
            //$('#loading').css("display","none");
            $('#comments').html("Wystąpił błąd podczas łączenia z serwerem, spróbuj ponownie.");
        },
        success: function(data) {
            $('#comments').html(data);
            //$('#loading').css("display","none");
        }
    });
}
    
function clickAddComment(isMain) {
    //$('#loading').css("display","block");
    if (logged) {
		
		var browser=navigator.appName;
		
		if (browser == "Microsoft Internet Explorer") {
			var zz = 0;
		}
		else {
			var zz = 0;
		};

		
        var logo_z = $('<img src="'+$("#logo_z").attr('src')+'" class="blockUI blockMsg blockPage" style="z-index:2000;position:absolute">')
        $([logo_z[0]]).appendTo('body');
        var position  = $("#logo_z").position();
        logo_z.css({'position': 'absolute','left':position.left+zz+'px','top':position.top+'px'});
        logo_z.show();

        var main_z = $('<img src="'+$("#main_z").attr('src')+'" class="blockUI blockMsg blockPage" style="z-index:2000;position:absolute">')
        $([main_z[0]]).appendTo( 'body' );
        position = $('#main_z').position();
        main_z.css({'position': 'absolute','left':position.left+zz+'px','top':position.top+'px','width': $("#main_z").width(),'height': $("#main_z").height()});
        main_z.show();

        position  = $("#comments").position();
        if(isMain) {
            $.blockUI({ message: $('#add_comment'), css: { position: 'absolute', width: '329px', left: position.left, top: position.top, background: 'none', border: 'none'} });
        }
        else {
            $.blockUI({ message: $('#add_comment'), css: { position: 'absolute', width: '513px', left: position.left, top: position.top, background: 'none', border: 'none'} });
        }
    } else {
    	var position  = $("#comments").position();
    	if(isMain) {
    	  	 $.blockUI({ message: $('#notlogged'), css: { position: 'absolute', width: $("#comments").width(), left: position.left, top: position.top, background: 'none', border: 'none' } });
        }
        else {
        	 $.blockUI({ message: $('#notlogged'), css: { position: 'absolute', width: $("#comments").width(), left: position.left, top: position.top, background: 'none', border: 'none' } });
        }
    }
}

function addComment(author_name,entry_id) {
    //$('#loading').css("display","block");
    $.unblockUI();
    $.ajax({
        url: global_path+'dodaj_komentarz,'+author_name+','+entry_id+'.plain',
        type: "POST",
        data:{'comment_new':$('#comment_new').val()},
        dataType: "plain",
        timeout: 5000,
        error: function() {
            //$('#loading').css("display","none");
            $('#comments').html("Wystąpił błąd podczas łączenia z serwerem, spróbuj ponownie.");
            $.unblockUI();
        },
        success: function(data) {
             if(data=="ok"){
                 getCommentsPage(0,entry_id);
             }
             else if(data=="niezalogowany") {
             	$('#add_comment').css("display","none");
             	$('#notlogged').css("display","block");
             }
             else {
             	alert(data);
             	getCommentsPage(0,entry_id); 
             }
            //$('#loading').css("display","none");
        }
    });
}

function addCommentModerated(author_name,entry_id,blog_name) {
    //$('#loading').css("display","block");
    $.unblockUI();
    $.ajax({
        url: global_path+'dodaj_komentarz,'+author_name+','+entry_id+','+blog_name+'.plain',
        type: "POST",
        data:{'comment_new':$('#comment_new').val()},
        dataType: "plain",
        timeout: 5000,
        error: function() {
            //$('#loading').css("display","none");
            $('#comments').html("Wystąpił błąd podczas łączenia z serwerem, spróbuj ponownie.");
        },
        success: function(data) {
             if(data=="ok"){
                getCommentsPageModerated(0,entry_id,blog_name);
                var position  = $("#comments").position();
                $.blockUI({ message: $('#added_comment_info'), css: { position: 'absolute', width: '329px', left: position.left, top: position.top, background: 'none', border: 'none' } });
             }
             else if(data=="niezalogowany") {
                $('#add_comment').css("display","none");
                $('#notlogged').css("display","block");
             }
             else {
                alert(data);
                getCommentsPageModerated(0,entry_id,blog_name); 
             }
            //$('#loading').css("display","none");
        }
    });
}

function deleteCommentPaged(comment_id,page,entry_id){
//$('#loading').css("display","block");
	$.ajax({
	    url: global_path+'usun_komentarz,'+comment_id+'.plain',
	    dataType: "plain",
	    timeout: 5000,
	    error: function() {
	        //$('#loading').css("display","none");
	        $('#comments').html("Wystąpił błąd podczas łączenia z serwerem, spróbuj ponownie.");
	    },
	    success: function(data) {
	        if(data=="ok"){
	            getCommentsPage(page,entry_id);	
	        }
	        else {
	        	alert(data);
	        }
	        //$('#loading').css("display","none");
	    }
	});
}

function reportComment(comment_id,blog_id){
    //$('#loading').css("display","block");
    
    $.ajax({
        url: global_path+'zglos_komentarz,'+comment_id+','+blog_id+'.plain',
        //type: "POST",
        //data:{'name':$('#login').val()},
        dataType: "plain",
        timeout: 5000,
        error: function() {
            //$('#loading').css("display","none");
            $('#comments').html("Wystąpił błąd podczas łączenia z serwerem, spróbuj ponownie.");
        },
        success: function(data) {
            //$('#comments').html(data);
            if(data=='ok') {
                $.blockUI({ message: $('#report_comment_info'), css: { width: '275px', background: 'none', border: 'none' } });
            }
            else {
            	alert(data);
            }
            //$('#loading').css("display","none");
        }
    });
}

function controlCommentCharsCounter(){ //funkcja sprawdza i pokazuje ilosc znakow podczas pisania nowego komentarza
    var MaxLengthComment = 2500;
    var textComment=document.getElementById("comment_new").value;
    if(textComment.length>MaxLengthComment){
        textComment=textComment.substr(0,MaxLengthComment);
        document.getElementById("comment_new").value=textComment;
    }
    //document.getElementById("licznikZnakowComment").value=MaxLengthComment-textComment.length;
    document.getElementById("commentCharsCounter").innerHTML=MaxLengthComment-textComment.length;
}

function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    return [curleft,curtop];
}

function toggle_comment(state,comment_id)
{
    if (state == 1)
    {
        $("#comment_whole"+comment_id).show();
        $("#comment_short"+comment_id).hide();
    }
    else
    {
        $("#comment_whole"+comment_id).hide();
        $("#comment_short"+comment_id).show();
    }
}