jQuery(function(){
    jQuery('.addNewComment').hide();
    jQuery('.replyComment').hide();

    function regenerateCaptcha(){
        jQuery.ajax({
            url: 'captcha_generate',
            type:    'POST',
            async:   false,
            dataType:    'json',
            success: function(result){
                if(result.img != ''){
                    jQuery('#formComment .img_cap img').remove();
                    jQuery('#formComment .img_cap').html(result.img);
                    jQuery('#formComment input[name=comment_status]').attr('value',result.status);
                }
            }
        });
    }

    function verifyCaptcha(capImg,capInput){
        var returnData = false;

        jQuery.ajax({
            url: 'captcha_verify',
            async:   false,
            type:    'POST',
            dataType:    'json',
            data:    {
                captcha_img: ''+capImg+'',
                captcha_code:    ''+capInput+''
            },
            success: function(result){
                if(result.status == 'true'){
                    returnData = result.status;
                }else if(result.status == 'false'){
                    returnData = result.status;
                }
            }
        });

        return returnData;
    }

    jQuery('#boxComments .border_d8d8d8 .button_add').click(function(){
        jQuery('#comments .replyComment').hide();
        jQuery('#boxComments .addNewComment').show();
        jQuery('#boxComments #noComments').hide();
    });

    jQuery('#boxComments .addNewComment .button_close').click(function(){
        jQuery('#boxComments .addNewComment').hide();
        jQuery('#boxComments #noComments').show();
    });

    jQuery('#boxComments #comments .commentReply').click(function(){
        jQuery('#boxComments #comments .replyComment').hide();
        jQuery('#boxComments .addNewComment').hide();

        var replyID = jQuery(this).attr('id');

        jQuery('#boxComments #comments .replyID_'+replyID).show();
    });

    jQuery('#boxComments #comments .replyComment .button_close').click(function(){
        jQuery('#boxComments #comments .replyComment').hide();
    });

    jQuery('#formComment .img_ref').click(function(){
        regenerateCaptcha();
    });
    
    $("#boxComments").find("form").each(function(i){
        jQuery(this).submit(function(){
            var text = '';
            var counter = '0';

            if(jQuery('input.comment_author', this).val() == ''){
                text += 'Pole "Autor" jest puste.\n';
                counter = parseInt(counter) + parseInt(1);
            }
            if(jQuery('input.comment_author', this).val().length > '12'){
                text += 'Pole "Autor" jest za długie.\n';
                counter = parseInt(counter) + parseInt(1);
            }

            if(jQuery('input.comment_title', this).val() == ''){
                text += 'Pole "Tytuł" jest puste.\n';
                counter = parseInt(counter) + parseInt(1);
            }
            if(jQuery('input.comment_title', this).val().length > '120'){
                text += 'Pole "Tytuł" jest za długie.\n';
                counter = parseInt(counter) + parseInt(1);
            }

            if(jQuery('textarea.comment_content', this).val() == ''){
                text += 'Pole "Komentarz" jest puste.\n';
                counter = parseInt(counter) + parseInt(1);
            }else{
                if(jQuery('textarea.comment_content', this).val().length < '5'){
                    text += 'Pole "Komentarz" jest za krótki.\n';
                    counter = parseInt(counter) + parseInt(1);
                }else if(jQuery('textarea.comment_content', this).val().length > '2000'){
                    text += 'Pole "Komentarz" jest za długi.\n';
                    counter = parseInt(counter) + parseInt(1);
                }
            }

            if(counter == '0'){
                var capInput = jQuery('input.captcha_code', this).val();
                var capImg = jQuery("#formComment .img_cap img").attr('src');

                if(capInput == ''){
                    alert('Pole "Kod obrazka" jest puste.\n');

                    return false;
                }else{
                    if(verifyCaptcha(capImg,capInput) == 'true'){
                        return true;
                    }else{
                        regenerateCaptcha();

                        alert('Niepoprawny kod obrazka.\n');
                        jQuery('#captcha_code').val('');

                        return false;
                    }
                    return false;
                }
                return false;
            }else{
                if(text != ''){
                    alert(text);
                }

                return false;
            }
        });
    });
});

