/* start custom jquery call for wordpress */
(function($, root, undefined) {
    $(function() {
        "use strict";
        /* Special jQuery wrapper for WordPress */
        /* DOM ready, take it away */

        /* headroom sticky nav */
        $(".header-main").headroom({
            "classes": {
                "initial": "headroom animated",
                "pinned": "slideDown",
                "unpinned": "slideUp",
            },
            "tolerance": {
                "down": 3,
                "up": 13,
            },
            "offset": {
                "down": 0,
                "up": 6,
            },
        });
        /* end headroom sticky nav */

        /* mega menu */
        $(".header-menu > li.menu-item-has-children").click(function(event) {

            /* remove clicked underline class for current menu indicator */
            $(".header-menu > li.menu-item-has-children > a").removeClass("clicked");

            // prevent default clickthrough behavior if sub menu is not expanded/visible
            if (!$(this).children(".sub-menu").hasClass("expanded")) {
                event.preventDefault();
            };

            /* remove expanded sub menu class when clicking a different parent menu item */
            $(".header-menu > li.menu-item-has-children .sub-menu").not(this).removeClass("expanded");

            /* add sub-menu expanded class and current link indicator clicked class */
            if (!$(".sub-menu").hasClass("expanded")) {
                $(this).children(".sub-menu").toggleClass("expanded");
                $(this).children(".header-menu > li.menu-item-has-children > a").addClass("clicked");
            };

            /* Hide dropdown menu and remove current clicked indicator class on click outside of menu */
            $(document).on("click", function(event) {
                if ( !$(event.target).closest(".menu-header-menu-container, .menu-header-submenu-container").length ) {
                    $(".sub-menu").not(this).removeClass("expanded");
                    $("a.clicked").removeClass();
                }
            });

            /* press escape key to hide menu */
            $(document).on('keydown', function(event) {
                if (event.keyCode === 27) { // ESC
                    $(".sub-menu").removeClass("expanded");
                    $("li.menu-item-has-children > a").removeClass();
                }
            });
            /* end press escape key to hide search overlay div */

        });
        /* end mega menu stuff */

        /* Global menu */
        /* add menu highlight on click */
        $(".global-menu-region").click(function() {
            $(".menu-regions .sub-menu li").not(this).removeClass("current");
            $(this).addClass("current");
            if ($(this).hasClass("current")) {
                event.preventDefault();
            }
        });

        // Reveal for africa
        $(".region-africa").click(function() {
            $(".countries-asia-pacific, .countries-central-south-america, .countries-europe, .countries-middle-east, .countries-north-america").hide();
            $(".countries-africa").show();
        });

        // Reveal for asia pacific
        $(".region-asia-pacific").click(function() {
            $(".countries-africa, .countries-central-south-america, .countries-europe, .countries-middle-east, .countries-north-america").hide();
            $(".countries-asia-pacific").show();
        });

        // Reveal for central south america
        $(".region-central-south-america").click(function() {
            $(".countries-africa, .countries-asia-pacific, .countries-europe, .countries-middle-east, .countries-north-america").hide();
            $(".countries-central-south-america").show();
        });

        // Reveal for europe
        $(".region-europe").click(function() {
            $(".countries-africa, .countries-asia-pacific, .countries-central-south-america, .countries-middle-east, .countries-north-america").hide();
            $(".countries-europe").show();
        });

        // Reveal for middle east
        $(".region-middle-east").click(function() {
            $(".countries-africa, .countries-asia-pacific, .countries-central-south-america, .countries-europe, .countries-north-america").hide();
            $(".countries-middle-east").show();
        });

        // Reveal for north america
        $(".region-north-america").click(function() {
            $(".countries-africa, .countries-asia-pacific, .countries-central-south-america, .countries-europe, .countries-middle-east").hide();
            $(".countries-north-america").show();
        });
        /* End global menu */

        /* mobile nav */
        /* show overlay on menu open click */
        $(".toggle-menu a").click(function() {

            /* close search overlay if it is open already */
            if ($(".search-overlay.open").is(":visible")) {
                /* remove class and close it */
                $(".search-overlay").removeClass("open");
            }

            $(".overlay").fadeToggle(200);
            $(this).toggleClass("btn-open").toggleClass("btn-close");
        });

        /* hide overlay on menu close click */
        $(".toggle-menu a.btn-close").on("click", function() {
            $(".overlay").fadeToggle(200);
            $(".toggle-menu a").toggleClass("btn-close").toggleClass("btn-open");
            open = false;
        });

        /* mobile full screen search overlay toggle */
        $('.toggle-overlay').click(function() {
            $('.search-overlay').toggleClass('open');
            setTimeout(function() {
                /* set time delay for focusing because input field is not visible until fade in animation completes */
                $('.search-overlay-form input.search-input').focus();
            }, 250);
        });
        /* end full screen search overlay toggle */

        /* mobile nested nav menus*/
        /* Open dropdown and close others if they are open */
        $(".mobile-menu li.menu-item-has-children").click(function() {

            /* change parent link to point right and indicate that the sub menu is expanded */
            $(this).children("a:first-child").toggleClass("expanded");

            /* Select only those which have dropdown, but not the one clicked */
            var $subs = $(".mobile-menu li.menu-item-has-children li").parent().parent().not($(this));

            /* Loop through each of the other dropdowns */
            $subs.each(function() {

                /* Check for visibility of sub menu and expanded arrow icon class */
                if ($(this).children("ul.sub-menu, a.expanded").is(":visible")) {
                    /* close it */
                    $(this).children("ul").slideUp();
                    /* remove expanded class to reset arrow icon */
                    $(this).children("a.expanded").removeClass("expanded");
                }
            });

            /* Toggle open state of target dropdown */
            $(this).children("ul").slideToggle();

        });
        /* end mobile nav */

        /* swiper home */
        var swiper = new Swiper('.swiper-home', {
            autoplay: {
                delay: 10000,
            },
            grabCursor: true,
            loop: false,
            pauseOnMouseEnter: true,
            simulateTouch: true,
            slidesPerView: 1,
            speed: 500,
            /* Navigation arrows */
            navigation: {
                nextEl: ".swiper-button-next-home",
                prevEl: ".swiper-button-prev-home",
            },
            /* If we need pagination */
            pagination: {
                clickable: true,
                el: ".swiper-pagination",
            },
        });
        /* end swiper home */

        /* swiper case studies */
        var swiper = new Swiper('.swiper-case-studies', {
            autoplay: {
                delay: 10000,
            },
            grabCursor: true,
            loop: true,
            pauseOnMouseEnter: true,
            simulateTouch: true,
            slidesPerView: 1,
            speed: 500,
            /* If we need pagination */
            pagination: {
                clickable: true,
                el: ".swiper-pagination",
            },
        });
        /* end swiper case studies */

        /* swiper testimonials */
        var swiper = new Swiper('.swiper-testimonials', {
            autoplay: {
                delay: 10000,
            },
            grabCursor: true,
            loop: false,
            pauseOnMouseEnter: true,
            simulateTouch: true,
            slidesPerView: 1,
            speed: 250,
            /* If we need pagination */
            pagination: {
                clickable: true,
                el: ".swiper-pagination",
            },
        });
        /* end swiper */

        /* swiper */
        var swiper = new Swiper('.swiper-about', {
            autoplay: {
                delay: 1000,
            },
            breakpoints: {
                // when window width is >= 0px
                0: {
                    slidesPerView: 2,
                    spaceBetween: 0
                },
                // when window width is >= 480px
                480: {
                    slidesPerView: 3,
                    spaceBetween: 0
                },
                // when window width is >= 960px
                960: {
                    slidesPerView: 6,
                    spaceBetween: 0
                }
            },
            grabCursor: true,
            loop: false,
            pauseOnMouseEnter: true,
            simulateTouch: true,
            slidesPerView: 6,
            speed: 2000,
        });
        /* end swiper */

        /* swiper products */
        var swiper = new Swiper('.swiper-products', {
            autoplay: {
                delay: 1000,
            },
            breakpoints: {
                // when window width is >= 0px
                0: {
                    slidesPerView: 1,
                    spaceBetween: 0
                },
                // when window width is >= 480px
                480: {
                    slidesPerView: 2,
                    spaceBetween: 0
                },
                // when window width is >= 960px
                960: {
                    slidesPerView: 3,
                    spaceBetween: 0
                }
            },
            grabCursor: true,
            loop: false,
            pauseOnMouseEnter: true,
            simulateTouch: true,
            slidesPerView: 3,
            speed: 2000,
            /* Navigation arrows */
            navigation: {
                nextEl: ".swiper-button-next-products",
                prevEl: ".swiper-button-prev-products",
            },
            /* If we need pagination */
            pagination: {
                clickable: true,
                el: ".swiper-pagination",
            },
        });
        /* end swiper */

        /* swiper */
        var swiper = new Swiper('.swiper-course-photos', {
            autoplay: {
                delay: 1000,
            },
            breakpoints: {
                // when window width is >= 0px
                0: {
                    slidesPerView: 2,
                    spaceBetween: 33,
                },
                // when window width is >= 480px
                480: {
                    slidesPerView: 3,
                    spaceBetween: 33,
                },
                // when window width is >= 960px
                960: {
                    slidesPerView: 6,
                    spaceBetween: 33,
                }
            },
            grabCursor: true,
            loop: false,
            pauseOnMouseEnter: true,
            simulateTouch: true,
            slidesPerView: 6,
            speed: 2000,
        });
        /* end swiper */

        /* swiper */
        var swiper = new Swiper('.swiper-shorts', {
            autoplay: {
                disableOnInteraction: true,
                pauseOnMouseEnter: true,
            },
            breakpoints: {
                // when window width is >= 0px
                0: {
                    slidesPerView: 1,
                    spaceBetween: 33,
                },
                // when window width is >= 480px
                480: {
                    slidesPerView: 3,
                    spaceBetween: 33,
                },
            },
            disableOnInteraction: true,
            slidesPerView: 3,
            speed: 3000,
            /* Navigation arrows */
            navigation: {
                nextEl: ".swiper-button-next-shorts",
                prevEl: ".swiper-button-prev-shorts",
            },
            /* If we need pagination */
            pagination: {
                clickable: true,
                el: ".swiper-pagination",
            },
        });
        /* end swiper */

        /* scroll to top */
        if ($(window).width() > 960) {
            $(document).ready(function() {
                $(window).bind("scroll", function() {
                    checkScrollPos();
                });
                checkScrollPos();

                $(".to-top-link").bind("click", function() {
                    $("html, body").animate({
                        scrollTop: 0
                    }, $(window).scrollTop() / 15);
                    return false;
                });
            });

            function checkScrollPos() {
                if ($(window).scrollTop() > 600) {
                    $(".to-top-link").fadeIn(500);
                } else {
                    $(".to-top-link").fadeOut(500);
                }
            }
        }

        /* prevent default scroll to top behavior on empty hashtag links */
        $("a[href$='#']").click(function(event) {
            event.preventDefault();
        });
        /* end back to top */

        /* tooltipster fancy tooltips for devices larger than 960 pixels */
        /* check window width first */
        if ($(window).width() > 960) {
            /* generic tooltips */
            $(".tooltip").tooltipster({
                side: "left",
                theme: "tooltipster-shadow",
            });
        }
        /* end tooltipster fancy tooltips */

        /* jquery colorbox lightbox */
        $(".iframe-lightbox-form").colorbox({
            iframe: true,
            height: "75%",
            rel: "nofollow",
            width: "75%",
        });

        $(".gallery").colorbox({
            height: "75%",
            rel: "gallery",
            transition: "none",
            width: "75%",
        });
        /* end colorbox lightbox*/

        /* reveal solution on failure analysis page */
        $("a.reveal-solution").click(function() {
            $(this).siblings(".solution").slideToggle("slow");

            /* change button text */
            var self = $(this);
            if (self.text() == "See the solution") {
                self.text("Hide the solution");
            } else {
                self.text("See the solution");
            }

            /* scroll to solution after clicking button to show it */
            $([document.documentElement, document.body]).animate({
                scrollTop: $(this).siblings(".solution").offset().top
            }, 250);

        });

        /* reveal content */
        $("a.reveal-content").click(function() {
            $(this).siblings(".hidden-content").slideToggle("slow");

            /* change button text */
            var self = $(this);
            if (self.text() == "Read More") {
                self.text("Hide");
            } else {
                self.text("Read More");
            }

        });

        /* Move noria guarantee badge on course pages mobile views <700 */
        /* check window width first */
        if ($(window).width() < 700) {
            $(".course-overview .guarantee-link").prependTo(".course-description>p");
        }

        /* popup flash sale */
        /* jquery cookie */
        /* jquery popup */
        /* drop into scripts.js to activate */

        /* if flash sale cookie is not set, set it */
        if (!($.cookie('flashSale') == 1)) {
            $(document).ready(function() {

                var id = '#dialog';

                //transition effect
                $('#mask').fadeIn(500);
                $('#mask').fadeTo("slow", 0.9);

                //transition effect
                $(id).fadeIn(2000);

                //if close button is clicked, hide and set cookie
                $('#x').click(function(e) {
                    //Cancel the link behavior
                    e.preventDefault();

                    $('#mask').hide();
                    $('.window').hide();

                    $.cookie('flashSale', '1', { expires: 1, path: '/' });

                });

                //if mask is clicked, hide and set cookie
                $('#mask').click(function() {
                    $(this).hide();
                    $('.window').hide();
                    $.cookie('flashSale', '1', { expires: 1, path: '/' });
                });

                //if popup graphic link is clicked, hide and set cookie
                $('.flash-sale').click(function() {
                    $(this).hide();
                    $('#mask').hide();
                    $('.window').hide();
                    $.cookie('flashSale', '1', { expires: 1, path: '/' });
                });

            });
        }

    }); /* end main function wrapper, put all script code before this closing bracket */

})(jQuery, this);
/* end custom jquery call for wordpress */