/* Minification failed. Returning unminified contents.
(1150,84-85): run-time error JS1195: Expected expression: >
(1152,14-15): run-time error JS1195: Expected expression: )
 */
/*!
Waypoints - 4.0.1
Copyright © 2011-2016 Caleb Troughton
Licensed under the MIT license.
https://github.com/imakewebthings/waypoints/blob/master/licenses.txt
*/
(function () {
    'use strict'

    var keyCounter = 0
    var allWaypoints = {}

    /* http://imakewebthings.com/waypoints/api/waypoint */
    function Waypoint(options) {
        if (!options) {
            throw new Error('No options passed to Waypoint constructor')
        }
        if (!options.element) {
            throw new Error('No element option passed to Waypoint constructor')
        }
        if (!options.handler) {
            throw new Error('No handler option passed to Waypoint constructor')
        }

        this.key = 'waypoint-' + keyCounter
        this.options = Waypoint.Adapter.extend({}, Waypoint.defaults, options)
        this.element = this.options.element
        this.adapter = new Waypoint.Adapter(this.element)
        this.callback = options.handler
        this.axis = this.options.horizontal ? 'horizontal' : 'vertical'
        this.enabled = this.options.enabled
        this.triggerPoint = null
        this.group = Waypoint.Group.findOrCreate({
            name: this.options.group,
            axis: this.axis
        })
        this.context = Waypoint.Context.findOrCreateByElement(this.options.context)

        if (Waypoint.offsetAliases[this.options.offset]) {
            this.options.offset = Waypoint.offsetAliases[this.options.offset]
        }
        this.group.add(this)
        this.context.add(this)
        allWaypoints[this.key] = this
        keyCounter += 1
    }

    /* Private */
    Waypoint.prototype.queueTrigger = function (direction) {
        this.group.queueTrigger(this, direction)
    }

    /* Private */
    Waypoint.prototype.trigger = function (args) {
        if (!this.enabled) {
            return
        }
        if (this.callback) {
            this.callback.apply(this, args)
        }
    }

    /* Public */
    /* http://imakewebthings.com/waypoints/api/destroy */
    Waypoint.prototype.destroy = function () {
        this.context.remove(this)
        this.group.remove(this)
        delete allWaypoints[this.key]
    }

    /* Public */
    /* http://imakewebthings.com/waypoints/api/disable */
    Waypoint.prototype.disable = function () {
        this.enabled = false
        return this
    }

    /* Public */
    /* http://imakewebthings.com/waypoints/api/enable */
    Waypoint.prototype.enable = function () {
        this.context.refresh()
        this.enabled = true
        return this
    }

    /* Public */
    /* http://imakewebthings.com/waypoints/api/next */
    Waypoint.prototype.next = function () {
        return this.group.next(this)
    }

    /* Public */
    /* http://imakewebthings.com/waypoints/api/previous */
    Waypoint.prototype.previous = function () {
        return this.group.previous(this)
    }

    /* Private */
    Waypoint.invokeAll = function (method) {
        var allWaypointsArray = []
        for (var waypointKey in allWaypoints) {
            allWaypointsArray.push(allWaypoints[waypointKey])
        }
        for (var i = 0, end = allWaypointsArray.length; i < end; i++) {
            allWaypointsArray[i][method]()
        }
    }

    /* Public */
    /* http://imakewebthings.com/waypoints/api/destroy-all */
    Waypoint.destroyAll = function () {
        Waypoint.invokeAll('destroy')
    }

    /* Public */
    /* http://imakewebthings.com/waypoints/api/disable-all */
    Waypoint.disableAll = function () {
        Waypoint.invokeAll('disable')
    }

    /* Public */
    /* http://imakewebthings.com/waypoints/api/enable-all */
    Waypoint.enableAll = function () {
        Waypoint.Context.refreshAll()
        for (var waypointKey in allWaypoints) {
            allWaypoints[waypointKey].enabled = true
        }
        return this
    }

    /* Public */
    /* http://imakewebthings.com/waypoints/api/refresh-all */
    Waypoint.refreshAll = function () {
        Waypoint.Context.refreshAll()
    }

    /* Public */
    /* http://imakewebthings.com/waypoints/api/viewport-height */
    Waypoint.viewportHeight = function () {
        return window.innerHeight || document.documentElement.clientHeight
    }

    /* Public */
    /* http://imakewebthings.com/waypoints/api/viewport-width */
    Waypoint.viewportWidth = function () {
        return document.documentElement.clientWidth
    }

    Waypoint.adapters = []

    Waypoint.defaults = {
        context: window,
        continuous: true,
        enabled: true,
        group: 'default',
        horizontal: false,
        offset: 0
    }

    Waypoint.offsetAliases = {
        'bottom-in-view': function () {
            return this.context.innerHeight() - this.adapter.outerHeight()
        },
        'right-in-view': function () {
            return this.context.innerWidth() - this.adapter.outerWidth()
        }
    }

    window.Waypoint = Waypoint
}())
    ; (function () {
        'use strict'

        function requestAnimationFrameShim(callback) {
            window.setTimeout(callback, 1000 / 60)
        }

        var keyCounter = 0
        var contexts = {}
        var Waypoint = window.Waypoint
        var oldWindowLoad = window.onload

        /* http://imakewebthings.com/waypoints/api/context */
        function Context(element) {
            this.element = element
            this.Adapter = Waypoint.Adapter
            this.adapter = new this.Adapter(element)
            this.key = 'waypoint-context-' + keyCounter
            this.didScroll = false
            this.didResize = false
            this.oldScroll = {
                x: this.adapter.scrollLeft(),
                y: this.adapter.scrollTop()
            }
            this.waypoints = {
                vertical: {},
                horizontal: {}
            }

            element.waypointContextKey = this.key
            contexts[element.waypointContextKey] = this
            keyCounter += 1
            if (!Waypoint.windowContext) {
                Waypoint.windowContext = true
                Waypoint.windowContext = new Context(window)
            }

            this.createThrottledScrollHandler()
            this.createThrottledResizeHandler()
        }

        /* Private */
        Context.prototype.add = function (waypoint) {
            var axis = waypoint.options.horizontal ? 'horizontal' : 'vertical'
            this.waypoints[axis][waypoint.key] = waypoint
            this.refresh()
        }

        /* Private */
        Context.prototype.checkEmpty = function () {
            var horizontalEmpty = this.Adapter.isEmptyObject(this.waypoints.horizontal)
            var verticalEmpty = this.Adapter.isEmptyObject(this.waypoints.vertical)
            var isWindow = this.element == this.element.window
            if (horizontalEmpty && verticalEmpty && !isWindow) {
                this.adapter.off('.waypoints')
                delete contexts[this.key]
            }
        }

        /* Private */
        Context.prototype.createThrottledResizeHandler = function () {
            var self = this

            function resizeHandler() {
                self.handleResize()
                self.didResize = false
            }

            this.adapter.on('resize.waypoints', function () {
                if (!self.didResize) {
                    self.didResize = true
                    Waypoint.requestAnimationFrame(resizeHandler)
                }
            })
        }

        /* Private */
        Context.prototype.createThrottledScrollHandler = function () {
            var self = this
            function scrollHandler() {
                self.handleScroll()
                self.didScroll = false
            }

            this.adapter.on('scroll.waypoints', function () {
                if (!self.didScroll || Waypoint.isTouch) {
                    self.didScroll = true
                    Waypoint.requestAnimationFrame(scrollHandler)
                }
            })
        }

        /* Private */
        Context.prototype.handleResize = function () {
            Waypoint.Context.refreshAll()
        }

        /* Private */
        Context.prototype.handleScroll = function () {
            var triggeredGroups = {}
            var axes = {
                horizontal: {
                    newScroll: this.adapter.scrollLeft(),
                    oldScroll: this.oldScroll.x,
                    forward: 'right',
                    backward: 'left'
                },
                vertical: {
                    newScroll: this.adapter.scrollTop(),
                    oldScroll: this.oldScroll.y,
                    forward: 'down',
                    backward: 'up'
                }
            }

            for (var axisKey in axes) {
                var axis = axes[axisKey]
                var isForward = axis.newScroll > axis.oldScroll
                var direction = isForward ? axis.forward : axis.backward

                for (var waypointKey in this.waypoints[axisKey]) {
                    var waypoint = this.waypoints[axisKey][waypointKey]
                    if (waypoint.triggerPoint === null) {
                        continue
                    }
                    var wasBeforeTriggerPoint = axis.oldScroll < waypoint.triggerPoint
                    var nowAfterTriggerPoint = axis.newScroll >= waypoint.triggerPoint
                    var crossedForward = wasBeforeTriggerPoint && nowAfterTriggerPoint
                    var crossedBackward = !wasBeforeTriggerPoint && !nowAfterTriggerPoint
                    if (crossedForward || crossedBackward) {
                        waypoint.queueTrigger(direction)
                        triggeredGroups[waypoint.group.id] = waypoint.group
                    }
                }
            }

            for (var groupKey in triggeredGroups) {
                triggeredGroups[groupKey].flushTriggers()
            }

            this.oldScroll = {
                x: axes.horizontal.newScroll,
                y: axes.vertical.newScroll
            }
        }

        /* Private */
        Context.prototype.innerHeight = function () {
            /*eslint-disable eqeqeq */
            if (this.element == this.element.window) {
                return Waypoint.viewportHeight()
            }
            /*eslint-enable eqeqeq */
            return this.adapter.innerHeight()
        }

        /* Private */
        Context.prototype.remove = function (waypoint) {
            delete this.waypoints[waypoint.axis][waypoint.key]
            this.checkEmpty()
        }

        /* Private */
        Context.prototype.innerWidth = function () {
            /*eslint-disable eqeqeq */
            if (this.element == this.element.window) {
                return Waypoint.viewportWidth()
            }
            /*eslint-enable eqeqeq */
            return this.adapter.innerWidth()
        }

        /* Public */
        /* http://imakewebthings.com/waypoints/api/context-destroy */
        Context.prototype.destroy = function () {
            var allWaypoints = []
            for (var axis in this.waypoints) {
                for (var waypointKey in this.waypoints[axis]) {
                    allWaypoints.push(this.waypoints[axis][waypointKey])
                }
            }
            for (var i = 0, end = allWaypoints.length; i < end; i++) {
                allWaypoints[i].destroy()
            }
        }

        /* Public */
        /* http://imakewebthings.com/waypoints/api/context-refresh */
        Context.prototype.refresh = function () {
            /*eslint-disable eqeqeq */
            var isWindow = this.element == this.element.window
            /*eslint-enable eqeqeq */
            var contextOffset = isWindow ? undefined : this.adapter.offset()
            var triggeredGroups = {}
            var axes

            this.handleScroll()
            axes = {
                horizontal: {
                    contextOffset: isWindow ? 0 : contextOffset.left,
                    contextScroll: isWindow ? 0 : this.oldScroll.x,
                    contextDimension: this.innerWidth(),
                    oldScroll: this.oldScroll.x,
                    forward: 'right',
                    backward: 'left',
                    offsetProp: 'left'
                },
                vertical: {
                    contextOffset: isWindow ? 0 : contextOffset.top,
                    contextScroll: isWindow ? 0 : this.oldScroll.y,
                    contextDimension: this.innerHeight(),
                    oldScroll: this.oldScroll.y,
                    forward: 'down',
                    backward: 'up',
                    offsetProp: 'top'
                }
            }

            for (var axisKey in axes) {
                var axis = axes[axisKey]
                for (var waypointKey in this.waypoints[axisKey]) {
                    var waypoint = this.waypoints[axisKey][waypointKey]
                    var adjustment = waypoint.options.offset
                    var oldTriggerPoint = waypoint.triggerPoint
                    var elementOffset = 0
                    var freshWaypoint = oldTriggerPoint == null
                    var contextModifier, wasBeforeScroll, nowAfterScroll
                    var triggeredBackward, triggeredForward

                    if (waypoint.element !== waypoint.element.window) {
                        elementOffset = waypoint.adapter.offset()[axis.offsetProp]
                    }

                    if (typeof adjustment === 'function') {
                        adjustment = adjustment.apply(waypoint)
                    }
                    else if (typeof adjustment === 'string') {
                        adjustment = parseFloat(adjustment)
                        if (waypoint.options.offset.indexOf('%') > - 1) {
                            adjustment = Math.ceil(axis.contextDimension * adjustment / 100)
                        }
                    }

                    contextModifier = axis.contextScroll - axis.contextOffset
                    waypoint.triggerPoint = Math.floor(elementOffset + contextModifier - adjustment)
                    wasBeforeScroll = oldTriggerPoint < axis.oldScroll
                    nowAfterScroll = waypoint.triggerPoint >= axis.oldScroll
                    triggeredBackward = wasBeforeScroll && nowAfterScroll
                    triggeredForward = !wasBeforeScroll && !nowAfterScroll

                    if (!freshWaypoint && triggeredBackward) {
                        waypoint.queueTrigger(axis.backward)
                        triggeredGroups[waypoint.group.id] = waypoint.group
                    }
                    else if (!freshWaypoint && triggeredForward) {
                        waypoint.queueTrigger(axis.forward)
                        triggeredGroups[waypoint.group.id] = waypoint.group
                    }
                    else if (freshWaypoint && axis.oldScroll >= waypoint.triggerPoint) {
                        waypoint.queueTrigger(axis.forward)
                        triggeredGroups[waypoint.group.id] = waypoint.group
                    }
                }
            }

            Waypoint.requestAnimationFrame(function () {
                for (var groupKey in triggeredGroups) {
                    triggeredGroups[groupKey].flushTriggers()
                }
            })

            return this
        }

        /* Private */
        Context.findOrCreateByElement = function (element) {
            return Context.findByElement(element) || new Context(element)
        }

        /* Private */
        Context.refreshAll = function () {
            for (var contextId in contexts) {
                contexts[contextId].refresh()
            }
        }

        /* Public */
        /* http://imakewebthings.com/waypoints/api/context-find-by-element */
        Context.findByElement = function (element) {
            return contexts[element.waypointContextKey]
        }

        window.onload = function () {
            if (oldWindowLoad) {
                oldWindowLoad()
            }
            Context.refreshAll()
        }


        Waypoint.requestAnimationFrame = function (callback) {
            var requestFn = window.requestAnimationFrame ||
                window.mozRequestAnimationFrame ||
                window.webkitRequestAnimationFrame ||
                requestAnimationFrameShim
            requestFn.call(window, callback)
        }
        Waypoint.Context = Context
    }())
    ; (function () {
        'use strict'

        function byTriggerPoint(a, b) {
            return a.triggerPoint - b.triggerPoint
        }

        function byReverseTriggerPoint(a, b) {
            return b.triggerPoint - a.triggerPoint
        }

        var groups = {
            vertical: {},
            horizontal: {}
        }
        var Waypoint = window.Waypoint

        /* http://imakewebthings.com/waypoints/api/group */
        function Group(options) {
            this.name = options.name
            this.axis = options.axis
            this.id = this.name + '-' + this.axis
            this.waypoints = []
            this.clearTriggerQueues()
            groups[this.axis][this.name] = this
        }

        /* Private */
        Group.prototype.add = function (waypoint) {
            this.waypoints.push(waypoint)
        }

        /* Private */
        Group.prototype.clearTriggerQueues = function () {
            this.triggerQueues = {
                up: [],
                down: [],
                left: [],
                right: []
            }
        }

        /* Private */
        Group.prototype.flushTriggers = function () {
            for (var direction in this.triggerQueues) {
                var waypoints = this.triggerQueues[direction]
                var reverse = direction === 'up' || direction === 'left'
                waypoints.sort(reverse ? byReverseTriggerPoint : byTriggerPoint)
                for (var i = 0, end = waypoints.length; i < end; i += 1) {
                    var waypoint = waypoints[i]
                    if (waypoint.options.continuous || i === waypoints.length - 1) {
                        waypoint.trigger([direction])
                    }
                }
            }
            this.clearTriggerQueues()
        }

        /* Private */
        Group.prototype.next = function (waypoint) {
            this.waypoints.sort(byTriggerPoint)
            var index = Waypoint.Adapter.inArray(waypoint, this.waypoints)
            var isLast = index === this.waypoints.length - 1
            return isLast ? null : this.waypoints[index + 1]
        }

        /* Private */
        Group.prototype.previous = function (waypoint) {
            this.waypoints.sort(byTriggerPoint)
            var index = Waypoint.Adapter.inArray(waypoint, this.waypoints)
            return index ? this.waypoints[index - 1] : null
        }

        /* Private */
        Group.prototype.queueTrigger = function (waypoint, direction) {
            this.triggerQueues[direction].push(waypoint)
        }

        /* Private */
        Group.prototype.remove = function (waypoint) {
            var index = Waypoint.Adapter.inArray(waypoint, this.waypoints)
            if (index > -1) {
                this.waypoints.splice(index, 1)
            }
        }

        /* Public */
        /* http://imakewebthings.com/waypoints/api/first */
        Group.prototype.first = function () {
            return this.waypoints[0]
        }

        /* Public */
        /* http://imakewebthings.com/waypoints/api/last */
        Group.prototype.last = function () {
            return this.waypoints[this.waypoints.length - 1]
        }

        /* Private */
        Group.findOrCreate = function (options) {
            return groups[options.axis][options.name] || new Group(options)
        }

        Waypoint.Group = Group
    }())
    ; (function () {
        'use strict'

        var $ = window.jQuery
        var Waypoint = window.Waypoint

        function JQueryAdapter(element) {
            this.$element = $(element)
        }

        $.each([
            'innerHeight',
            'innerWidth',
            'off',
            'offset',
            'on',
            'outerHeight',
            'outerWidth',
            'scrollLeft',
            'scrollTop'
        ], function (i, method) {
            JQueryAdapter.prototype[method] = function () {
                var args = Array.prototype.slice.call(arguments)
                return this.$element[method].apply(this.$element, args)
            }
        })

        $.each([
            'extend',
            'inArray',
            'isEmptyObject'
        ], function (i, method) {
            JQueryAdapter[method] = $[method]
        })

        Waypoint.adapters.push({
            name: 'jquery',
            Adapter: JQueryAdapter
        })
        Waypoint.Adapter = JQueryAdapter
    }())
    ; (function () {
        'use strict'

        var Waypoint = window.Waypoint

        function createExtension(framework) {
            return function () {
                var waypoints = []
                var overrides = arguments[0]

                if (framework.isFunction(arguments[0])) {
                    overrides = framework.extend({}, arguments[1])
                    overrides.handler = arguments[0]
                }

                this.each(function () {
                    var options = framework.extend({}, overrides, {
                        element: this
                    })
                    if (typeof options.context === 'string') {
                        options.context = framework(this).closest(options.context)[0]
                    }
                    waypoints.push(new Waypoint(options))
                })

                return waypoints
            }
        }

        if (window.jQuery) {
            window.jQuery.fn.waypoint = createExtension(window.jQuery)
        }
        if (window.Zepto) {
            window.Zepto.fn.waypoint = createExtension(window.Zepto)
        }
    }())
    ;;

