var map = new google.maps.MVCArray();
var directionsDisplay = new google.maps.MVCArray();

var icon = 'themes/admin_myfa/googlemap.png';

function initialize_googlemap_v3(lat, lng, nazev_bodu, zoom, id_mapy, typ_mapy) {
  bod = new google.maps.LatLng(lat, lng);
 
  if (typ_mapy == 'satelitni') {
    myOptions = { 
      zoom: zoom, 
      center: bod, 
      mapTypeId: google.maps.MapTypeId.SATELLITE,
      draggable: true
    }; 
  } else {
    myOptions = { 
      zoom: zoom, 
      center: bod, 
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      draggable: true
    }; 
  }

  map[id_mapy] = new google.maps.Map(document.getElementById('googlemap_' + id_mapy), myOptions);

  marker = new google.maps.Marker({
    position: bod, 
    map: map[id_mapy], 
    title: nazev_bodu,
    icon: icon
  });

  /* inicializace objektu pro vykresleni vysledku vyhledavani */
  directionsDisplay[id_mapy] = new google.maps.DirectionsRenderer();
  directionsDisplay[id_mapy].setMap(map[id_mapy]);
  directionsDisplay[id_mapy].setPanel(document.getElementById('googlemap_route_' + id_mapy));
}

function set_directions(lat, lng, id_mapy) {
  var origin = $('#planovac_start_' + id_mapy).val();
  var destination = new google.maps.LatLng(lat, lng);

  /* povoleni / zakazani placenych useku */
  var avoidHighwaysCheck = document.getElementById('avoid_highways_' + id_mapy);
  if (avoidHighwaysCheck.checked == true) {
    avoidHighways = true;
  } else {
    avoidHighways = false;
  }

  /* inicializace objektu pro hledani trasy */
  directionsService = new google.maps.DirectionsService();
  
  /* inicuializace pozadavku pro vyhledavani */
  request = {
    origin : origin,
    destination : destination,
    travelMode : google.maps.DirectionsTravelMode.DRIVING,
    avoidHighways : true,
    avoidHighways : avoidHighways
  };  

  /* samotne hledani trasy */
  directionsService.route(request, function (response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      $('#googlemap_hlaska_' + id_mapy).html('');
      directionsDisplay[id_mapy].setDirections(response);    
    } else {
      $('#googlemap_route_' + id_mapy).html('');
      $('#googlemap_hlaska_' + id_mapy).html("<p class=\'ko\'>Výchozí bod se nepodařilo nalést. Změňte výchozí bod a vyhledejte trasu znovu.</p>");
    }
  });
} 
