jQuery(document).ready(function() {
	if(jQuery("#map_canvas").length > 0) {
          /*
              OPTIONS
           */
          // The table containing the links for the map:
          var links_table = jQuery("#standorte table");
          // Selector for links which add functionality to the map.
          var action_links = "a.map_action";
          //get the top offset of the target anchor
          var jumpmark = jQuery("#map_canvas").offset().top;
  
          // Start location:
          var options = {
                  zoom: 7,
                  center: new google.maps.LatLng(51.37863823622004, 7.679443359375),
                  mapTypeId: google.maps.MapTypeId.ROADMAP
          };
          var map = new google.maps.Map(document.getElementById("map_canvas"), options);
          
          // Standort-Sammlung:
          var marker_collection = [];
          // Info-Window Sammlung:
          var window_collection = [];
          
          // Standorte laden:
          jQuery.getJSON('tl_files/page/js/locations.json', function(json){
                  // Hole das "locations"-Objekt:
                  var locations = json.locations;
                  // Loopen:
                  jQuery.each(locations, function(i){
                          // Marker setzen:
                          var marker = new google.maps.Marker({
                                  position: new google.maps.LatLng(
                                          this.lat, this.lon
                                  ),
                                  title: this.name,
                                  map: map
                          });
                          marker_collection[i] = marker;
                          // Windows anhängen:
              var url = "http://maps.google.com/maps?daddr="+
                  "Deutschland+"+encodeURIComponent(this.ort)+"+"+this.plz+"+"+
                  encodeURIComponent(this.strasse);
                          var content = "<div>" +
                  "<h3>"+this.name+"</h3><p style='margin-bottom: 0;'>"+
                  this.strasse+"<br>"+this.plz+" "+this.ort+
                  "</p>" +
                  "<a href='"+url+"' target='_blank'>Route von Hier</a></div>";
                          var window = new google.maps.InfoWindow({
                                  content: content
                          });
                          window_collection[i] = window;
                          // Listener:
                          google.maps.event.addListener(marker, 'click', function() {
                  // Schließe fenster wieder:
                  jQuery.each(window_collection, function(i){
                      window_collection[i].close();
                  });
                                  window.open(map, marker);
                          });
                  });
          // Standort in die Liste:
          links_table.find(action_links).each(function(i,ele){
              jQuery(ele).attr("rel", i);
          });
          });
          
          // Jumper setzen:
          jQuery(action_links).live("click", function(event){
                  // Breche aktion ab:
                  event.preventDefault();
          // Schließe fenster wieder:
                  jQuery.each(window_collection, function(i){
                          window_collection[i].close();
                  });
                  // Hole Index:
                  var i = jQuery(this).attr("rel");
                  // Öffne Info:
                  window_collection[i].open(map, marker_collection[i]);
          // Zur karte springen:
          jQuery('html, body').animate({scrollTop:jumpmark}, 500);
                  // Bewege zu standort:
                  map.panTo(marker_collection[i].getPosition());
                  // Zooooom:
                  map.setZoom(6);
          });
	}
});