function isRightBarStickyNeeded() {
    return $(window).width() > 992;
}

function makeStaticRightBarSticky() {

    if (isRightBarStickyNeeded()) {
        $('.right-bar-column').each(function (i, obj) {
            var $rightBarColumn = $(this);
            var $rightBarSection = $rightBarColumn.find('.right-bar-section').last();
            var windowHeight = $(window).height();
            var rightbarCount = $('.right-bar-column').length;

            if ($rightBarColumn && $rightBarSection) {
                //Set column height to match the left container, default to parent container height. This sets the boundaries for the section inside.
                var $itemHeightToMatch = $rightBarColumn.parent().find('.col-md-8').first() ? $rightBarColumn.parent().find('.col-md-8').first() : $rightBarColumn.parent();
              
                var rightColumnHeight = $itemHeightToMatch.outerHeight();
                var rightSectionHeight = $rightBarSection.outerHeight();
                $rightBarColumn.addClass('rightbar-is-sticky');
                //Defaults
                var topOffset = windowHeight - rightSectionHeight;
                var bottomOffset = 'bottom-in-view';

                //Don't do sticky if there' s no room for it or if the column is smaller than the window to prevent awkward overlapping
                if (rightSectionHeight + 150 >= rightColumnHeight || rightColumnHeight < windowHeight){
                    return true;
                }

                $rightBarColumn.height(rightColumnHeight);

                // search pages have an extra filter widget that isn't sticky; need to remove the height when calculating sticky-able height
                $searchFilter = $rightBarColumn.find('.mobile-minitablet-smdesktop-hide-element');
                if ($searchFilter.length) {
                    topOffset -= $searchFilter.last().height();
                }

                //to avoid overlapping widgets and dividers, make sure the previous/next div is not a horizontal widget/divider
                var $parentPost = $rightBarColumn.parents('.posts');
                var div = $parentPost.prevAll('div');
                var nextDiv = $parentPost.nextAll('div');
                var hasDividerPrev = $(div[0]).hasClass('horizontal-widget') || $(div[0]).hasClass('more-posts-divider');
                var hasDividerNext = $(nextDiv[0]).hasClass('horizontal-widget') || $(nextDiv[0]).hasClass('more-posts-divider') ||( $parentPost.next('.horizontal-widget').length);
                
                //hides whatever whitespaces is in the rightbarsection but isn't visible
                if (hasDividerNext || i == rightbarCount - 1)
                    $rightBarSection.css("background", "transparent");

                var waypointTop = new Waypoint({
                    element: $rightBarColumn,
                    handler: function (direction) {
                        //During scroll down towards bottom of page, when bottom of right bar section becomes visible in window make it stick (fixed) to the bottom of the screen.
                        if (direction == "down") {
                            $rightBarSection.removeClass('unstuck').addClass('stuck');
                        }
                        //During scroll up towards top of page, when top of right bar column becomes visible in window remove all sticky styles so the section goes back to its original placement.
                        else if (direction == "up") {
                            $rightBarSection.removeClass('stuck').removeClass('unstuck');
                        }
                    },
                    offset: topOffset
                });
      
                var waypointBottom= new Waypoint({
                    element: $rightBarColumn,
                    handler: function (direction) {
                        //During scroll down towards bottom of page, when bottom of right bar column becomes visible in window unstick the section so it scrolls with the page.
                        if (direction == "down") {
                            $rightBarSection.removeClass('stuck').addClass('unstuck');
                        }
                        //During scroll up towards top of page, when bottom of right bar column becomes visible in window make the section stick (fixed) to the bottom of the screen.
                        else if (direction == "up") {
                            $rightBarSection.removeClass('unstuck').addClass('stuck');
                        }
                    },
                    offset: bottomOffset
                })
             
                }
        });
    }
}

