var map;
		  var gdir;
	    var geocoder = null;
	    var addressMarker;
	    var line;



      function initialize() {
        if(GBrowserIsCompatible()) {




        	function createMarker(point, id, number, icon) {
  				var marker = new GMarker(point, icon);
  				GEvent.addListener(marker, "click", function() {
    			marker.openInfoWindowHtml("<b>" + number + "</b>");
    			document.getElementById("opt" + id).selected=true;
  				});
  				return marker;
					}



          map = new GMap2(document.getElementById('map'));

          var bounds = new GLatLngBounds();
          //alert(bounds);


          if (get_id)
          {
					
          map.setCenter(new GLatLng(get_id.gx, get_id.gy), get_id.zoom);
          //alert(get_id);
          }
          else
          if (get_zone)
          {

          map.setCenter(new GLatLng(get_zone.gx, get_zone.gy), get_zone.zoom);
          //alert(get_id);
          }
          else
          {
          map.setCenter(new GLatLng(56, 90.3), 3);
        	}
	      map.setCenter(new GLatLng(56, 90.3), 3);

          map.addControl(new GLargeMapControl());
					map.addControl(new GMapTypeControl());
					map.addControl(new GScaleControl());
					map.addControl(new GOverviewMapControl());





          var icon = new GIcon(G_DEFAULT_ICON);
          var gs = GSize("16",  "16")
          icon.iconSize = gs;
          icon.shadowSize = gs;
          icon.shadow = "http://www.oxothik.ru/map/home_new.png";
          icon.image = "http://www.oxothik.ru/map/home_new.png";
          icon.iconAnchor = new GPoint(8, 8);

          var icon1 = new GIcon(G_DEFAULT_ICON);
          var gs1 = GSize("20",  "18")
          icon1.iconSize = gs1;
          icon1.shadowSize = gs1;
          icon1.shadow = "http://www.oxothik.ru/map/icon_fish_new.png";
          icon1.image = "http://www.oxothik.ru/map/icon_fish_new.png";
          icon1.iconAnchor = new GPoint(10, 9);

          var markers = [];
          for (var i = 0; i < data.count; ++i) {
            var latlng = new GLatLng(data.bases[i].latitude, data.bases[i].longitude);

            var nmar = createMarker(latlng, data.bases[i].id, "<div style='margin: 10px; width: 300px; height: 90px;'><img src='" + data.bases[i].pic + "' height='70' style='border: 1px solid #000; margin: 5px;' align='left' /><b><a href='index.php?action=bases&id=" + data.bases[i].id + "'>" + data.bases[i].title1 + "</a></b><br/>" + data.bases[i].region + "<br/>" + data.bases[i].ori + "</div>", (data.bases[i].water) ? icon1 : icon);

            //var marker = new GMarker(latlng, {icon: icon});
            //GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml("<b>" + i + "</b>");  });
            //map.addOverlay(nmar);
            markers.push(nmar);

            if ((get_id) && (data.bases[i].id == get_id.id))
            	{
            	nmar.openInfoWindowHtml("<div style='margin: 10px; width: 300px; height: 90px;'><img src='" + data.bases[i].pic + "' height='70' style='border: 1px solid #000; margin: 5px;' align='left' /><b><a href='index.php?action=bases&id=" + data.bases[i].id + "'>" + data.bases[i].title1 + "</a></b><br/>" + data.bases[i].region + "<br/>" + data.bases[i].ori + "</div>");

            	//alert(get_id.id + " " + data.bases[i].pic + " " + data.bases[i].title1 + " "  + data.bases[i].region + " " + data.bases[i].ori + " " + data.bases[i].id);
            	}


          }
          
          var markerCluster = new MarkerClusterer(map, markers);
          if (get_id)
          {

          map.setCenter(new GLatLng(get_id.gx, get_id.gy), get_id.zoom);
          //alert(get_id);
          }
          else
          if (get_zone)
          {

          map.setCenter(new GLatLng(get_zone.gx, get_zone.gy), get_zone.zoom);
          //alert(get_id);
          }
          
        }
      }

     function setDirections(f, t)
     	{
     	var geocoder = new GClientGeocoder();

     	geocoder.getLatLng( f, function(point) {
     		if (!point) {
				alert(f + " not found");
					} else {
					//map.setCenter(point, 8);
					var ll = t.split(',');
					to_ll = new GLatLng(ll[0], ll[1]);
					map.setCenter(to_ll, 8);
					document.getElementById("directions").innerHTML = "РАССТОЯНИЕ: " + (point.distanceFrom(to_ll)/1000).toFixed(2) + " км.";

					var myArray = new Array(point, to_ll);
					//alert(line);
					if (line)
						{
						map.removeOverlay(line);
						}
					line = new GPolyline(myArray);
       		map.addOverlay(line);

       		var bounds = new GLatLngBounds;

    			bounds.extend(myArray[0]);
  				map.setZoom(map.getBoundsZoomLevel(bounds));

					bounds.extend(myArray[1]);
  				map.setZoom(map.getBoundsZoomLevel(bounds));
					map.setCenter(bounds.getCenter());



				}
				}
				);
			return false;

     	}

