addressTimeWait = 400;
// Drawing control
function DistanceControl() {
}
DistanceControl.prototype = new GControl(true, false);

// We add the control to
// to the map container and return the element for the map class to
// position properly.
DistanceControl.prototype.initialize = function(map) {
  var container = document.createElement("div");
  container.map = map;
  container.style.cursor = "pointer";
  container.backgroundOn = false;
  container.id = "DistanceContainer1";
  container.timerId=-1;

  container.helpDiv= document.createElement("DIV");
  container.helpDiv.style.visibility = "hidden";
    
  this.oDistanceSign = document.createElement("img");
  this.oDistanceSign.title='';
  this.oDistanceSign.src='../images/DistanceSign.gif';
  this.oDistanceSign.style.cursor = "pointer";
  container.appendChild(this.oDistanceSign);

  var oMapType=map.getCurrentMapType();

  this.oDistanceText = document.createElement("small");
  this.oDistanceText.Text="";
  this.oDistanceText.id = "DistanceText1";
  this.oDistanceText.style.cursor = "pointer";
  this.oDistanceText.style.position = "absolute";
  this.oDistanceText.style.top = "0px";
  this.oDistanceText.style.left = "15px";
  //this.oDistanceText.style.height = "100%";

  if (oMapType)
    this.oDistanceText.style.color=oMapType.getTextColor();
  else
    this.oDistanceText.style.color = "#FFFFFF";
  this.oDistanceText.innerHTML = "";
  this.oDistanceText.setDistance=function(nDistance){
    polyShape = new GPolyline(polyPoints,lineColor,lineWeight,opacity);
    nDistance = polyShape.getLength();
    var sDUM = "m";
    var sDUKM = "km";
    var sDUMin = "Minuty";
    var sDUH = "Godziny";
    var sDUD = "Dni";
    if (document.oInterfaceText){
        sDUM = oInterfaceText.getItemValue('ScaleControlMetersUnit',1);
        sDUKM = oInterfaceText.getItemValue('ScaleControlKilometersUnit',1);
        sDUMin = oInterfaceText.getItemValue('DistanceControlMinutesUnit',1);
        sDUH = oInterfaceText.getItemValue('DistanceControlHoursUnit',1);
        sDUD = oInterfaceText.getItemValue('DistanceControlDaysUnit',1);
    }
    if (nDistance)
        if (nDistance > -1)
            this.nDistance = nDistance;
        else
            nDistance = this.nDistance;
    else
        nDistance = this.nDistance;

    if (nDistance > -1){
        var nDistanceValue=nDistance;
        var sDistanceUnit=" " + sDUM + " ";
        var nWalkTime=(((nDistance / 1000) / 5) * 60).toFixed(0);
        var nWalkTimeMinutes=nWalkTime;
        var nWalkTimeHours=0;
        var nWalkTimeDays=0;
        var sWalkTime=nWalkTime.toString() + " " + sDUMin + " ";
        var re = / /g;
        if (nDistance > 1000)
        { nDistanceValue = nDistance / 1000; sDistanceUnit=" " + sDUKM + " "; }
        if (nWalkTime > 59)
        {
            nWalkHours = Math.floor((nWalkTime / 60));
            if (nWalkHours < 24)
                sWalkTime=nWalkHours.toString() + sDUH + " " + (nWalkTime-(nWalkHours*60)).toString() + sDUMin + " ";
            else
                sWalkTime=Math.floor((nWalkHours / 24)).toString() + sDUD + " " + (nWalkHours-(Math.floor((nWalkHours/24))*24)).toString() + sDUH + " " + (nWalkTime-(nWalkHours*60)).toString() + sDUMin;
        }
        var sHTMLDistanceUnit=sDistanceUnit.replace(re, "&nbsp;");
        var sHTMLWalkTime=sWalkTime.replace(re, "&nbsp;");
        this.innerHTML=nDistanceValue.toFixed(2).toString() + sHTMLDistanceUnit + "<br>" + sHTMLWalkTime;
        this.Text=nDistanceValue.toFixed(2).toString() + sDistanceUnit + "\n" + sWalkTime;
        this.parentNode.style.width = (this.offsetWidth + this.offsetLeft).toString() + "px";
    }
  }

  container.onmouseover=function(){
     if (!this.backgroundOn)
        this.setBackground(true);

    if (this.timerId == -1)
    {
        if (document.mapData)
            document.mapData.requestControlHelp();
        this.timerId = window.setTimeout("Oblicz odleglosc", 6000);
    }
  }
  
  container.onmouseout=function(){
     if (!this.backgroundOn)
        this.setBackground(false);

    if (this.timerId > -1)
    {
        window.clearTimeout(this.timerId);
        this.timerId = -1;
        this.hideControlHelp();
    }
  }
  
  container.showControlHelp=function(){
      this.helpDiv.style.visibility = 'visible';
  }

  container.hideControlHelp=function(){
      this.helpDiv.style.visibility = 'hidden';
  }
  
  container.onclick=function(){
     this.backgroundOn=!this.backgroundOn;
     if (this.backgroundOn)
        this.setBackground(true);
  }
  
  container.setBackground=function(bOn){
      if (bOn)
      {
         this.style.backgroundColor="#FFFFFF";
         var oText = document.getElementById("DistanceText1");
         if (oText)
            oText.style.color="#000000";

         this.style.border="1px solid #0000FF";
      }
      else
      {
         this.style.backgroundColor="transparent";
         this.style.border="none";
         var oText = document.getElementById("DistanceText1");
         if (oText){
            //oText.style.backgroundColor="transparent";
            var oMapType=map.getCurrentMapType();
            if (oMapType)
              oText.style.color=oMapType.getTextColor();
            else
              oText.style.color = "#FFFFFF";
         }
      }
  }
  
  container.appendChild(this.oDistanceText);
  container.appendChild(container.helpDiv);


  map.getContainer().appendChild(container);
  
  GEvent.addListener(map, "maptypechanged", function() {
      var oButton = document.getElementById("DistanceText1");
      if (!oButton.parentNode.backgroundOn){
          var oMapType=map.getCurrentMapType();
          if (oMapType)
            oButton.style.color=oMapType.getTextColor();
      }
  });
  
  
  return container;
}