function trackTopAdHeight() {
    if (!isRightBarStickyNeeded())
        return;

    //Recalculates trigger points when top02 ad expands/contracts
    var observer = new MutationObserver(function (mutations) {
        for (var i = 0; i < mutations.length; i++) {
            var mutation = mutations[i];
            if (mutation.type === 'attributes' && mutation.attributeName === "style") {
                Waypoint.refreshAll();
            }
        }
    });

    var oopTop02 = document.getElementById('div-oop-gpt-ad-top02');
    if (oopTop02 != null) {
        observer.observe(oopTop02, {
            attributes: true,
            childList: true,
            subtree: true
        });
    }
}

function trackFlexiblelistposition(){
   var observerFlex = new MutationObserver(function (mutations) {
        mutations.forEach(function (mutation) {
            [].filter.call(mutation.addedNodes, function (node) {
                return node.nodeName === 'video';
            }).forEach(function (node) {
                console.log('video added');
                node.addEventListener('loaddata', function (e) {
                    RemoveSticky();
                    try {
                        makeStaticRightBarSticky();
                    } catch (e) {
                        console.warn(e)
                    }
                });
            });
        });
    });

    var oopFlex = document.getElementById('div-oop-gpt-ad-flexiblelistposition');
    if (oopFlex !== null) {
        observerFlex.observe(oopFlex, {
            childList: true,
            subtree: true
        });
    }
}

