$(document).ready(function(){
    // dodawanie komentarza
    // comments -> comments -> addAction()
    $("#addComment").click(function(){
         if(!lock){
             lock = true;
             
        	$('div.correct').css('display', 'none');
     		$('div.wrong').css('display', 'none');
        	 
     		$('#userNameError').text('');
    		$('#emailError').text('');
    		$('#commentTextError').text('');
     		
             $.ajax({
                 type: "POST",
                 url: BASE_URL+"/comments/comments/add",
                 data: $(".newsCommentForm").serialize(),
                 dataType: "json",
                 error: function (o){
                     alert('błąd');
                  },
                 success: function(msg){
                     if(msg.noValid == null){
                    	 var comment_ID = msg.comment_ID;
                    	 $('#commentList').load('/comments/comments/list/commentsModule/'+msg.commentsModule+'/', {obj_ID: msg.insertData.obj_ID}, function(){
                    		 
                    		 $('div.correct').css('display', 'block');
                    		 $('div.correct').hide();
                    		 $('div.correct').slideDown("slow");
                        	 
                        	 // czyszczenie formularza
                        	 
                        	 $('#userName').val('');
                        	 $('#userEmail').val('');
                        	 $('#userWww').val('');
                        	 
							 $('#komentarze input:checkbox').attr('checked', '');
							 $('#komentarze textarea').val('treść...');
							 
                        	 $('#komentarz-'+comment_ID).fadeIn("slow");
                        	 
                        	 
                        	 var newCommentNum = parseInt($('#commentNum').text());
                             $('#commentNum').html(newCommentNum + 1);
                    	 });

                     } else {
                    	 
                    	 $('div.wrong').css('display', 'block');
                    	 $('div.wrong').hide();
                    	 $('div.wrong').slideDown("slow");
                    	 
                         if(msg.noValid.userName != null)
                         {
                             $('#userNameError').text(msg.noValid.userName[0]);
                         }
                         
                         if(msg.noValid.userEmail != null)
                         {
                             $('#emailError').text(msg.noValid.userEmail[0]);
                         }
                         
                         if(msg.noValid.commentText != null)
                         {
                             $('#commentTextError').text(msg.noValid.commentText[0]);
                         }
                     }
                     lock = false;
                 }
             });
         }
         return false;
    });

    // autogrow dla textarea "dodaj komentarz"
    $('#commentText').autogrow({minHeight: 100});

    // markitup - bbcode/edytor
    // dla textarea "dodaj komentarz"

    // Add markItUp! to your textarea in one line
    // $('textarea').markItUp( { Settings }, { OptionalExtraSettings } );
    $('#commentText').markItUp(mySettings);

    // You can add content from anywhere in your page
    // $.markItUp( { Settings } );
    $('.add').click(function() {
         $.markItUp( {     openWith:'<opening tag>',
                        closeWith:'<\/closing tag>',
                        placeHolder:"New content"
                    }
                );
         return false;
    });

    // And you can add/remove markItUp! whenever you want
    // $(textarea).markItUpRemove();
    $('.toggle').click(function() {
        if ($("#commentText.markItUpEditor").length === 1) {
             $("#commentText").markItUpRemove();
            $("span", this).text("get markItUp! back");
        } else {
            $('#commentText').markItUp(mySettings);
            $("span", this).text("remove markItUp!");
        }
         return false;
    });

    // usuwanie komentarza (visibilityStatus ustawiany na 'delete')
    $('.deleteComment').live('click', function(){
         if(!lock){
             lock = true;
             var comment_ID = $(this).attr('rel');
             var post_ID = $(this).attr('postid');
             var href = $(this).attr('href');
             $.ajax({
                 type: "POST",
                 url: href,
                 data: "comment_ID="+comment_ID+"&post_ID="+post_ID,
                 dataType: "json",
                 error: function (o){
                     alert("comment error");
                  },
                 success: function(msg){
                     $('#komentarz-'+comment_ID).remove();
                     var newCommentNum = parseInt($('#commentNum').text());
                     $('#commentNum').html(newCommentNum - 1);
                     lock = false;
                 }
             });
         }
         return false;
    });


    // edytowanie swojego komentarza
    $('.editMyComment').live('click', function() {

        var comment_ID = $(this).attr('rel');
        var commentnr = $(this).attr('commentnr');

        $.ajax({
            type: "POST",
            url: "/comments/comments/edit-my-comment",
            data: "comment_ID="+comment_ID+"&commentnr="+commentnr,
            success: function(msg){
                if (msg != '') {
                    $('#komentarz-'+comment_ID).html(msg);
                    $('#editCommentText').markItUp(mySettings);

                }
            }
        });

        return false;
    });

    $('.saveMyComment').live('click', function() {
        var comment_ID = $(this).attr('rel');
        $.ajax({
            type: "POST",
            url: "/comments/comments/edit-my-comment",
            data: $(".editNewsCommentForm").serialize(),
            success: function(msg){
                if ((msg != '') && (msg != 'error')) {
                    $('#komentarz-'+comment_ID).html(msg);
                    $("#editCommentText").markItUpRemove();
                }
                else {
                    $('#editCommentTextError').text('Uzupełnij treść komentarza!');
                }
            }
        });

        return false;
    });

});