(function(document, $) {

    'use strict';

    var init = function() {
        $(document).ready(function() {

            // Panel 0
            $('#rate-this-topic-panel-0 #rate-this-topic-yes').click(function() {
                saveRateThisTopicYesNo($(this).val());
                $('#rate-this-topic-panel-0').fadeOut('slow', function() {
                    $('#rate-this-topic-panel-1').show();
                });
            });
            $('#rate-this-topic-panel-0 #rate-this-topic-no').click(function() {
                saveRateThisTopicYesNo($(this).val());
                $('#rate-this-topic-panel-0').fadeOut('slow', function() {
                    $('#rate-this-topic-panel-2').show();
                });
            });

            // Panels 1 & 2
            $('.rate-this-topic-skip').click(function() {
                showRateThisTopicFirstPanel($(this));
            });
            $('.rate-this-topic-send-feedback').click(function() {
                var $parent = $(this).closest('.rate-this-topic-panel');
                createJiraTicket($parent);
                showRateThisTopicFinalMessage($(this));
            });

        });
    },

    saveRateThisTopicYesNo = function(rateThisTopicYesNo) {
        if ($('#rate-this-topic-yes-no').length) {
            $('#rate-this-topic-yes-no').val(rateThisTopicYesNo);
        } else {
            $('.rate-this-topic').append('<input type="hidden" id="rate-this-topic-yes-no" value="'+rateThisTopicYesNo+'">');
        }
    },

    showRateThisTopicFinalMessage = function($element) {
        var $parent = $element.closest('.rate-this-topic-panel');
        $parent.fadeOut('slow', function() {
            $('#rate-this-topic-panel-3').show();
        });
    },

    showRateThisTopicFirstPanel = function($element) {
        var $parent = $element.closest('.rate-this-topic-panel');
        $parent.fadeOut('slow', function() {
            $('#rate-this-topic-panel-0').show();
        });
    },

    createJiraTicket = function(context) {

        var rateThisTopicCaptcha = $('.g-recaptcha-response', context).val();
        var rateThisTopicYesNo = $('#rate-this-topic-yes-no').val();
        var rateThisTopicHowToImprove = [];

        if (rateThisTopicYesNo == 'Yes') {
            var rateThisTopicThoughts = $('#rate-this-topic-thoughts-1').val();
        } else {
            var rateThisTopicThoughts = $('#rate-this-topic-thoughts-2').val();

            $('.rate-this-topic input:checked').each(function(index) {
                rateThisTopicHowToImprove.push($(this).val());
            });
        }

        $.ajax({
            url: 'https://websearch.itrsgroup.com/jira/create',
            type: 'post',
            dataType: 'json',
            data: {
                'rate-this-topic-recaptcha': rateThisTopicCaptcha,
                'rate-this-topic-yes-no': rateThisTopicYesNo,
                'rate-this-topic-thoughts': rateThisTopicThoughts,
                'rate-this-topic-how-to-improve': rateThisTopicHowToImprove
            }
        });

    };

    init();

})(document, jQuery);

// Captcha implementation
var captchaVerifyCallback1 = function(response) {
    var $button = jQuery('#rate-this-topic-panel-1 .rate-this-topic-send-feedback');
    $button.attr('disabled', response == '');
};

var captchaVerifyCallback2 = function(response) {
    var $button = jQuery('#rate-this-topic-panel-2 .rate-this-topic-send-feedback');
    $button.attr('disabled', response == '');
};