//----- Train support modules -----

// Module for the footer steps
$m.core.register('#Train-Support', function (hub) {
    var containerHeight,
        self = {};

    self.construct = function () {
        // Get the container height
        containerHeight = $m.jQuery(hub.container).innerHeight();

        // Set height of footer columns
        $m.jQuery(hub.find('.step')).height(containerHeight - 40);
    };

    self.destruct = function () {

    };

    return self;
});
// -- END -- Module for the footer steps

// Module for the "Support-webTRAIN.aspx" page
$m.core.register('#Support-Landing-Page', function (hub) {
    var supportCriteria,
        supportCriteriaHeight,
        supportQuestions,
        supportQuestionsHeight,
        getSatisfactionQuestions,
        self = {};

    self.construct = function () {
        supportCriteria = $m.jQuery(hub.find('.support-criteria')[0]);

        supportCriteriaHeight = supportCriteria.height();

        supportCriteria.height(0).css('opacity', 0);

        supportQuestions = $m.jQuery(hub.find('.support-questions')[0]);

        supportQuestionsHeight = supportQuestions.height();

        supportQuestions.height(0).css('opacity', 0);

        getSatisfactionQuestions = $m.jQuery(hub.find('.gsfn_link'));

        $m.jQuery.each(getSatisfactionQuestions, function () {
            $m.jQuery(this).attr('target', '_blank');
        });

        // Check query string for "decide=no"
        if ($m.jQuery.queryString['decide'] === 'no') {
            self.showGetSatisfaction();
        } else {
            console.log('hello');
            self.showSupportHome();
        }
        // -- END -- Check query string for "decide=no"

        // Shows and hides panels (.support-criteria and .support-questions) on the support-webTRAIN.aspx page
        $m.jQuery('.questions-link').click(function (e) {
            e.preventDefault();
            e.stopPropagation();

            self.showGetSatisfaction();

            return false;
        });

        $m.jQuery('.back').click(function (e) {
            e.preventDefault();
            e.stopPropagation();

            self.showSupportHome();

            return false;
        });
        // -- END -- Shows and hides panels (.support-criteria and .support-questions) on the support-webTRAIN.aspx page

        $m.jQuery('#fdbk_iframe').removeAttr('width').removeAttr('height').css({ 'width': '100%', 'height': '500px' });
    };

    self.destruct = function () {

    };

    self.showGetSatisfaction = function () {
        $m.jQuery('#BreadCrumb').text('SUPPORT > How webTRAIN works');

        if (supportCriteria.height() > 0) {
            hub.applyCSS(supportQuestions, 'display', 'block');
            supportCriteria.animate({
                height: 0,
                opacity: 0
            }, 450, 'easeInOutQuad', function () {
                supportCriteria.css('display', 'none');
                hub.applyCSS(supportQuestions, 'display', 'block');
                supportQuestions.animate({
                    height: supportQuestionsHeight,
                    opacity: 1
                }, 450, 'easeInOutQuad');
            });
        } else {
            hub.applyCSS(supportQuestions, 'display', 'block');
            supportQuestions.animate({
                height: supportQuestionsHeight,
                opacity: 1
            }, 450, 'easeInOutQuad');
        }
    };

    self.showSupportHome = function () {
        if (supportQuestions.height() > 0) {
            supportQuestions.animate({
                height: 0,
                opacity: 0
            }, 450, 'easeInOutQuad', function () {
                supportQuestions.css('display', 'none');
                supportCriteria.animate({
                    height: supportCriteriaHeight,
                    opacity: 1
                }, 450, 'easeInOutQuad');
            });
        } else {
            hub.applyCSS(supportCriteria, 'display', 'block');
            supportCriteria.animate({
                height: supportCriteriaHeight,
                opacity: 1
            }, 450, 'easeInOutQuad');
        }
    };

    return self;
});
// -- END -- Module for the "Support-webTRAIN.aspx" page

// Module for the "Support.aspx" page
$m.core.register('#Support-Page', function (hub) {
    var faqDivHeight,
        column2Height,
        self = {};

    self.construct = function () {
        //Variable declarations
        faqDivHeight = $m.jQuery('.faq').height();
        column2Height = $m.jQuery('#Column2-Content').height();

        //Sets column2 height if faq div is taller than column2
        if (faqDivHeight > column2Height) {
            $m.jQuery('#Column2-Content').animate({
                height: faqDivHeight + 40
            }, 450, 'easeInOutQuad');
        }
        // -- EBD -- Sets column2 height if faq div is taller than column2

        //Shows and hides the FAQ panel
        $m.jQuery('.show-faq').click(function (e) {
            e.preventDefault();

            e.stopPropagation();

            $m.jQuery('.show-faq').animate({
                left: '5px'
            }, 300, 'easeInOutQuad', function () {
                $m.jQuery('.faq').animate({
                    left: '21%'
                }, 300, 'easeInOutQuad', function () {
                    $m.jQuery('.close-faq').fadeIn(300);
                });
            });

            return false;
        });

        $m.jQuery('.close-faq').click(function (e) {
            e.preventDefault();

            e.stopPropagation();

            $m.jQuery('.faq').animate({
                left: '101%'
            }, 300, 'easeInOutQuad', function () {
                $m.jQuery('.show-faq').animate({
                    left: '-40px'
                }, 300, 'easeInOutQuad', function () {
                    $m.jQuery('.close-faq').fadeOut(300);
                });
            });

            return false;
        });
        // -- END -- Shows and hides the FAQ panel

        //Code for smiley faces on leave feedback page.
        var smilies = $m.jQuery('.client-leave-feedback-smiley');
        var rating = $m.jQuery('input[id$=_hfClientFeedbackRating]');

        smilies.hoverIntent({ //Code for feedback smiley hovering
            timeout: 50,

            over: function () {
                $m.jQuery(this).addClass('selected');
            },

            out: function () {
                var hoveredElem = $m.jQuery(this);

                if (parseInt(rating.val()) !== self.getSmileyValue(hoveredElem)) {
                    hoveredElem.removeClass('selected');
                }
            }
        }).click(function (e) { //Code for feedback smiley selection
            e.preventDefault();
            e.stopPropagation();

            var clickedElem = $m.jQuery(this);

            rating.val(self.getSmileyValue(clickedElem));

            clickedElem.addClass('selected').siblings().removeClass('selected');

            return false;
        });
        // -- END -- Code for feedback smiley selection

        $m.jQuery('.panel > div > table tr').not('.table-header').hoverIntent({
            timeout: 10,

            over: function () {
                $m.jQuery(this).css('background-color', '#cbe5f4');
            },

            out: function () {
                $m.jQuery(this).css('background-color', 'Transparent');
            }
        });
    };

    self.destruct = function () {

    };

    self.getSmileyValue = function (element) {
        if (element.hasClass('client-leave-feedback-smiley-happy')) {
            return 3;
        } else if (element.hasClass('client-leave-feedback-smiley-indifferent')) {
            return 2;
        } else if (element.hasClass('client-leave-feedback-smiley-sad')) {
            return 1;
        }
        return -1;
    };

    return self;
});
// -- END -- Module for the "Support.aspx" page

//----- END Train support modules -----
