//go to point on the map & zoom
    function go(lat, lng, zoom){
      map.panTo(new GLatLng(lat,lng));
      map.setZoom(zoom);
    }

//--------------------------------------------------------------------

//go to marker on the map
  function goMarker(lat, lng, id, name, address, type_id, is_free, is_accessibility){
  map.setZoom(17);
  map.panTo(new GLatLng(lat,lng));

  var marker = map.getMarkerById(id);
  if(!marker){
    var point = new GLatLng(lat,lng);
    marker = createPointMarker(point, id, name, address, type_id, is_free, is_accessibility);
    marker.marker_id = id;
    marker.type_id = type_id;
    map.addOverlay(marker);
    }

  GEvent.trigger(marker,"click");
  }

//--------------------------------------------------------------------

  function showAddress(address) {
    if (geocoder) {
      geocoder.getLatLng(
        address,
        function(point) {
          if (!point) {
            alert(address + " not found");
          } else {
            map.setCenter(point, 13);
            var marker = new GMarker(point);
            map.addOverlay(marker);
            marker.openInfoWindowHtml(address);
          }
        }
      );
    }
  }

//####################################################################
//add & remove points markers on the map

    function createPointMarker(point, id, name, address, type_id, is_free, is_accessibility) {
      var icon
      /*
      if(is_free=="1" || is_accessibility=="1"){
      	var prm = "";
      	if(is_free=="1") prm += "&free";
      	if(is_accessibility=="1") prm += "&access";

      	var icon_file = customIcons[type_id].image;
      	icon_file = icon_file.substr(icon_file.lastIndexOf("/")+1);
      	icon = new GIcon(); 
		icon.image = 'http://www.addyourpoint.com/thumb.php?icon='+icon_file+prm;
		//icon.shadow = 'http://www.addyourpoint.com/map_icons/mm_20_shadow.png';
		//icon.iconSize = new GSize(12, 20);
		//icon.shadowSize = new GSize(22, 20);
		icon.iconAnchor = new GPoint(20, 20);
		icon.infoWindowAnchor = new GPoint(20, 1);
      }else{
      	icon = customIcons[type_id];
      }
      */
      icon = customIcons[type_id];

      var marker = new GMarker(point, icon);
      var selPointsLang = document.getElementById("selPointsLang");
      var pl = ((typeof points_lang != "undefined")?points_lang:selPointsLang.options[selPointsLang.selectedIndex].value);

      var html = ""+
      "<table width=\"200\" style=\"font-size: 14px; color: black\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\">"+
      "<tr><td colspan=\"2\" align=\"center\"><u><b><a href=\"view_point.php?id=" + id + "\" target=\"_blank\">" + name + "</a></b></u></td></tr>"+
      "<tr><td colspan=\"2\""+((pl=='he')?" dir=\"rtl\"":"")+">" + address + "</td></tr>"+
      "<tr><td align=\"left\"><font size=\"2\"><a href=\"view_point.php?id=" + id + "\" target=\"_blank\" style=\"color:blue\""+((view_lang=='he')?" dir=\"rtl\"":"")+">"+messages['MoreDetails']+"</a>&nbsp;&nbsp;</font></td><td align=\"right\">&nbsp;&nbsp;<font size=\"2\"><a href=\"javascript:reportJunk(" + id + ")\">"+messages['JunkReport']+"</a></font></td></tr>"+
      "</table>";

      GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(html);
      });
      return marker;
    }

//--------------------------------------------------------------------

    function createPointGroupMarker(point, type_id, type_name, is_free, is_accessibility, points, zoomLevel) {
      var icon
      /*
      if(is_free=="1" || is_accessibility=="1"){
      	var prm = "";
      	if(is_free=="1") prm += "&free";
      	if(is_accessibility=="1") prm += "&access";

      	var icon_file = customIcons[type_id].image;
      	icon_file = icon_file.substr(icon_file.lastIndexOf("/")+1);
      	icon = new GIcon(); 
		icon.image = 'http://www.addyourpoint.com/thumb.php?icon='+icon_file+prm;
		//icon.shadow = 'http://www.addyourpoint.com/map_icons/mm_20_shadow.png';
		//icon.iconSize = new GSize(12, 20);
		//icon.shadowSize = new GSize(22, 20);
		icon.iconAnchor = new GPoint(20, 20);
		icon.infoWindowAnchor = new GPoint(20, 1);
      }else{
      	icon = customIcons[type_id];
      }
      */
      icon = customIcons[type_id];

      var marker = new GMarker(point, icon);
      if(typeof view_lang == "undefined") view_lang = "en";
      zoomLevel = parseInt(zoomLevel);
      if(zoomLevel<=map.getZoom()) zoomLevel++;
      if(zoomLevel>16) zoomLevel = 16;

      var html = ""+
      "<table width=\"200\" style=\"font-size: 14px; color: black\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\">"+
      "<tr><td colspan=\"2\" align=\"center\"><u><b>" + type_name + "</b></u></td></tr>"+
      "<tr><td"+((view_lang=='he')?" dir=\"rtl\"":"")+">" + points + " "+messages['PointsFound']+"<br/><font size=\"2\">"+messages['ForMoreDetails']+" <a href=\"javascript:map.setCenter(new GLatLng("+point.lat()+","+point.lng()+"), "+zoomLevel+")\" style=\"color:blue\">"+messages['ZoomIn']+"</a></font></td></tr>"+
      "</table>";

      GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(html);
      });
      return marker;
    }

