;(function(window, $, undefined) { var _once = false; function _initializeMap() { $('owd-map:not(.map-initialized)').each(function() { var $map = $(this); $map.addClass('map-initialized'); var $markers = $map.find('owd-map-marker'); var lat = $map.attr('center-lat'); var lng = $map.attr('center-lng'); var Latlng = new google.maps.LatLng(lat, lng); var zoom = $map.attr('zoom'); var myOptions = { zoom: zoom !== undefined ? parseInt(zoom) : 13, panControl: false, draggable: true, scrollwheel: false, mapTypeControl: false, scaleControl: false, navigationControl: false, streetViewControl: false, center: Latlng, mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map($map[0], myOptions); map.setOptions({ styles: [ { "featureType": "administrative", "elementType": "all", "stylers": [ { "visibility": "on" } ] }, { "featureType": "administrative", "elementType": "labels.text.fill", "stylers": [ { "color": "#444444" } ] }, { "featureType": "administrative.country", "elementType": "all", "stylers": [ { "visibility": "on" } ] }, { "featureType": "administrative.country", "elementType": "geometry.fill", "stylers": [ { "visibility": "on" } ] }, { "featureType": "administrative.province", "elementType": "all", "stylers": [ { "visibility": "on" }, { "lightness": "-12" }, { "gamma": "1.00" } ] }, { "featureType": "administrative.locality", "elementType": "all", "stylers": [ { "visibility": "on" } ] }, { "featureType": "landscape", "elementType": "all", "stylers": [ { "color": "#ffffff" } ] }, { "featureType": "poi", "elementType": "all", "stylers": [ { "visibility": "off" } ] }, { "featureType": "road", "elementType": "all", "stylers": [ { "saturation": "3" }, { "lightness": "3" }, { "gamma": "1.03" }, { "color": "#ff4e2e" }, { "invert_lightness": true } ] }, { "featureType": "road.highway", "elementType": "all", "stylers": [ { "visibility": "simplified" }, { "gamma": "1.78" }, { "lightness": "23" } ] }, { "featureType": "road.highway", "elementType": "labels", "stylers": [ { "visibility": "off" } ] }, { "featureType": "road.highway.controlled_access", "elementType": "all", "stylers": [ { "visibility": "on" }, { "color": "#ff4e2e" }, { "saturation": "-3" }, { "lightness": "-4" }, { "gamma": "1.78" }, { "weight": "0.66" } ] }, { "featureType": "road.highway.controlled_access", "elementType": "labels", "stylers": [ { "visibility": "off" } ] }, { "featureType": "road.arterial", "elementType": "all", "stylers": [ { "visibility": "off" } ] }, { "featureType": "road.arterial", "elementType": "labels.icon", "stylers": [ { "visibility": "off" } ] }, { "featureType": "road.local", "elementType": "all", "stylers": [ { "visibility": "off" } ] }, { "featureType": "transit", "elementType": "all", "stylers": [ { "visibility": "off" } ] }, { "featureType": "water", "elementType": "all", "stylers": [ { "color": "#e1e1e1" }, { "visibility": "on" } ] } ] }); var markers = []; $markers.each(function() { var $marker = $(this); var lat = $marker.attr('lat'); var lng = $marker.attr('lng'); var width = parseInt($marker.attr('width')); var height = parseInt($marker.attr('height')); var markerLatlng = new google.maps.LatLng(lat, lng); var image = $marker.attr('src'); var href = $marker.attr('href'); var anchor; if ($marker.attr('anchor') == 'bottom') { anchor = new google.maps.Point(width/2, height); } else if ($marker.attr('anchor') == 'bottom-left') { anchor = new google.maps.Point(0, height); } else if ($marker.attr('anchor') == 'bottom-right') { anchor = new google.maps.Point(width, height); } else if ($marker.attr('anchor') == 'top-left') { anchor = new google.maps.Point(0, 0); } else if ($marker.attr('anchor') == 'top-right') { anchor = new google.maps.Point(width, 0); } else if ($marker.attr('anchor') == 'top') { anchor = new google.maps.Point(width/2, 0); } else if ($marker.attr('anchor') == 'left') { anchor = new google.maps.Point(0, height/2); } else if ($marker.attr('anchor') == 'right') { anchor = new google.maps.Point(width, height/2); } else { anchor = new google.maps.Point(width/2, height/2); } var marker = new google.maps.Marker({ position : markerLatlng, icon : image, anchor : anchor, size : new google.maps.Size(width, height), animation: google.maps.Animation.DROP }); google.maps.event.addListener(marker, 'click', function() { window.open(href); }); $('body').on('destroy', function _destroy(e) { if ($(e.target).find($map).length) { $('body').off('destroy', _destroy); google.maps.event.clearListeners(marker, 'click'); marker.setMap(null); delete marker; } }); markers.push(marker); marker.setMap(map); }); var mainLat = $('.locations-main-office').data('lat'), mainLng = $('.locations-main-office').data('lng'), distLat, distLng; $('.locations-city').on('click touchstart', function() { var nextSection = $(this).parents('.locations').next().offset().top - 100; var bounds = new google.maps.LatLngBounds(); $('.map').removeClass('hidden'); bounds.extend(new google.maps.LatLng(mainLat, mainLng)); distLat = $(this).data('lat'); distLng = $(this).data('lng'); var $markersArray = []; for ( j = 0; j < markers.length; j++) { srcLat = $markers[j].attributes.lat.value; srcLng = $markers[j].attributes.lng.value; if ((srcLat == distLat && srcLng == distLng) || (srcLat == mainLat && srcLng == mainLng)) { markers[j].setVisible(true); } else { markers[j].setVisible(false); } } bounds.extend({ lat: parseFloat(distLat), lng: parseFloat(distLng) }); var map_center = bounds.getCenter(); map.setCenter(map_center); map.panToBounds(bounds); map.fitBounds(bounds); $('html,body').animate({ scrollTop: nextSection }, 300, 'easeOutQuint'); }); $('body').on('destroy', function _destroy(e) { if ($(e.target).find($map).length) { $('body').off('destroy', _destroy); delete map; delete myOptions; delete latlng; } }); }); } var _prepareMapsApi = function() { if ($('owd-map').length) { if (!_once) { var script = document.createElement('script'); script.type = 'text/javascript'; script.src = '//maps.googleapis.com/maps/api/js?v=3.exp&' + 'callback=prepareMapsReady&key=AIzaSyBe26U-sNlEJYqthDv4bDI7kkySfi-n9nQ'; document.body.appendChild(script); _once = true; } else { prepareMapsReady(); } } }; window.prepareMapsReady = _initializeMap; $(window).on('load ajaxload', _prepareMapsApi); })(window, window.jQuery);