var geocoder;
var map;
var div2infowindow = {};
var lastInfoWindow = null;

if(typeof String.prototype.trim !== 'function') {
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, ''); 
  }
}


$(document).ready(function()
{
  initialize();
  $('ul#standorteListe li > div').each(function (e)
  {
    var div = $(this);
    var address = $("p.googlemap",div).text().trim();
    parseCoords(address,div);	
  });


  $('ul#standorteListe li a').click(function (e)
  {
    var div = $(this).next();
    if (div.html() == null || $(this).attr('class')=='active') return;
    $('#standorteListe li a.active').removeClass("active");
    $(this).addClass("active");
    $("h2").text($(this).text());
    $("#standorteDetails").html(div.html());
    $("#standorteDetails").fadeIn("slow");
    var address = (div).find("p.googlemap").text().trim();
    openInfoWindow(div);
    $("ul.bilderleiste li.bild1").css("background-image","url("+ div.find("p.bild1").text() +")");
    $("ul.bilderleiste li.bild2").css("background-image","url("+ div.find("p.bild2").text() +")");
    return false;
   });

   //$('ul#standorteListe li a:eq(0)').click();
});

function initialize() 
{
  geocoder = new google.maps.Geocoder();
  var latlng = new google.maps.LatLng(49.48,11.09627);
  var myOptions = {
      zoom: 11,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  map = new google.maps.Map(document.getElementById("map"), myOptions);
}



function parseCoords(address, div)
{
   var coordsStr = (div).find("p.googlemap").attr("title");
   if (coordsStr != undefined)
   {
       var coords = coordsStr.split(",");
       var location = new google.maps.LatLng(parseFloat(coords[0]),parseFloat(coords[1]));
       setMarker(location,address,div);
   }
   else
       codeAddress(address, div);
}

function codeAddress(address, div) 
{
    geocoder.geocode({'address': address},function(results, status) { onGeocodeResult(results, status, div); }); 
}

function onGeocodeResult(results, status, div) 
{
    if (status == google.maps.GeocoderStatus.OK) 
        setMarker(results[0].geometry.location, results.formatted_address, div)
    else
        alert("Geocode was not successful for the following reason: " + status);
}

function setMarker(location,address,div)
{
    var marker = new google.maps.Marker({
        map: map, 
        position: location,
        title : address, 
        icon : "/common/img/punkt_gruen.png"
    });
    attachMessage(marker,div);
}

function attachMessage(marker, div) 
{
   var infowindow = new google.maps.InfoWindow({ content: div.html() });
   google.maps.event.addListener(marker, 'click', function() { openInfoWindow(div) });
   div2infowindow[div.attr("id")] = { infowindow:infowindow, marker:marker  };
}

function openInfoWindow(div)
{
   var obj = div2infowindow[$(div).attr("id")];
   if (lastInfoWindow!=null) lastInfoWindow.close();
   obj.infowindow.open(map,obj.marker);
   lastInfoWindow = obj.infowindow;
}

function log()
{
  try {console.log(arguments); } catch (e) {};
}





