var directionsService = new google.maps.DirectionsService();
var directionsDisplay;
var infowindow;
var map;
var centerX = 0;
var centerY = 0;

function init() {
	  directionsDisplay  = new google.maps.DirectionsRenderer();
	  var myOptions = {
			zoom: 5,
		    mapTypeControl: false,
		    center: new google.maps.LatLng(51.165691,10.451526),
		    mapTypeId: google.maps.MapTypeId.ROADMAP
		  }
	  map = new google.maps.Map(document.getElementById("map"),
	                                myOptions);
	  directionsDisplay.setMap(map);
}


function initialize(users,url) {
  directionsDisplay  = new google.maps.DirectionsRenderer();
  
  var theMinLat = users[0].lat;
  var theMinLng = users[0].lng;
  var theMaxLat = users[0].lat;
  var theMaxLng = users[0].lng;
  
  for(var i=0; i<users.length; i++ ){
	  
	  centerX += Number(users[i].lat);
	  centerY += Number(users[i].lng);
	  if(theMinLat >= users[i].lat){
		  theMinLat = users[i].lat;
	  }else{
		  theMaxLat = users[i].lat;
	  }
	  
	  if(theMinLng >= users[i].lng){
		  theMinLng = users[i].lng;
	  }else{
		  theMaxLng = users[i].lng;
	  }
  }
  centerX = centerX/users.length;
  centerY = centerY/users.length;
  
  var myOptions = {
    mapTypeControl: false,
    center: new google.maps.LatLng(centerX, centerY),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  map = new google.maps.Map(document.getElementById("map"),
                                myOptions);
  
  map.fitBounds(new google.maps.LatLngBounds(
		  //bottom left
		  new google.maps.LatLng(theMinLat, theMinLng),
		  //top right
		  new google.maps.LatLng(theMaxLat, theMaxLng)
		));
  
  
  directionsDisplay.setMap(map);
  
  setMarkers(map, users,url);
}

function setMarkers(map, users,url) {
  // Add markers to the map

  // Marker sizes are expressed as a Size of X,Y
  // where the origin of the image (0,0) is located
  // in the top left of the image.

  // Origins, anchor positions and coordinates of the marker
  // increase in the X direction to the right and in
  // the Y direction down.
  var image = new google.maps.MarkerImage(url+'fileadmin/templates/images/icon.jpg',
      // This marker is 20 pixels wide by 32 pixels tall.
      new google.maps.Size(45,39),
      // The origin for this image is 0,0.
      new google.maps.Point(0,0),
      // The anchor for this image is the base of the flagpole at 0,32.
      new google.maps.Point(0, 39));
  for (var i = 0; i < users.length; i++) {
    var user = users[i];
    var myLatLng = new google.maps.LatLng(user.lat, user.lng);
    var a=0;
    
    var content = '<div class="map-info-window">';
    content += '<h3>'+user.name+'</h3>';
    if(user.street != '') content += '<p>'+user.street+' </p>';
    if(user.zip != '' || user.city != '') content += '<p>'+user.zip+' '+user.city+' </p>';
    if(user.phone != '') content += '<p>'+user.phone+' </p>';
    if(user.winename != '') content += '<p>'+user.winename+' '+user.winevintage+'</p>';
    content += '</div>';
    
    
    var marker = new google.maps.Marker({
        position: myLatLng,
        map: map,
        icon: image,
        title: user.name,
        zIndex: 1,
        html: content
    });
    
    
    var infowindow = new google.maps.InfoWindow(
    { 
    	content: content,
        size: new google.maps.Size(20,20),
        position: myLatLng
    });
    
    var event = new google.maps.event.addListener(marker, 'click', function() {
    	infowindow.setContent(this.html);
    	infowindow.open(map,this);
    });
  }
}
if(users.length > 0 ) {
	initialize(users,url);
}else{
	init();
}