function trackPrestitialScroll() {
    if (!isRightBarStickyNeeded())
        return;

    var oopPrestitial = document.getElementById('prestitialWrapper');
    if (oopPrestitial != null) {
        //Recalculates trigger points when user leaves the prestitial
        $('#prestitialWrapper').mouseleave(function () {
            Waypoint.refreshAll();
        })
    }
}

//This is called in layout on slotRenderEnded event
function RecreateRightBar() {
    if (isRightBarStickyNeeded()) {
        if ($('.right-bar-column').first().hasClass('rightbar-is-sticky')) {
            console.log("RightBar recreated after ads load");
            RemoveSticky();
            try {
                makeStaticRightBarSticky();
            } catch (e) {
                console.warn(e)
            }
        }
    }
}

function RemoveSticky() {
    $('.right-bar-column').each(function (i, obj) {
        $(this).height('auto');
        $(this).removeClass('rightbar-is-sticky');
    });
    $('.right-bar-section.stuck').removeClass('stuck');
    $('.right-bar-section.unstuck').removeClass('unstuck');
    Waypoint.destroyAll();
}

function externalLinksNewTab() {
    console.log('external links');
    // Get the domain of the current page
    var domain = window.location.hostname;

    // Select all <a> tags
    var links = document.getElementsByTagName("a");

    // Loop over all links
    for (var i = 0; i < links.length; i++) {
        var link = links[i];

        // Get the host of the link
        var linkHost = link.hostname;
        // If the host of the link does not match the host of the current page or "localhost", add target="_blank"
        if (linkHost && linkHost !== "localhost" && linkHost !== domain ) {
            link.target = "_blank";
        }
    }

}

