 /*******************************************************************************
* Sripty pro Googlemap
* 
* 2009-02-04 Chaloupka Jaromír
*******************************************************************************/

// Funkce pro kontrolku GPS souřadnic podle kurzoru myši
function kontrolka_gps_souradnic() {
}

kontrolka_gps_souradnic.prototype = new GControl();

kontrolka_gps_souradnic.prototype.initialize = function(map) {
  var container = document.createElement('div');
  this.setStyle_(container);
  container.innerHTML = 'GPS souřadnice';
  GEvent.addDomListener(map, "mousemove", function(point) {
    souradnice = zformatuj_gps_souradnice(point.x, point.y);
    container.innerHTML = 'GPS souřadnice: ' + souradnice;
  });
  map.getContainer().appendChild(container);
  return container;
}

kontrolka_gps_souradnic.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(70, 7));
}

kontrolka_gps_souradnic.prototype.setStyle_ = function(div) {
  div.style.backgroundColor = '#565455';
  div.style.border = '1px solid #999';
  div.style.padding = '3px';
  div.style.fontFamily = 'Verdana,sans-serif';
  div.style.fontSize = '11px';
  div.style.fontWeight = 'bold';
}

// Funkce, která zformátuje GPS souřadnice
function zformatuj_gps_souradnice(y, x) {
  var gps_x = '';
  var gps_y = '';  
  
  x_stupne = zformatuj_stupne(x);  
  gps_x += (x<0) ? 'S' : 'N';
  gps_x += x_stupne;
  
  y_stupne = zformatuj_stupne(y);  
  gps_y += (y<0) ? 'W' : 'E';
  gps_y += y_stupne;
  
  gps = gps_x + ', ' + gps_y;
  return gps;
}

// Funkce, která zformátuje stupně x°xy'xy.zw"
function zformatuj_stupne(s) {
  var stupne = '';
  var abs = Math.abs(s);
  stupne += Math.floor(abs);
  stupne += '°';
    
  minuty = (s - Math.floor(s)) * (60/100) * 100;
  stupne += Math.floor(minuty);
  stupne += '\'';
    
  sekundy = (minuty - Math.floor(minuty)) * (60/100) * 100;
  stupne += sekundy.toFixed(2);
  stupne += '"';
    
  return stupne;
}

var map = null;
var geocoder = null;
var gdir;
var markersOnMap = [];

// Ikonka firmy pro zobrazení v googlemapě
var ikonka = new GIcon(); 
ikonka.image = 'http://www.stresni-zahrady.cz/img/googlemap.png';
ikonka.shadow = 'http://www.stresni-zahrady.cz/img/googlemap_stin.png';
ikonka.iconSize = new GSize(20, 34);
ikonka.shadowSize = new GSize(37, 34);
ikonka.iconAnchor = new GPoint(9, 34);
ikonka.infoWindowAnchor = new GPoint(9, 2);

var customIcons = [];
customIcons["firma"] = ikonka;

// Funkce pro inicializaci googlemapy
function initialize(id_firmy) {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("googlemap"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.addControl(new kontrolka_gps_souradnic());
    map.setCenter(new GLatLng(49.3550, 17.1525), 15);
    geocoder = new GClientGeocoder();
    gdir = new GDirections(map, document.getElementById("route"));
    GEvent.addListener(gdir, "load", onGDirectionsLoad);
    GEvent.addListener(gdir, "error", handleErrors);
    //setDirections("San Francisco", "Mountain View", "en_US");

    var name = 'Jart - Janda, spol. s r.o.';
    var address = 'Hanáckého pluku 1153/4, Olomouc';
    var type = 'firma';
        
    if (geocoder) {
      geocoder.getLatLng(
        address,
        function(point) {
          if (!point) {
            //alert(address + " nebyla nalezena.");
          } else {                
            var marker = createMarker(point, name, address, type, gdir);
            map.setCenter(point, 15);
            map.addOverlay(marker);                
          }
        }
      );
    }
  }
}

// Funkce pro vytvoření značky na googlemapě
function createMarker(point, name, address, type) {
  var marker = new GMarker(point, customIcons[type]);
  markersOnMap.push(marker);
  var html = "<p><strong>" + name + "</strong></p>"+
             "<p style=\"color: #000\">" + address + "</p>";
  GEvent.addListener(marker, 'click', function() {
    marker.openInfoWindowHtml(html);
  });
  return marker;
}

// Funkce pro plánovač trasy
function setDirections(fromAddress, toAddress, locale) {
  for (var i = 0; i < markersOnMap.length; i++) {
    var marker = markersOnMap[i];
    if (marker.isHidden()) {
      marker.show();
    } else {
      marker.hide();
    }
  }
  gdir.load("from: " + fromAddress + " to: " + toAddress,
  { "locale": locale });
}

// Funkce pro odchytávání chyb
function handleErrors(){
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	  alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
	else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	  alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	  alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
  else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	  alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	  alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	 else alert("An unknown error occurred.");
}

function onGDirectionsLoad() {
  // Use this function to access information about the latest load()
  // results.
  // e.g.
  // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
  // and yada yada yada...
}

