FSB.contact = {
    markers: {},
    gMapsLoaded: false,
    init: function() {
      $(document).foundation();
      this.accordionClose();
      FSB.contact.loadGoogleMaps();
      FSB.contact.mapsData = {
          "mapsData": [
             {
                  "title": "福建福山轴承有限公司",
                  "address": "地址：福建省南安市霞美镇福山工业区",
                  "tel": "电话: +86-595-86760088 / 86760022",
                  "fax": "传真: +86-595-86750000 / 8635700",
                  "latitude": 24.922120760657247,
                  "longitude": 118.49218904972076
              }
          ]
      };
    },
    accordionClose: function() {
        $('.accordion-content .btn-close').click(function() {
            $('.is-active .accordion-title').trigger('click');
        })
    },
    gMapsCallback: function() {
      FSB.contact.gMapsLoaded = true;
      $(window).trigger('FSB.contact.gMapsLoaded');
      FSB.contact.initMap();
    },
    loadGoogleMaps: function() {
      if(FSB.contact.gMapsLoaded) {
        return window.FSB.contact.gMapsCallback();
      } else {
        var script = document.createElement('script');
        script.setAttribute("type","text/javascript");
        script.setAttribute("src","http://ditu.google.cn/maps/api/js?key=AIzaSyCqXJmSRZntV52LKsleNMGmxHAfqm_qP9k&callback=FSB.contact.gMapsCallback");
        (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script);
      }
    },
    initMap: function() {
      FSB.contact.infowindow = new google.maps.InfoWindow();
      if (FSB.contact.mapsData.mapsData.length > 0) {
        FSB.contact.mapCenter = {lat: FSB.contact.mapsData.mapsData[0].latitude, lng: FSB.contact.mapsData.mapsData[0].longitude}
      } else {
        FSB.contact.mapCenter = {lat: 26.267136, lng: 117.64057700000001}
      }

      FSB.contact.map = new google.maps.Map(document.getElementById('map'), {
        scrollwheel: false,
        mapTypeControl: false,
        mapTypeId: google.maps.MapTypeId.SATELLITE,
        center: FSB.contact.mapCenter,
        zoom: 12
      });

      if (FSB.contact.mapsData.mapsData.length > 0) {
        for (i in FSB.contact.mapsData.mapsData) {
            var marker = FSB.contact.createMarkers(FSB.contact.mapsData.mapsData[i]);
        };
      };

    },
    createMarkers: function(location) {
      var icon = {
        url: '/workspace/assets/img/icons/marker_icon@2x.png',
        size: new google.maps.Size(36, 45),
        origin: new google.maps.Point(0, 0),
        scaledSize: new google.maps.Size(36,45),
        anchor: new google.maps.Point(0, 36)
      };
      var latLng = new google.maps.LatLng( location.latitude, location.longitude);
      var marker = new google.maps.Marker({
        position : latLng,
        map: FSB.contact.map
        // icon: icon
      });
      google.maps.event.addListener(marker, 'click', function(){
          FSB.contact.map.setCenter(marker.getPosition());
          FSB.contact.infowindow.close(); // Close previously opened infowindow
          FSB.contact.infowindow.setContent(
            '<div class="infowindow">'+
              '<h4>'+ location.title +'</h4>'+
              '<p>'+ location.address +'<br/>'+ location.tel +'<br/>'+ location.fax +'</p>'+
            '</div>'
          );
          FSB.contact.infowindow.open(FSB.contact.map, marker);
      });
      return marker;
    },
    createMarkerButton: function(marker,location) {
        var ul = document.querySelector(".network-list");
        var li = document.createElement("li");
        var content = '<a href="javascript:void(0);"><span class="title">'+ location.title +'</span><span class="address">'+ location.address +'</span></a>';
        li.innerHTML = content;
        ul.appendChild(li);
        
        //Trigger a click event to marker when the button is clicked.
        google.maps.event.addDomListener(li, "click", function(){
          google.maps.event.trigger(marker, "click");
        });
    }
}