//--------------------------------------------------------------------

    function createCountryGroupMarker(point, country, points) {
      var marker = new GMarker(point, iconBlue);
      if(typeof view_lang == "undefined") view_lang = "en";

      var html = ""+
      "<table width=\"200\" style=\"font-size: 14px; color: black\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\">"+
      "<tr><td align=\"center\"><u><b>" + country + "</b></u></td></tr>"+
      "<tr><td"+((view_lang=='he')?" dir=\"rtl\"":"")+">" + points + " "+messages['PointsFound']+"<br/><font size=\"2\">"+messages['ForMoreDetails']+" <a href=\"javascript:go("+point.lat()+","+point.lng()+",4)\" style=\"color:blue\">"+messages['ZoomIn']+"</a></font></td></tr>"+
      "</table>";

      GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(html);
      });
      return marker;
    }

//--------------------------------------------------------------------

    function getBoundPrm(){
      var bounds = map.getBounds();
      var southWest = bounds.getSouthWest();
      var northEast = bounds.getNorthEast();
      var lngSpan = northEast.lng() - southWest.lng();
      var latSpan = northEast.lat() - southWest.lat();

      var stBoundPrm = "lat="+southWest.lat()+"&latSpan="+latSpan+"&lng="+southWest.lng()+"&lngSpan="+lngSpan;
      return stBoundPrm;
    }

//--------------------------------------------------------------------

    function addMarkers(data){
      if(sw_view!="points") return;

      var xml = GXml.parse(data);
      var markers = xml.documentElement.getElementsByTagName("marker");
      for (var i = 0; i < markers.length; i++) {
        var id = markers[i].getAttribute("id");

        if(map.isMarkerIdExist(id)) continue;

        var name = markers[i].getAttribute("name");
        var address = markers[i].getAttribute("address");
        var type_id = markers[i].getAttribute("type_id");
        var ext_type_id = markers[i].getAttribute("ext_type_id");
        var is_free = markers[i].getAttribute("is_free");
        var is_accessibility = markers[i].getAttribute("is_accessibility");
        var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                                parseFloat(markers[i].getAttribute("lng")));

        var marker = createPointMarker(point, id, name, address, type_id, is_free, is_accessibility);
        marker.marker_id = id;
        marker.type_id = type_id;
        marker.ext_type_id = ext_type_id;
        
        mrk_mgr.addMarker(marker);
      }
    mrk_mgr.refresh();
    //loadingMarkers = false;
    }

//--------------------------------------------------------------------

    function addPointGroupMarkers(data){
      if(sw_view!="p_groups") return;

      var xml = GXml.parse(data);
      var markers = xml.documentElement.getElementsByTagName("marker");
      for (var i = 0; i < markers.length; i++) {
        var type_id = markers[i].getAttribute("type_id");
        var ext_type_id = markers[i].getAttribute("ext_type_id");
        var is_free = markers[i].getAttribute("is_free");
        var is_accessibility = markers[i].getAttribute("is_accessibility");
        var r_lat = markers[i].getAttribute("r_lat");
        var r_lng = markers[i].getAttribute("r_lng");
        var g_id = type_id+"_"+r_lat+"_"+r_lng;

        if(map.isMarkerIdExist(g_id)) continue;

        var type_name = markers[i].getAttribute("type_name");
        var points = markers[i].getAttribute("points");

        var southWest = new GLatLng(parseFloat(markers[i].getAttribute("min_lat")),
                                parseFloat(markers[i].getAttribute("min_lng")));
        var northEast = new GLatLng(parseFloat(markers[i].getAttribute("max_lat")),
                                parseFloat(markers[i].getAttribute("max_lng")));

        var pointsBound = new GLatLngBounds(southWest, northEast);
        var point = pointsBound.getCenter();
        var zoomLevel = map.getBoundsZoomLevel(pointsBound);

        var marker = createPointGroupMarker(point, type_id, type_name, is_free, is_accessibility, points, zoomLevel);
        marker.marker_id = g_id;
        marker.type_id = type_id;
        marker.ext_type_id = ext_type_id;

        mrk_mgr.addMarker(marker);
      }
    mrk_mgr.refresh();
    //loadingMarkers = false;
    }

