global.js 8.07 KB
require.config({
    "baseUrl": "/wp-content/themes/commonwell-corp/js/",
    "paths": {
        "jquery": "../components/jquery/jquery.min",
        "hoverIntent" : "../components/superfish/dist/js/hoverIntent",
        "superfish": "../components/superfish/dist/js/superfish.min",
        "fittext": "vendor/jquery.fittext",
        "superclick": "vendor/superclick",
        "featherlight": "vendor/featherlight.min"
    },
    "shim": {
        jquery: {
            exports: 'jquery'
        },
        hoverIntent: {
            deps: ['jquery']
        },
        superfish: {
            deps: ['jquery']
        },
        fittext: {
            deps: ['jquery']
        },
        superclick: {
            deps: ['jquery']
        },
        featherlight: {
            deps: ['jquery']
        }
    }
});  

require(['jquery', 'src/menu', 'fittext', 'src/sizing', 'src/care_form', 'src/facebook_extensible', 'src/canada-post'], function ($, Menu) {
    $.fn.extend({
        limiter: function (max, counter) {
            $(this).on("keyup focus", function () {
                var self = this;
                var words = (self.value.length > 0) ? ($.trim(self.value).replace(/['";:,.?¿\-]+/g, '').match(/\S+/g)).length : 0;
                if (typeof(counter) != 'undefined') {
                    counter.html(max - words);
                }
            });
        }
    });

    function noscroll(event) {
        event.preventDefault();
    }

    var App = {
        menu: null,
        init: function () {
            this.menu = new Menu($('#primary-nav'));
            this.menu.setup();
            $("#area").limiter(250, $("#chars"));
            $('.mobile-menu-btn').on('click', function (event) {
                event.preventDefault();
                $('body').toggleClass('menu-on');
                console.log('menu on');
                // lock scrolling when nav is open
//                if ($('body').hasClass('menu-on')) {
//                    $(document).on('touchmove', noscroll);
//                } else {
//                    $(document).off('touchmove', noscroll);
//                }
                return false;
            });

            // Change --- word to please select one
            $('form .form-control').find('select[name="community"] option').eq(0).html('Please select one');
            $('form .form-control').find('select[name="referral"] option').eq(0).html('Please select one');

        }
    };

    $(document).ready(function() {
        App.init();

        // we should use RAF for smooth moving
        window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.oRequestAnimationFrame;
        function createMapCloseBtn() {
            var $map = $('#map');
            $map.hide();
            // inject the close button
            $map.append('<a href="#" class="close-map"></a>');
            // hide the map when its clicked
            $map.find('.close-map').on('click', function (event) {
                event.preventDefault();
                $map.hide();
                return false;
            });
            // show the map when markers are clicked on
            $('#sl_div').find('.marker').on('click', function () {
                $map.show();
            });
        }

        function setMapStart() {
            // are we mobile?
            var mobile = (window.innerWidth < 960);
            // different lefts for different screens
            var mapLeft = mobile ? '53px' : '8px';
            // when should the map stop moving up?
            var minTop = $('#other-header').outerHeight() + $('#primary-nav').outerHeight() + 40;
            if(window.isBrokerPage) {
                minTop = (mobile) ? 50 : 0;
            }
            var is_safari = navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0;
            //if(is_safari) {
            var smallMobile = (window.innerWidth < 600);
            var mapWidth = smallMobile ? 0 : $('#map').outerWidth() + 8;
            $('#map').css({
                              top: minTop,
                              left: mapLeft,
                              opacity: 1,
                              zIndex: 1,
                              marginLeft: -mapWidth
                          });

            $(window).resize(function() {
                var smallMobile = (window.innerWidth < 600);
                var mapWidth = smallMobile ? 0 : $('#map').outerWidth() + 8;
                var mapLeft = smallMobile ? '53px' : '8px';

                $('#map').css({
                    left: mapLeft,
                    marginLeft: -mapWidth
                 });
            }).resize();
            //} else {
            //    // if the screen is mobile, and we havent injected the button
            //    $('#map').css({
            //        top: minTop,
            //        left: mapLeft,
            //        opacity: 1,
            //        zIndex: 1
            //    });
            //}

            if ($('.close-map').length === 0) {
                createMapCloseBtn();
            }
        }

        // scroller function to move the map
        function moveMap() {
            var mobile = (window.innerWidth < 960);
            var pos = (mobile) ? $(document).scrollTop() + 200 : $(document).scrollTop();
            var minTop = $('#other-header').outerHeight() + $('#primary-nav').outerHeight() + 20;
            if(window.isBrokerPage) {
                minTop = 0;
                pos =  (mobile) ? $(document).scrollTop() + 50 : $(document).scrollTop();
            }

            if (pos > minTop) {
                $('#map').css('top', pos);
                // call this function with RAF
                requestAnimationFrame(moveMap);
            } else {
                $('#map').css('top', minTop);
            }
        }

        // use a dom ready function for these events
        $(function () {
            var searchCat = $('#sl_div').find('#cat').attr('textvalue');
            if(searchCat && searchCat != 'broker') {
                $('#div_nameSearch #nameSearch').attr('placeholder', 'Company Name');
            }

            // when the search is clicked
            // and if the screensize is correct
            // move the map under the nav on the left side
            $('#addressSubmit').on('click', function () {
                // timeout for the delayed map loading
                setTimeout(function () {
                    setMapStart();
                }, 1000);
                // feebback to let people know that the form is actually working
                //$('#address_search').append('<p id="searching-feedback">Searching...</p>');
                $(this).attr('value', 'Searching...');
            });
            // used to avoid multiple listeners
            var scrollListener = false;
            // this event is trigger whenever the map is updated according to
            // the slp.js file in the store-locator-le plugin
            $('#map_sidebar').on('contentchanged', function () {
                //$('#searching-feedback').remove();
                $('#addressSubmit').attr('value', 'Search');

                setMapStart();
                if (!scrollListener) {
                    $(window).scroll(function () {
                        requestAnimationFrame(moveMap);
                    });
                    scrollListener = true;
                }

                // Check if the store has site url
                var url = $(this).parent().find('.slp_result_website > a').attr('href');
                if(url === undefined) {
                    $('.results_entry').find('.results_row_top .location_name').css('color', '#706f73');
                }

                //$(this).parent().find('.results_row_top').click(function() {
                //    var companyName = $(this).find('.location_name').text();
                //    var hasURL = $(this).find('.storelocatorlink');
                //    if(hasURL) {
                //        ga('send', 'event', 'broker', 'click',  companyName);
                //    }
                //});
            });
        });
    });


});