/* Mathematica section shared javascript.

   @file        shared.js.en
   @author      marionm
   ========================================================================== */

$(document).ready(function() {

/* fixed page nav
   ========================================================================== */

    var fpn_elem = '.fpn-wrapper';

    // handle floating page nav
    if ($(fpn_elem).length > 0) {
        // floating page nav found
        var fpn_box = '.fpn-wrapper .fpn-box';
        var fpn_box_origin_top = (typeof gl_GetAdjustedOffset === 'function') ? gl_GetAdjustedOffset(fpn_elem) : $(fpn_elem).offset().top;
        var fpn_top_threshold_offset = 102;

        // handle scroll and resize events
        $(window).on('scroll resize', gl_ThrottleFunction(function() {
            var fpn_box_origin_left = $(fpn_elem).offset().left;
            var fpn_scroll_top = $(window).scrollTop();
            var fpn_scroll_left = $(window).scrollLeft();
            if (fpn_scroll_top >= fpn_box_origin_top) {
                // scrolled into fixed nav range, use fixed position
                if (!$(fpn_elem).hasClass('fixed')) $(fpn_elem).addClass('fixed');

                // handle link highlighting
                $('.fpn-link').each(function(i) {
                    // determine which selector to use
                    if ($(this).hasClass('fpn-select')) {
                        // special link detected, verify it's integrity
                        var fpn_selected_id = $(this).text();
                        if (fpn_selected_id && $(fpn_selected_id).length > 0) {
                            // special link, use id as selector
                            var fpn_range_id = fpn_selected_id;
                        } else {
                            // normal link, use href as selector
                            var fpn_range_id = $(this).attr('href');
                        }
                    } else {
                        // normal link, use href as selector
                        var fpn_range_id = $(this).attr('href');
                    }

                    var fpn_header_offset = gl_HeaderFullHeight - gl_HeaderCompactHeight;
                    var fpn_range_top = (typeof gl_GetAdjustedOffset === 'function') ? gl_GetAdjustedOffset(fpn_range_id) - fpn_top_threshold_offset + fpn_header_offset : $(fpn_range_id).offset().top - fpn_top_threshold_offset + fpn_header_offset;
                    var fpn_range_bottom = fpn_range_top + $(fpn_range_id).outerHeight();

                    // adjust the values of the last available range so we can avoid overlapping the global footer
                    if (i == $('.fpn-link').length - 1) {
                        fpn_range_top -= fpn_top_threshold_offset + fpn_header_offset;
                        fpn_range_bottom += fpn_top_threshold_offset + fpn_header_offset;
                    }

                    // do the highlighting
                    if (fpn_range_id && $(fpn_range_id).length > 0 && fpn_scroll_top >= fpn_range_top - 1 && fpn_scroll_top <= fpn_range_bottom - 1) {
                        // current range is associated with this link, highlight it
                        if (!$(this).hasClass('active') || fpn_range_id === $(this).text()) $(this).addClass('active');
                    } else {
                        // current range is not associated with this link, use default styles
                        if ($(this).hasClass('active')) $(this).removeClass('active');
                    }
                }); // link highlighting

                // prevent overlap on the global footer
                if ($(gl_Footer).length > 0) {
                    var fpn_box_bottom = (typeof gl_GetAdjustedOffset === 'function') ? gl_GetAdjustedOffset(fpn_box) : $(fpn_box).offset().top;
                    fpn_box_bottom += $(fpn_box).outerHeight();
                    var fpn_footer_top = (typeof gl_GetAdjustedOffset === 'function') ? gl_GetAdjustedOffset(gl_Footer) : $(gl_Footer).offset().top;
                    fpn_footer_top -= 50; // subtract an illusionary margin above the global footer
                    var fpn_adjusted_box_top = parseInt($(fpn_box).css('top'), 10);

                    if (fpn_box_bottom >= fpn_footer_top || fpn_adjusted_box_top < gl_HeaderOffset) {
                        // overlap detected, compensate by reducing the 'top' property
                        fpn_adjusted_box_top -= fpn_box_bottom - fpn_footer_top;
                        $('.fpn-link').not('.fpn-select').last().addClass('active');
                    } else {
                        // no overlap detected, preserve default global header offset
                        fpn_adjusted_box_top = gl_HeaderOffset;
                    }

                    // set top and left positions
                    $(fpn_box).css('top', fpn_adjusted_box_top);
                    $(fpn_box).css('left', fpn_box_origin_left);
                    // set left position
                    if (fpn_scroll_left > 0) {
                        $(fpn_box).css('margin-left', 0 - fpn_scroll_left + 'px');
                    } else {
                        $(fpn_box).css('margin-left', '');
                    }
                } // footer overlap prevention
            } else {
                // scrolled out of fixed nav range, use relative position
                $(fpn_elem).removeClass('fixed');
                // reset css attributes
                $(fpn_box).css('top', '').css('left', '').css('margin-left', '');
                // handle link highlighting
                $('.fpn-link').each(function(i) {
                    if (i == 0) {
                        // highlight first link in the list
                        if (!$(this).hasClass('active')) $(this).addClass('active');
                    } else {
                        // deactivate highlighting on all the others
                        $(this).removeClass('active');
                    }
                });
            } // fixed nav range
        })); // scroll and resize events

        // handle hash links
        $('.fpn-wrapper a[href*=#]').on('click', function(e) {
            e.preventDefault();

            // search for identifiers on the page, quit if not found
            var fpn_id = this.hash;
            if ($(fpn_id).length == 0) return;

            // insert a dummy span in front of the fpn_element so we always get an accurate offset
            $(fpn_id).before('<span class="gl-anchor"></span>');
            // get dummy span offset
            var fpn_top = $('.gl-anchor').offset().top;
            // remove the dummy span now that we have the offset
            $('.gl-anchor').remove();

            // scroll to the new offset
            $('html, body').animate({scrollTop: fpn_top}, 500);
        }); // hash links

    } // floating page nav

    // expand collapse
    $('.expanded-features').addClass('hide');
    $('.allfeatures').click(function() {
        $(this).parents('.pricing-row').next('.features').next('.expanded-features').toggleClass('show').toggleClass('hide');
    });
});