//--------------------------------------------------------------------

    function addCountryGroupMarkers(data){
      if(sw_view!="c_groups") return;
      
      var xml = GXml.parse(data);
      var markers = xml.documentElement.getElementsByTagName("marker");
      for (var i = 0; i < markers.length; i++) {
        var country = markers[i].getAttribute("country");

        if(map.isMarkerIdExist(country)) continue;

        var points = markers[i].getAttribute("points");
        var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                                parseFloat(markers[i].getAttribute("lng")));

        var marker = createCountryGroupMarker(point, country, points);
        marker.marker_id = country;

        mrk_mgr.addMarker(marker);
      }
    mrk_mgr.refresh();
    //loadingMarkers = false;
    }

//--------------------------------------------------------------------

    function reportJunk(id){
        if(!confirm("this operation will report this point as junk\ndo you want to proceed?")) return;
        if(typeof view_lang == "undefined") view_lang = "en";
        var url = "mail_point_key.php?jr=1&id="+id+"&view_lang="+view_lang;
        GDownloadUrl(url, function(){alert(messages['JunkReportSent'])});
    }

//--------------------------------------------------------------------

    function showLatLng(point){
    	var txtLatLng = document.getElementById('txtLatLng');
    	var lblLatLng = document.getElementById('lblLatLng');
    	if(txtLatLng){
			var v = 'Lng:'+point.lng()+'\tLat:'+point.lat();
    		txtLatLng.value = v;
    	}
    	if(lblLatLng){
    		lblLatLng.innerHTML = messages['CursorLatLng'];
    	}
    }
    
//--------------------------------------------------------------------

    function showCenter(){
    	var point = map.getCenter();
    	var txtLatLng = document.getElementById('txtLatLng');
    	var lblLatLng = document.getElementById('lblLatLng');
    	if(txtLatLng){
			var v = 'Lng:'+point.lng()+'\tLat:'+point.lat();
    		txtLatLng.value = v;
    	}
    	if(lblLatLng){
    		lblLatLng.innerHTML = messages['MapCenterLatLng'];
    	}
    }

//--------------------------------------------------------------------

    function showZoom(){
		var lblZoom = document.getElementById('lblZoom');
		if(lblZoom){
			lblZoom.innerHTML = map.getZoom();
		}
    }
	
//--------------------------------------------------------------------

var MaxZoom = 20;
function checkMaxZoom() {
  var latlng = map.getCenter();
  var mta = map.getMapTypes();
  var mt;
  for(var i in mta){
  	mt = mta[i];
  	if(mt.getName()=="Satellite") break;
  }

  //map.getCurrentMapType().getMaxZoomAtLatLng(latlng, function(response) {

  mt.getMaxZoomAtLatLng(latlng, function(response) {
    if (response && response['status'] == G_GEO_SUCCESS) {
      var zoom = map.getZoom();
      if(zoom>parseInt(response['zoom'])){
        //map.setCenter(latlng, response['zoom']);
        map.setZoom(parseInt(response['zoom']));
        MaxZoom = parseInt(response['zoom']);
        if(typeof loadMarkers == "function") loadMarkers();
      }
    }else{
      //if(map.getZoom()>16) map.setZoom(16);
    }
  });
}

//--------------------------------------------------------------------

function setMaxZoomCenter(map, latlng) {
  map.getCurrentMapType().getMaxZoomAtLatLng(latlng, function(response) {
    if (response && response['status'] == G_GEO_SUCCESS) {
      map.setCenter(latlng, response['zoom']);
    }
  });
}

//--------------------------------------------------------------------

function createCF() {
  var d = document.createElement("div");
  d.id = "cf";
  document.body.appendChild(d);
  GDownloadUrl("get_cf.php", setCF);
}
