/* Google Maps */
/* -------------------------------------------------------------------- */

function mk_google_maps() {
  "use strict";

  $('.mk-gmaps').each(function() {
    var $this = $(this),
      $parent = $this.parent(),
      height = $parent.height(),
      id = $this.attr('id'),
      zoom = $this.data('zoom'),
      latitude = $this.data('latitude'),
      longitude = $this.data('longitude'),
      address = $this.data('address'),
      latitude_2 = $this.data('latitude2'),
      longitude_2 = $this.data('longitude2'),
      address_2 = $this.data('address2'),
      latitude_3 = $this.data('latitude3'),
      longitude_3 = $this.data('longitude3'),
      address_3 = $this.data('address3'),
      pin_icon = $this.data('pin-icon'),
      pan_control = $this.data('pan-control'),
      map_type_control = $this.data('map-type-control'),
      scale_control = $this.data('scale-control'),
      draggable = $this.data('draggable'),
      zoom_control = $this.data('zoom-control'),
      modify_coloring = $this.data('modify-coloring'),
      saturation = $this.data('saturation'),
      hue = $this.data('hue'),
      lightness = $this.data('lightness'),
      json_styles = $this.data('json'),
      fullHeight = $this.data('fullheight'),
      map_height,
      header_height = 0,
      styles;

    var mapDimensions = function() {
      if ($.exists('#mk-header') && !$('#mk-header').hasClass('transparent-header')) {
        if($('#mk-header').hasClass('sticky-header')) {
          header_height = $('#mk-header').data('sticky-height');
        } else {
          header_height = $('#mk-header').data('height');
        }
      }
      if (fullHeight) {
        map_height = global_window_height - header_height - global_admin_bar_height;
      } else {
        map_height = height;
      }

      $parent.height(map_height);

      if($parent.hasClass('mk-gmaps-parallax')){
        $this.height(map_height+200);
      } else {
        $this.height(map_height);
      }
    };


    if (modify_coloring) {
      var styles = [{
        stylers: [
          {
            hue: hue
          }, {
            saturation: saturation
          }, {
            lightness: lightness
          }, {
            featureType: "landscape.man_made",
            stylers: [{ visibility: "on" }]
          }
        ]
      }];
    } else if (json_styles) {
      var styles = json_styles;
    }

    var map;

    function initialize() {
      var bounds = new google.maps.LatLngBounds();

      var mapOptions = {
        zoom: zoom,
        panControl: pan_control,
        zoomControl: zoom_control,
        mapTypeControl: map_type_control,
        scaleControl: scale_control,
        draggable: draggable,
        scrollwheel: false,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        styles: styles
      };

      map = new google.maps.Map(document.getElementById(id), mapOptions);
      map.setTilt(45);

      // Multiple Markers
      var markers = [];
      var infoWindowContent = [];

      if (latitude && longitude) {
        markers[0] = [address, latitude, longitude];
        infoWindowContent[0] = ['<div class="info_content"><p>' + address + '</p></div>'];
      }

      if (latitude_2 && longitude_2) {
        markers[1] = [address_2, latitude_2, longitude_2];
        infoWindowContent[1] = ['<div class="info_content"><p>' + address_2 + '</p></div>'];
      }

      if (latitude_3 && longitude_3) {
        markers[2] = [address_3, latitude_3, longitude_3];
        infoWindowContent[2] = ['<div class="info_content"><p>' + address_3 + '</p></div>'];
      }

      var infoWindow = new google.maps.InfoWindow(),
        marker, i;

      for (i = 0; i < markers.length; i++) {
        var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
        bounds.extend(position);
        marker = new google.maps.Marker({
          position: position,
          map: map,
          title: markers[i][0],
          icon: pin_icon
        });

        google.maps.event.addListener(marker, 'click', (function(marker, i) {
          return function() {
            infoWindow.setContent(infoWindowContent[i][0]);
            infoWindow.open(map, marker);
          }
        })(marker, i));

        map.fitBounds(bounds);
      }

      var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function(event) {
        this.setZoom(zoom);
        google.maps.event.removeListener(boundsListener);
      });
    }

    $(window).load(function() {
      mapDimensions();
      setTimeout(function() {
        initialize();
      }, 500);
    });

    $(window).on('resize', function() {
      setTimeout(function() {
        mapDimensions();
        google.maps.event.trigger(map, 'resize');
      }, 500);
    });
  });

  $(window).load(function() {
    if ($.exists('.mk-gmaps-parallax')) {
      var mk_skrollr = skrollr.init({
        forceHeight: false
      });
      mk_skrollr.refresh($('.mk-page-section'));
    }
  });
}