// JavaScript Document
function vykreslit_polygon(pts, lvls, line_color, line_opacity, fill_color, fill_opacity)
{
  var polygon = new GPolygon.fromEncoded({
    polylines: [{
        points: pts,
        levels: lvls,
        color: line_color,
        opacity: line_opacity,
        numLevels: 18,
        zoomFactor: 2
    }],
    fill: true,
    color: fill_color,
    opacity: fill_opacity,
    outline: true
  });
  if (GBrowserIsCompatible()) {
    map.addOverlay(polygon);
  }
}

function vykreslit_polyline(pts, lvls, line_color, line_opacity, weight)
{
  var polyline = new GPolyline.fromEncoded({
    color: line_color,
    weight: weight,
    opacity: line_opacity,
    points: pts,
    levels: lvls,
    zoomFactor: 2, 
    numLevels: 18
  });
  if (GBrowserIsCompatible()) {
    map.addOverlay(polyline);
  }
}

function vykreslit_znacku(primaryColor, strokeColor, label, labelColor, souradnice, odkaz )
{
  var iconOptions = {};
  iconOptions.primaryColor = primaryColor;
  iconOptions.strokeColor = strokeColor;
  iconOptions.label = label;
  iconOptions.labelColor = labelColor;
  var icon = MapIconMaker.createLabeledMarkerIcon(iconOptions);
  
  var marker = new GMarker(souradnice, {icon: icon});
  GEvent.addListener(marker, "click", function() {
            window.location.href=odkaz;
          });
  
  map.addOverlay(marker);    
}

function vykreslit_znacku_info(primaryColor, strokeColor, label, labelColor, souradnice, html )
{
  var iconOptions = {};
  iconOptions.primaryColor = primaryColor;
  iconOptions.strokeColor = strokeColor;
  iconOptions.label = label;
  iconOptions.labelColor = labelColor;
  var icon = MapIconMaker.createLabeledMarkerIcon(iconOptions);
  
  var marker = new GMarker(souradnice, {icon: icon});
  GEvent.addListener(marker, "click", function() {
    var myHtml = html;
    map.openInfoWindowHtml(souradnice, myHtml);
  });
  
  map.addOverlay(marker);    
}

// This function is from Google's polyline utility.
function decodeLine (encoded) {
  var len = encoded.length;
  var index = 0;
  var array = [];
  var lat = 0;
  var lng = 0;

  while (index < len) {
    var b;
    var shift = 0;
    var result = 0;
    do {
      b = encoded.charCodeAt(index++) - 63;
      result |= (b & 0x1f) << shift;
      shift += 5;
    } while (b >= 0x20);
    var dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
    lat += dlat;

    shift = 0;
    result = 0;
    do {
      b = encoded.charCodeAt(index++) - 63;
      result |= (b & 0x1f) << shift;
      shift += 5;
    } while (b >= 0x20);
    var dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
    lng += dlng;

    array.push(new GLatLng( lat * 1e-5, lng * 1e-5 ));
  }

  return array;
}

// BEGIN NEW CODE TO AVOID USING BODY TAG FOR LOAD/UNLOAD
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function vycentrovani(latlng) {
  var latlngbounds = new GLatLngBounds( );
  for ( var i = 0; i < latlng.length; i++ )
  {
    latlngbounds.extend( latlng[ i ] );
  }
  map.setCenter( latlngbounds.getCenter( ), map.getBoundsZoomLevel( latlngbounds ) );
}



