
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';
}


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;
}


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 = [];


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;


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(56.3297, 52.40123), 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 = 'NOVATRONIC';
    var address = 'U potoka 1307/10, 787 01 Šumperk';
    var type = 'firma';
    
    var html = "<p style=\"margin: 0px 0px 10px 0px;\"><strong>" + name + "</strong></p>"+
              "<p style=\"color: #000; margin: 0px 0px 10px 0px;\">" + address + "</p>"+
              "<p style=\"color: #000; margin: 0px;\">www.novatronic.cz</p>";
    
    if (geocoder) {
      geocoder.getLatLng(
        address,
        function(point) {
          if (!point) {

          } else {                
            var marker = createMarker(point, name, address, type, html);
            map.setCenter(point, 15);
            map.addOverlay(marker); 
            marker.openInfoWindowHtml(html);                
          }
        }
      );
    }
  }
}


function createMarker(point, name, address, type, html) {
  var marker = new GMarker(point, customIcons[type]);
  markersOnMap.push(marker);

  GEvent.addListener(marker, 'click', function() {
    marker.openInfoWindowHtml(html);
  });

  return marker;
}


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 });
}


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() {

}
