// wersja 2 google map
var GoogleMapBObj; // na razie tylko jedna mapa na stronie

function GoogleMapBInit(lat, lng, zoom, showMarker, forManage, address) {
  GoogleMapBObj = new Object();
  GoogleMapBObj.lat = lat;
  GoogleMapBObj.lng = lng;
  GoogleMapBObj.zoom = zoom;
  GoogleMapBObj.showMarker = showMarker;
  GoogleMapBObj.forManage = forManage;
  GoogleMapBObj.address = address;
}

function GoogleMapBShow() { 
  if (GBrowserIsCompatible()) { 
    if(GoogleMapBObj.map) {
      ShowElement(GoogleMapBObj.gmElem); 
    }else {
      var o = GoogleMapBObj;
      GoogleMapBCreate(o.lat, o.lng, o.zoom, o.showMarker, o.forManage, o.address);
    }
  } else {
    alert('Ta przeglądarka nie pozwala wyświetlić mapy');
  }
}


function GoogleMapBCreate(lat, lng, zoom, showMarker, forManage, address) { 
  var gmElem = document.getElementById('GoogleMapBElement');
  gmElem.style.width = (document.getElementById('VLTopLeftPanel').offsetWidth+10) + 'px'; 
  gmElem.style.height = (document.getElementById('VLTopLeftPanel').offsetHeight) + 'px'; 
  ShowElement(gmElem); 
  var map = new GMap2(document.getElementById('GoogleMapBElement'));
  map.addControl(new GoogleMapBHideControl());
  if(forManage) map.addControl(new GoogleMapBManageControl());
  //map.addControl(new GMenuMapTypeControl(false), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(100, 10)) );
  map.setCenter(new GLatLng(lat, lng), zoom); 
  //map.setUIToDefault();

  var customUI = map.getDefaultUI();
  customUI.controls.maptypecontrol = false;
  customUI.controls.menumaptypecontrol = true;
  map.setUI(customUI);


  GoogleMapBObj.gmElem = gmElem;
  GoogleMapBObj.map = map;
  if(showMarker) {
    var marker = new GMarker(new GLatLng(lat, lng), {draggable: (forManage ? true : false)}); 
    map.addOverlay(marker); 
    GoogleMapBObj.marker = marker;
  }else{
    GoogleMapBObj.geocoder = new GClientGeocoder();
    GoogleMapBShowAddress(address);
  }
}

function GoogleMapBHide()
{
  if(GoogleMapBObj.gmElem) HideElement(GoogleMapBObj.gmElem);
}

function GoogleMapBManageControl() {
}

// To "subclass" the GControl, we set the prototype object to
// an instance of the GControl object
GoogleMapBManageControl.prototype = new GControl();

// Creates a one DIV for each of the buttons and places them in a container
// DIV which is returned as our control element. We add the control to
// to the map container and return the element for the map class to
// position properly.
GoogleMapBManageControl.prototype.initialize = function(map) {
  var container = document.createElement("div");

  var zoomInDiv = document.createElement("div");
  this.setButtonStyle_(zoomInDiv);
  container.appendChild(zoomInDiv);
  zoomInDiv.appendChild(document.createTextNode("Znacznik do środka"));
  GEvent.addDomListener(zoomInDiv, "click", function() {
    var marker = GoogleMapBObj.marker;
    if(marker) {
      marker.setLatLng(map.getCenter());
    } else {
      marker = new GMarker(map.getCenter(), {draggable: (GoogleMapBObj.forManage ? true : false)}); 
      map.addOverlay(marker); 
      GoogleMapBObj.marker = marker
    }
  });

  var zoomOutDiv = document.createElement("div");
  this.setButtonStyle_(zoomOutDiv);
  container.appendChild(zoomOutDiv);
  zoomOutDiv.appendChild(document.createTextNode("Zapisz położenie"));
  GEvent.addDomListener(zoomOutDiv, "click", function() {
      var marker = GoogleMapBObj.marker;
      if(marker) {
        BackASPSubmitForm(vl_form, '//listings/LstMapSetCoordinates.asp',
          'LMSC_ListingID=' + vl_form.elements[C_VL_ListingID].value +
          '&LMSC_LLatitude=' + marker.getLatLng().lat() + '&LMSC_LLongitude=' + marker.getLatLng().lng());
      } else {
        alert('Najpierw trzeba ustawić znacznik');
      }
    }
  );

  map.getContainer().appendChild(container);
  return container;
}

// By default, the control will appear in the top left corner of the
// map with 7 pixels of padding.
GoogleMapBManageControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(77, 7));
}

// Sets the proper CSS for the given button element.
GoogleMapBManageControl.prototype.setButtonStyle_ = function(button) {
  GoogleMapBSetButtonStyle(button)
}

function GoogleMapBSetButtonStyle(button) {
  //button.style.textDecoration = "underline";
  button.style.color = "black";
  button.style.backgroundColor = "white";
  button.style.font = "bold 12px Arial";
  button.style.border = "1px solid black";
  button.style.padding = "1px";
  button.style.marginBottom = "3px";
  button.style.textAlign = "center";
  //button.style.width = "10em";
  button.style.cursor = "pointer";
}

// krzyżyk do ukrywania mapy
function GoogleMapBHideControl() {
}

// To "subclass" the GControl, we set the prototype object to
// an instance of the GControl object
GoogleMapBHideControl.prototype = new GControl();

GoogleMapBHideControl.prototype.initialize = function(map) {
  //var img = document.createElement("img");
  //img.src = ImagesVirtDir + '/delete.gif';
  var img = document.createElement("div");
  GoogleMapBSetButtonStyle(img);
  img.appendChild(document.createTextNode("Zamknij mapę"));

  GEvent.addDomListener(img, "click", function() {
      HideElement(GoogleMapBObj.gmElem);
    }
  );
  map.getContainer().appendChild(img);
  return img;
}

GoogleMapBHideControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(120, 7));
}

function GoogleMapBShowAddress(address) {
  if (GoogleMapBObj.geocoder) {
    GoogleMapBObj.geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
          alert("Nie udało się znaleźć położenia na podstawie adresu");
        } else {
          var map = GoogleMapBObj.map;
          map.setCenter(point, 10);
          marker = new GMarker(map.getCenter(), {draggable: (GoogleMapBObj.forManage ? true : false)}); 
          map.addOverlay(marker); 
          marker.openInfoWindowHtml("<br>Położenie zostało znalezione automatycznie na podstawie adresu.<br>Może nie odpowiadać faktycznemu położeniu nieruchomości."
             //+ "<br>" + address
             + (GoogleMapBObj.forManage ? '<br>Koryguj przesuwając znacznik myszką lub przyciskiem "Znacznik do środka".' : '')
             );
          GoogleMapBObj.marker = marker
        }
      }
    );
  }
}
