/**
 * @version     $Id$
 * @author      JoomlaUX
 * @package     Joomla.Site
 * @subpackage  mod_jux_easy_megamenu
 * @copyright   Copyright (C) 2008 - 2018 JoomlaUX. All rights reserved.
 * @license     License GNU General Public License version 2 or later; see LICENSE.txt, see LICENSE.php
 */
(function($) {
    var $window = $(window),
        $stickies;

    function sticky_menu(stickies) {
        if (typeof stickies === "object" && stickies instanceof jQuery && stickies.length > 0) {
            $stickies = stickies.each(function(idx) {
                if (!$(this).hasClass('jux-no-sticky')) {
                    var $thisSticky = $(this);
                    if ($window.width() >= $thisSticky.prev().data('breakpoint')) {
                        $thisSticky
                            .attr('data-originalPosition', $thisSticky.parent().offset().top)
                            .attr('data-originalHeight', $thisSticky.outerHeight())
                            .parent()
                            .height($thisSticky.outerHeight());
                        var $stickyPosition = $thisSticky.attr('data-originalPosition');
                        if ($stickyPosition <= $window.scrollTop()) {
                            var $nextSticky = stickies.eq(idx + 1),
                                $nextStickyPosition = $nextSticky.attr('data-originalPosition') - $thisSticky.attr('data-originalHeight');
                            if ((parseInt($stickyPosition) + parseInt($thisSticky.outerHeight()) - 2) <= parseInt($window.scrollTop())) {
                                if (!$thisSticky.hasClass('jux-mm-sticky')) {
                                    $thisSticky.addClass("jux-mm-sticky");
                                }
                            }
                            if ((parseInt($stickyPosition) + parseInt($thisSticky.outerHeight())) <= parseInt($window.scrollTop())) {
                                if (!$thisSticky.hasClass('jux-slideDown')) {
                                    $thisSticky.addClass("jux-slideDown");
                                }
                            }
                            if ($nextSticky.length > 0 && (($thisSticky.offset().top) >= $nextStickyPosition)) {
                                $thisSticky.addClass("absolute") /*.css("top", $nextStickyPosition)*/ ;
                            }
                        }
                    } else {
                        $thisSticky.removeClass("jux-mm-sticky");
                        $thisSticky.removeClass("jux-slideDown");
                        $thisSticky.removeClass("absolute").parent().css('height', 'auto');
                    }
                }
            });
            var lastPosition = 0;
            $window.off("scroll.stickies").on("scroll.stickies", function() {
                var st = $window.scrollTop();
                $stickies.each(function(i) {
                    if (!$(this).hasClass('jux-no-sticky')) {
                        var $thisSticky = $(this),
                            $stickyPosition = $thisSticky.attr('data-originalPosition');
                        if ($window.width() >= $thisSticky.prev().data('breakpoint')) {

                            if ($stickyPosition <= $window.scrollTop()) {
                                var $prevSticky = $stickies.eq(i - 1),
                                    $preStickyPosition = $thisSticky.attr('data-originalPosition') - $prevSticky.attr('data-originalHeight');
                                var $nextSticky = $stickies.eq(i + 1),
                                    $nextStickyPosition = $nextSticky.attr('data-originalPosition') - $thisSticky.attr('data-originalHeight');
                                if ((parseInt($stickyPosition) + parseInt($thisSticky.outerHeight()) - 2) <= parseInt($window.scrollTop())) {
                                    if (!$thisSticky.hasClass('jux-mm-sticky')) {
                                        $thisSticky.addClass("jux-mm-sticky");
                                    }
                                }
                                if ((parseInt($stickyPosition) + parseInt($thisSticky.outerHeight())) <= parseInt($window.scrollTop())) {
                                    if (!$thisSticky.hasClass('jux-slideDown')) {
                                        $thisSticky.addClass("jux-slideDown");
                                    }
                                }
                                if ($nextSticky.length > 0 && ($window.scrollTop() >= $nextStickyPosition)) {
                                    $thisSticky.addClass("absolute") /*.css("top", $nextStickyPosition)*/ ;
                                }
                            } else {
                                var $prevSticky = $stickies.eq(i - 1),
                                    $preStickyPosition = $thisSticky.attr('data-originalPosition') - $prevSticky.attr('data-originalHeight');
                                if ($thisSticky.hasClass('jux-mm-sticky')) {
                                    $thisSticky.removeClass("jux-mm-sticky");
                                }
                                if ($prevSticky.length > 0 && $window.scrollTop() <= $preStickyPosition + parseInt($thisSticky.outerHeight())) {
                                    $prevSticky.removeClass("absolute").removeAttr("style");
                                    $thisSticky.removeClass("jux-slideDown");
                                }
                            }
                        } else {
                            $thisSticky.removeClass("jux-mm-sticky");
                            $thisSticky.removeClass("jux-slideDown");
                            $thisSticky.removeClass("absolute").removeAttr("style");
                        }
                        lastPosition = st;
                    }
                });
            });
        }
    }

    $(window).resize(function() {
        var windowWidth = $(window).width();
        $('.jux-collapse-bar').each(function() {
            var breakpoint = $(this).data('breakpoint');
            if (windowWidth >= breakpoint) {
                $(this).next().removeClass('jux-no-sticky');
                sticky_menu($(".jux-easy-megamenu"));
                var menu = $(this).next();
                menu.show();
            } else {
                if (!$(this).next().hasClass('jux-no-sticky')) {
                    $(this).next().addClass('jux-no-sticky');
                }
                $(this).next().removeClass('jux-mm-sticky jux-slideDown');
            }
        });
    });

    $(window).on('load', function() {
        var windowWidth = $(window).width();

        $('.jux-collapse-bar').each(function() {
            var breakpoint = $(this).data('breakpoint');

            if (windowWidth >= breakpoint) {
                sticky_menu($(".jux-easy-megamenu"));
            } else {
                var $nextElement = $(this).next();

                if (!$nextElement.hasClass('jux-no-sticky')) {
                    $nextElement.addClass('jux-no-sticky');
                }

                $nextElement.removeClass('jux-mm-sticky jux-slideDown');
            }
        });
    });

})(jQuery)