// By default, the control will appear in the top left corner of the
// map with 7 pixels of padding.
DistanceControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(100, 500)); //, new GSize(5, 27)
}

   // mapClick - Handles the event of a user clicking anywhere on the map
   // Adds a new point to the map and draws either a new line from the last point
   // or a new polygon section depending on the drawing mode.
 //   var container = document.createElement("div");
//  container.id="Odleglosc";
//  container.map=map;
    
    
    function mapClick(marker, clickedPoint) {
      //_uacct = "UA-2270875-1";
     // urchinTracker();
     // var map = document.getElementById("map");
      polygonMode = document.getElementById("drawMode_polygon").checked;

      // Push onto polypoints of existing polygon
      polyPoints.push(clickedPoint);
      drawCoordinates();
     }


      // Clear current Map
      function clearMap(){
        map.clearOverlays();
        polyPoints = [];
        //document.getElementById("status").innerHTML = "";
      }


      // Toggle from Polygon PolyLine mode

      function toggleDrawMode(){
        map.clearOverlays();
        polyShape = null;
        polygonMode = document.getElementById("drawMode_polygon").checked;
        drawCoordinates();
      }


    // drawCoordinates
    function drawCoordinates(){
      //Re-create Polyline/Polygon
      var sDUM = "m";
      var sDUKM = "km";
      var sDUMin = "min";
      var sDUH = "h";
      var sDUD = "dni";
      
      
      if (polygonMode) {
        polyShape = new GPolygon(polyPoints,lineColor,lineWeight,opacity,fillColor,opacity);
        var area = polyShape.getArea()/(1000*1000);
        var info = "Pole: <b> " + area.toFixed(4)+ "km2 </b>";
      //  document.getElementById("status").innerHTML = "Pole (km2): <b> " + area.toFixed(4)+ "</b>";
      } else {
        polyShape = new GPolyline(polyPoints,lineColor,lineWeight,opacity);
        var length = polyShape.getLength()/1000;
        var nDistance = polyShape.getLength();
        var nDistanceValue=nDistance;
        var sDistanceUnit=" " + sDUM + " ";
        var nWalkTime=(((nDistance / 1000) / 5) * 60).toFixed(0);
        var nWalkTimeMinutes=nWalkTime;
        var nWalkTimeHours=0;
        var nWalkTimeDays=0;
        var sWalkTime=nWalkTime.toString() + " " + sDUMin + " ";
        var addressMarker = new Array();       
        
        if (nDistance > 1000)
        { nDistanceValue = nDistance / 1000; sDistanceUnit=" " + sDUKM + " "; }
        if (nWalkTime > 59)
        {
            nWalkHours = Math.floor((nWalkTime / 60));
            if (nWalkHours < 24)
                sWalkTime=nWalkHours.toString() + sDUH + " " + (nWalkTime-(nWalkHours*60)).toString() + sDUMin + " ";
            else
                sWalkTime=Math.floor((nWalkHours / 24)).toString() + sDUD + " " + (nWalkHours-(Math.floor((nWalkHours/24))*24)).toString() + sDUH + " " + (nWalkTime-(nWalkHours*60)).toString() + sDUMin;
        }
        
        
        
        
        //document.getElementById("status").innerHTML = "Odległosc (km): " + length.toFixed(4) +" Czas" +nWalkTime;
        var info = "Odległość:<b> " + nDistanceValue.toFixed(2).toString() + sDistanceUnit + "</b><br> Czas przejścia: <b>" + sWalkTime+"</b>";
       // document.getElementById("status").innerHTML = "Odległość:<b> " + nDistanceValue.toFixed(2).toString() + sDistanceUnit + "</b><br> Czas przejścia: <b>" + sWalkTime+"</b>";
        
        //GLog.write("Odleglosc " + length.toFixed(4));
        //GLog.writeUrl("http://maps.com.pl");
      }
      map.clearOverlays();

      // Grab last point of polyPoints to add marker
      marker = new GMarker(polyPoints[polyPoints.length -1]);
     // addressMarker.push( marker );
     // var index = addressMarker.length - 1;
      //GEvent.addListener(marker, "mouseout", function() {
       // marker.openInfoWindowHtml(info);
      //});
      
      window.setTimeout(function() {
              marker.openInfoWindowHtml( info );
            }, addressTimeWait);
      //marker.openInfoWindowHtml("Some other stuff" + nDistance);
     // map.addOverlay(marker);
      map.addOverlay( marker );
      map.addOverlay(polyShape);
    
    }

   function zoomToPoly() {
     if (polyShape && polyPoints.length > 0) {
       var bounds = polyShape.getBounds();
       map.setCenter(bounds.getCenter());
       map.setZoom(map.getBoundsZoomLevel(bounds));
     }
   }