//<![CDATA[
if(navigator.platform.match(/linux|bsd/i)) {
   _mSvgEnabled = _mSvgForced = true;
}

var map;
var areaCntColor = "#0000ff";
var polyLineColor = "#ff0000";
var polyFillColor = "#bb6666";
var polyPoints = new Array();
var polyShape = new Array();
var areatext = new Array();
var center;

function centerToPoly(polyId){
   map.setCenter(polyShape[polyId].getBounds().getCenter(), 8);
}

function changePolyOpt(polyId){
   zoom=map.getZoom();
   var opt={weight:3,opacity: 1};
   polyShape[polyId].setFillStyle(opt);
}

function changePolygonsOpt(oldzoom,zoom){
   for (i=0; i<polyShape.length; i++){
         if (zoom<4){
            var opt={color:polyLineColor, opacity: 1};
         }else{
            var opa=0.1+oPolygons[i].cnt/20;
            if (opa >0.8) opa=0.8;
            var opt={color:polyFillColor, opacity: opa};
         }
         polyShape[i].setFillStyle(opt);

         var cnt=oPolygons[i].cnt;
         var textoffset=0;
         if (cnt>=100)textoffset=14;
         else if (cnt>=10)textoffset=10;
         else textoffset=7;
         var center=polyShape[i].getBounds().getCenter();
         var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint(0,0),true),map.getZoom());
         var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(center, map.getZoom());
         areatext[i].style.top=(offset.y-point.y-7)+'px';
         areatext[i].style.left=(offset.x-point.x-textoffset)+'px';

         if (zoom>=8){
            areatext[i].style.visibility='visible';
            areatext[i].style.fontSize='22px';
         }else if (zoom>=6){
            areatext[i].style.visibility='visible';
            areatext[i].style.fontSize='12px';
         } else areatext[i].style.visibility='hidden';
   }
}

function load() {
   if (GBrowserIsCompatible()) {
      map = new GMap2(document.getElementById("map"), {draggableCursor:'auto', draggingCursor:'move'});
      map.addControl(new GSmallZoomControl(), new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(8, 40)));
      map.addControl(new GScaleControl());
      var mapControl = new GMapTypeControl();
      map.removeMapType(G_SATELLITE_MAP);
      map.removeMapType(G_NORMAL_MAP);
      map.addMapType(G_PHYSICAL_MAP);
      map.addMapType(G_NORMAL_MAP);
      map.addMapType(G_SATELLITE_MAP);
      map.addControl(mapControl);
      map.setMapType(G_PHYSICAL_MAP);

      map.removeMapType(G_HYBRID_MAP);
      map.enableContinuousZoom();
      map.disableDoubleClickZoom();
      map.enableScrollWheelZoom();
      map.setCenter(new GLatLng(30, 0), 2);

      GEvent.addListener(map,"zoomend", function(a,b) {
         changePolygonsOpt(a,b);
      })

      for (i=0; i<oPolygons.length; i++){
         polyPoints.length = 0;
         polyPoints=[new GLatLng(oPolygons[i].latmin, oPolygons[i].lonmin),
                     new GLatLng(oPolygons[i].latmin, oPolygons[i].lonmax),
                     new GLatLng(oPolygons[i].latmax, oPolygons[i].lonmax),
                     new GLatLng(oPolygons[i].latmax, oPolygons[i].lonmin),
                     new GLatLng(oPolygons[i].latmin, oPolygons[i].lonmin)];

         polyShape[i] = new GPolygon(polyPoints, polyLineColor, 1, .8, polyLineColor,1);
         map.addOverlay(polyShape[i]);

         areatext[i] = document.createElement("div");
         areatext[i].style.visibility='hidden';
         areatext[i].style.fontFamily='Geneva,Arial,Helvetica,sans-serif';
         areatext[i].style.fontWeight='bold';
         areatext[i].style.color=areaCntColor;
         areatext[i].style.position='absolute';
         areatext[i].innerHTML = oPolygons[i].cnt;
         map.getPane(G_MAP_MARKER_MOUSE_TARGET_PANE).appendChild(areatext[i]);
         var center=polyShape[i].getBounds().getCenter();
         var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint(0,0),true),map.getZoom());
         var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(center, map.getZoom());
         areatext[i].style.top=(offset.y-point.y-10)+'px';
         areatext[i].style.left=(offset.x-point.x-10)+'px';
      }
   }
}
//]]>