$(window).on('load', function () {
    //Make sticky after all ads have loaded to ensure height calculations are correct
    //Right bar calculations will be updated on last ads iframe load events
    try {
        makeStaticRightBarSticky();
    } catch (e) {
        console.warn(e)
    }
    //Recalculate trigger points when oop ads affects height
    trackTopAdHeight();
    trackPrestitialScroll();
    trackFlexiblelistposition();
    externalLinksNewTab(); 
    console.log("RightBar created on LOAD");
});

//Detect window resize and disable sticky if needed
$(window).resize(function () {
    if (isRightBarStickyNeeded()) {
        if ($('.right-bar-column').first().hasClass('rightbar-is-sticky')) {
            Waypoint.refreshAll(); //Recalculate trigger points on window resize. Window height may have changed.
        }
        else { // recreate it again
            try {
                makeStaticRightBarSticky();
            } catch (e) {
                console.warn(e)
            }
        }
    }
    else {
        if ($('.right-bar-column').first().hasClass('rightbar-is-sticky')) {
            RemoveSticky() //No right sidebar. Remove existing waypoints to avoid floating sidebars out of place.
        }
    }
});
;

function rightBarStickyNeeded() {
    return $(window).width() > 992;
}

function loadMorePosts(e) {
    e.stopImmediatePropagation();
    e.preventDefault;

    //Ignore loadmore while it is loading...
    if ($(this).attr('disabled') == 'disabled')
        return false;

    //removes focus from the button so it can append correctly
    $(this).blur();

    var $btn = $(this);
    var $postsList = $('.posts').last();//in case of multiple posts classs, use the last one on the page.
    var newPageUrl = $btn.attr('href');
    var pageNumber = $btn.data('pageNumber');
    var pageParam = $btn.data('pageParam');

    var getUrl = $btn.data('ajaxPath');
    if (!getUrl) {
        //when there's no getUrl it should clickthrough to href
        return true;
    }
    else {
        e.stopImmediatePropagation();
        e.preventDefault;

        $.ajax({
            type: 'GET',
            cache: false,
            url: getUrl,
            dataType: 'html',
            beforeSend: function () {
                $btn.attr('disabled', 'disabled');
                $btn.text('Loading...');
            },
            success: function (data) {
                if ($.trim(data) != '') {   //because we're getting back HTML, we have to make sure the result is NOT empty

                    //display new posts, rightbar section, and banner ad
                    $postsList.append(data);

                    findAndLoadDynamicAds(data);

                    makeLastRightBarSectionSticky();

                    // update load more button
                    var nextPageNumber = pageNumber + 1;
                    $btn.data('pageNumber', nextPageNumber);
                    $btn.data('pageParam', pageParam);
                    //var nextPageUrl = newPageUrl.replace('page=' + pageNumber, 'page=' + nextPageNumber);
                    var nextPageUrl = newPageUrl.replace(pageParam + '=' + pageNumber, pageParam + '=' + nextPageNumber);
                    $btn.attr('href', nextPageUrl);

                    var nextPageAjaxPath = getUrl.replace(pageParam + '=' + pageNumber, pageParam + '=' + nextPageNumber);
                    $btn.data('ajaxPath', nextPageAjaxPath);

                    // update banner ad goes here (maybe?)

                    observeLozad();

                    // update page url
                    if (history.pushState) {
                        //window.history.pushState({ page: pageNumber }, document.title, newPageUrl);
                        switch (pageParam) {
                            case 'page': ;
                                window.history.pushState({ page: pageNumber }, document.title, newPageUrl);
                                break;
                            case 'pagenum':
                                window.history.pushState({ pagenum: pageNumber }, document.title, newPageUrl);
                                break;
                        }
                    }

                } else {
                    $btn.parent().hide(); // this hides the load more button when we have no more items to display
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert('jqXHR: ' + jqXHR + ', Status: ' + textStatus + ', Error: '+ errorThrown);
                //alert('There was an error loading more.');
            },
            complete: function () {
                $btn.removeAttr('disabled');
                $btn.text('Load More');
            }
        });
        return false;
    }

}

window.loadMorePostsFirstPage = function (pageUrl, loadingMessage) {
    var $container = $('#postlist-container').last();//in case of multiple posts classs, use the last one on the page.

    var getUrl = pageUrl;
    var loadingMsg = loadingMessage ? loadingMessage : 'Loading more stories...';

    $.ajax({
        type: 'GET',
        cache: false,
        url: getUrl,
        dataType: 'html',
        beforeSend: function () {
            $box = $("<div/>").addClass("loading p-14-gray text-center margin-bottom-10");
            $box.append($("<p/>").text(loadingMsg));
            $box.append($("<p/>").append($("<img/>").attr("src", "/content/img/spinner-1s-81px.gif")));
            $container.append($box);
        },
        success: function (data) {
            if ($.trim(data) != '') {   //because we're getting back HTML, we have to make sure the result is NOT empty

                //display new posts, rightbar section, and banner ad
                $container.append(data);

                findAndLoadDynamicAds(data);

                makeLastRightBarSectionSticky();

                observeLozad();

            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            //alert('jqXHR: ' + jqXHR + ', Status: ' + textStatus + ', Error: '+ errorThrown);
            //alert('There was an error loading more.');
        },
        complete: function () {
            $container.find(".loading").remove();
        }
    });
    return false;
};

window.loadContentOffers = function (pageUrl) {
    var $container = $('#contentoffers-container').last();

    var getUrl = pageUrl;

    $.ajax({
        type: 'GET',
        cache: false,
        url: getUrl,
        dataType: 'html',
        beforeSend: function () {
            $box = $("<div/>").addClass("loading p-14-gray text-center margin-bottom-10");
            $box.append($("<p/>").text("Loading related content..."));
            $box.append($("<p/>").append($("<img/>").attr("src", "/content/img/spinner-1s-81px.gif")));
            $container.append($box);
        },
        success: function (data) {
            if ($.trim(data) != '') {   //because we're getting back HTML, we have to make sure the result is NOT empty

                //display new posts, rightbar section, and banner ad
                $container.append(data);

                observeLozad();

            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            //alert('jqXHR: ' + jqXHR + ', Status: ' + textStatus + ', Error: '+ errorThrown);
            //alert('There was an error loading more.');
        },
        complete: function () {
            $container.find(".loading").remove();
        }
    });
    return false;
};


window.loadSolutionProviders = function (pageUrl) {
    var $container = $('.article-img').first();

    var getUrl = pageUrl;
    if ($container.length == 0) return;

    $.ajax({
        type: 'GET',
        startTime: performance.now(),
        cache: false,
        url: getUrl,
        dataType: 'html',
        beforeSend: function () {
           // $box = $("<div/>").addClass("loading loading-inline text-center margin-bottom-10");
           // $box.append($("<p/>").html("Loading content...").append($("<img/>").attr("src", "/content/img/spinner-1s-81px.gif")));
           // $container.append($box);
        },
        success: function (data) {
            if ($.trim(data) != '') {   //because we're getting back HTML, we have to make sure the result is NOT empty

                //display new posts, rightbar section, and banner ad
              $container.append(data);
            }

            //Calculate the difference in milliseconds.
            var time = performance.now() - this.startTime;
            //Convert milliseconds to seconds.
            var seconds = time / 1000;
            //Round to 3 decimal places.
            seconds = seconds.toFixed(3);
            //Write the result to the HTML document.
            var result = 'AJAX request took ' + seconds + ' seconds to complete.';
            //log it to the console.
            console.log(result);
        },
        error: function (jqXHR, textStatus, errorThrown) {
            //alert('jqXHR: ' + jqXHR + ', Status: ' + textStatus + ', Error: '+ errorThrown);
            //alert('There was an error loading more.');
        },
        complete: function () {
           // $container.find(".loading").remove();
        }
    });
    return false;
};


function makeLastRightBarSectionSticky() {
    //Sticky right bar is only for wide screen
    if (!rightBarStickyNeeded())
        return;
    
    //Grab the last column and section on the page.
    var $rightBarColumn = $('.posts').last().find('.right-bar-column').last();
    var $rightBarSection = $rightBarColumn.find('.right-bar-section').last();

    if ($rightBarColumn && $rightBarSection) {
        //Set column height to match the parent container height. This sets the boundaries for the section inside.
        //For the images that affect the height of the total list we need default height set on for sticky right bar height calculation because getting the section height usually happens before all images load in a list.
        // .article-thumbnail img (in main.css)
        $rightBarColumn.addClass('rightbar-is-sticky');
        var parentHeight = $rightBarColumn.parent().outerHeight();

        var columnHeight = parentHeight;
        var sectionHeight = $rightBarSection.outerHeight();
        var windowHeight = $(window).height();

        //Don't do sticky if there's no room for it.
        if (sectionHeight >= columnHeight || columnHeight < windowHeight)
            return;

        $rightBarColumn.height(parentHeight);      
    }
}

function defineAndRefreshDynamicPosition(slotName, slotSizes, slotDivId) {
    if (slotSizes === "") {
        if (googletag && window.constant.dfpAdConfig.dynamicPositions) {
            const result = window.constant.dfpAdConfig.dynamicPositions.filter(el => {
                return el['name'] === slotName;
            });
            if (result && $.isArray(result) && result.length > 0) {
                slotSizes = result[0].sizes;
            }
        }
    }
    if (slotName && slotSizes && slotDivId) {
        googletag.cmd.push(function () {
            var dynamicSlot = googletag.defineSlot('/' + window.constant.dfpAccountId + '/' + slotName, slotSizes, slotDivId).addService(googletag.pubads());
            googletag.display(slotDivId);
            googletag.pubads().refresh([dynamicSlot]);
        });
    }
}

function findAndLoadDynamicAds(data) {
    $data = $(data);
    $dynamicAds = $data.find("." + window.constant.dfpDynamicAdCssClass);
    $dynamicAds.each(function (index) {
        var name = $(this).data("dfpPositionName");
        var sizes = $(this).data("dfpPositionSizes");
        var divId = $(this).attr("id");
        defineAndRefreshDynamicPosition(name, sizes, divId);
    });
}

$(function () {
    $(document).on('click', '.posts-load-more', loadMorePosts);
});;
