﻿var map = null;
var geocoder = null;
var bounds = null;
var w = window;

function Tooltip(marker, text, padding) {
        this.marker_ = marker;
        this.text_ = text;
        this.padding_ = padding;
    }

    Tooltip.prototype = new GOverlay();

    Tooltip.prototype.initialize = function(map) {
        var div = document.createElement("div");
        div.innerHTML = this.text_;
        div.className = 'tooltip';

        div.style.position = 'absolute';
        div.style.visibility = 'hidden';
        map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
        this.map_ = map;
        this.div_ = div;
    }

    Tooltip.prototype.remove = function() {
        this.div_.parentNode.removeChild(this.div_);
    }

    Tooltip.prototype.copy = function() {
        return new Tooltip(this.marker_, this.text_, this.padding_);
    }

    Tooltip.prototype.redraw = function(force) {
        if (!force) return;
        var markerPos = this.map_.fromLatLngToDivPixel(this.marker_.getPoint());
        var iconAnchor = this.marker_.getIcon().iconAnchor;
        var xPos = Math.round(markerPos.x - this.div_.clientWidth / 2);
        var yPos = markerPos.y - iconAnchor.y - this.div_.clientHeight - this.padding_;
        this.div_.style.top = yPos + 'px';
        this.div_.style.left = xPos + 'px';
    }

    Tooltip.prototype.show = function() {
        this.div_.style.visibility = 'visible';
    }

    Tooltip.prototype.hide = function() {
        this.div_.style.visibility = 'hidden';
    }


function load() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
            map.setCenter(new GLatLng(0, 0), 0);
            map.addControl(new GLargeMapControl());
            map.addControl(new GMapTypeControl());
            map.enableScrollWheelZoom();

            geocoder = new GClientGeocoder();
            bounds = new GLatLngBounds();
    }
}

//Voeg nog een extra parameter (CallNo) toe. Soms lijkt Google een time out te geven, in het geval dat dit optreed,
    //even een teller opwaarderen, en het 10 keer proberen.
    function showAddressBullitInfo(street, cityCountry, Sector, BedrijfId, info, CallNo) {

        var Adres = street + ", " + cityCountry;
        geocoder.getLatLng(
				  Adres,
				  function(point) {

				      if (!point || !street) {

				          if (CallNo < 10) {

				              CallNo += 1
				              w.setTimeout("showAddressBullitInfo('" + street + "', '" + cityCountry + "', '" + Sector + "', '" + BedrijfId + "', '" + info + "', " + CallNo + ")", 100);

				          } else {
				              return
				          }
				      } else {

				          var icon1 = new GIcon()

				          if (Sector == 'BAT') {
        icon1.image = "http://test.savantis.nl/Images/bol_S&O.png";
    } else if (Sector == 'RPC') {
    icon1.image = "http://test.savantis.nl/Images/bol_RPC.png";
    } else if (Sector == 'AFB') {
    icon1.image = "http://test.savantis.nl/Images/bol_A&S.png";
    } else if (Sector == 'SMK') {
    icon1.image = "http://test.savantis.nl/Images/bol_SMK.png";
    }

				          icon1.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
				          icon1.iconSize = new GSize(20, 20);
				          icon1.shadowSize = new GSize(22, 20);
				          icon1.iconAnchor = new GPoint(6, 20);
				          icon1.infoWindowAnchor = new GPoint(5, 1);

				          var marker = new GMarker(point, icon1);
				          var tooltip = new Tooltip(marker, info, 4);

				          marker.tooltip = tooltip;

				          map.addOverlay(marker);
				          map.addOverlay(tooltip);

				          bounds.extend(marker.getLatLng());

				          GEvent.addListener(marker, "mouseover", function() {
				              this.tooltip.show();
				          });

				          GEvent.addListener(marker, "mouseout", function() {
				              this.tooltip.hide();
				          });

				          GEvent.addListener(marker, "click", function() {
				              __doPostBack('ROW_ISG_ID', BedrijfId);
				          });

				          //Set nog even de juiste zoom factor en center van de map
				          map.setZoom(map.getBoundsZoomLevel(bounds) - 1);
				          map.setCenter(bounds.getCenter());
				      }
				  }
				);
		}

function ShowZip() {
    geocoder.getLatLng(
      document.getElementById("txtPostCode").value + ", Nederland",
      function(point) {
          map.setCenter(point);
      }
    );
}