
function VZone(type,points)
{
   this.points = new Array;
   if(points) this.points = points;
   this.type = type;
   this.overlay;
   this.map;
   this.id;
   this.color  = 'FF00FF';
   this.weight = 4;
   this.name;
   this.firstPoint;
   this.create = function(LL)
   {
      if(!LL)return;
      this.firstPoint = LL;
     
      var dLat = 0.0015;
      var dLng = 0.003;
      
      this.points = new Array;

      var lat = LL.lat() + dLat/2;
      var lng = LL.lng() - dLng/2;

//      var lat = LL.lat();
//      var lng = LL.lng();

      this.points.push(new GLatLng(lat,lng));

      lng = lng + dLng;
      this.points.push(new GLatLng(lat,lng));

      lat  = lat - dLat;
      this.points.push(new GLatLng(lat,lng));

      lng = lng - dLng;
      this.points.push(new GLatLng(lat,lng));

      lat  = lat + dLat;
      this.points.push(new GLatLng(lat,lng));

   }
   this.addPoint = function(point)
   {
      this.points.push(point);
   }
   this.show = function(map)
   {
      if(!map)return;
      if(this.points.length < 2)return;
      this.map = map; 
      this.overlay = new GPolyline(this.points,this.color,this.weight);
      map.addOverlay(this.overlay);
      this.overlay.setPoint();
   }
   this.hide = function()
   {
      if(this.map && this.overlay)this.map.removeOverlay(this.overlay);
   }
}




function VMap(container)
{
   this.container = container;
   this.original;
}
VMap.prototype.setCenter     = function(){}
VMap.prototype.addControl    = function(){}
VMap.prototype.removeOverlay = function(){}
VMap.prototype.addOverlay    = function(){}
VMap.prototype.getCenter     = function(){}
VMap.prototype.getZoom       = function(){}
VMap.prototype.getBounds     = function(){}
VMap.prototype.checkResize   = function(){}
VMap.prototype.getCurrentMapType   = function(){}
VMap.prototype.createMarker  = function(){}
VMap.prototype.createIcon    = function(){}


VMap.prototype.left       = function(){}
VMap.prototype.right      = function(){}
VMap.prototype.up         = function(){}
VMap.prototype.down       = function(){}
VMap.prototype.setZoom    = function(){}
VMap.prototype.showInfoWindow    = function(){}

VMap.prototype.getPoint = function(a,b,c,d,e,f,g,h,i,g,k)
      {
         if(!this.original) return;
      }
VMap.prototype.setPoint = function(a,b,c,d,e,f,g,h,i,g,k)
      {
         if(!this.original) return;
      }
VMap.prototype.hide = function(a,b,c,d,e,f,g,h,i,g,k)
      {
         if(!this.original) return;
      }
VMap.prototype.show = function(a,b,c,d,e,f,g,h,i,g,k)
      {
         if(!this.original) return;
      }



function VMarker(point,icon)
{
   this.original;
   this.icon;
   this.point;
}

function IBounds()
{
   this.Top    = new ILatLng();
   this.Bottom = new ILatLng();
   this.contains = function(LatLng)
   {
      if(this.Top.Lat > LatLng.Lat && this.Top.Lng < LatLng.Lng && this.Bottom.Lat < LatLng.Lat && this.Bottom.Lng > LatLng.Lng)
      {
         return true;
      }
      else
      {
         return false;
      }
   }
}



function createIMap(dbName)
{
      if(!dbName){dbName = 'Town';}
      map.original =  new MapDatabase(dbName);
//      map.original =  new MapDatabase("Town");
      VMap.prototype.BindImageControl = function(container)
                    {
                       if(!this.original) return;
                       return this.original.BindImageControl(container);
                    }    

     VMap.prototype.image = map.BindImageControl(map.container);
     VMap.prototype._setCenter = function(X,Y,scale)
     {
         if(!this.original) return;

         this.image.Width = map.container.clientWidth;
         this.image.Height = map.container.clientHeight;

         this.image.CenterX = X;
         this.image.CenterY = Y;
         this.image.ZoomScale = scale;

         return this.image.ProjectionChanged(); 
     }

     VMap.prototype.setCenter = function(LatLng,zoom)
     {
         if(!this.original) return;

         this.image.Width = map.container.clientWidth;
         this.image.Height = map.container.clientHeight;

         var center = this.image.LatLng2Point(LatLng);
         var X = center.X;
         var Y = center.Y;

         this.image.CenterX = X;
         this.image.CenterY = Y;
//         this.image.ZoomScale = scale;
         if(zoom){this.setZoom(zoom);}

         return this.image.ProjectionChanged(); 
     }

     VMap.prototype.getCenter = function()
     {
//         var center = new IPoint();
         var center = new IPoint(this.image.CenterX,this.image.CenterY);
         return this.image.Point2LatLng(center);
     }


    VMap.prototype.zoom   = 8;
    VMap.prototype.getZoom   = function()
      {
         return this.zoom;
      }

    VMap.prototype._setZoom   = function(zoom)
      {
         if(zoom == this.zoom){return;} 
         var dZ = (zoom - this.zoom) * 2;
         if(dZ < 0){dZ = 1/Math.abs(dZ);}
         this.image.Zoom(dZ);
         this.zoom = zoom;
      }

    VMap.prototype.setZoom   = function(zoom)
      {
         this._setZoom(zoom);
         mapMove();
      }

    VMap.prototype.up   = function()
      {
         this.image.ScrollDelta(0,-this.image.Height/3) ;
         mapMove();
      }

    VMap.prototype.down   = function()
      {
         this.image.ScrollDelta(0,this.image.Height/3) ;
         mapMove();
      }

    VMap.prototype.left   = function()
      {
         this.image.ScrollDelta(this.image.Width/3,0) ;
         mapMove();
      }

    VMap.prototype.right   = function()
      {
         this.image.ScrollDelta(-this.image.Width/3,0) ;
         mapMove();
      }

    VMap.prototype.createMarker   = function(point,icon)
      {
          var marker = new IMarker(icon);
          marker.LatLng = point;
          marker.div.onclick = function(){IshowInfoWindow(marker,getDescription(marker));}
          return marker;
      }


    VMap.prototype.createIcon   = function(url,cursor)
      {
         var icon = new IIcon(url,24,24);
         if(cursor){icon.style.cursor = cursor;}
         return icon;
      }

    VMap.prototype.createLatLng   = function(lat,lng)
      {
          return new ILatLng(lat,lng)
      }

    VMap.prototype.addOverlay   = function(overlay)
      {
         this.image.addMarker(overlay);
      }

    VMap.prototype.removeOverlay   = function(overlay)
      {
         this.image.removeMarker(overlay);
      }

    VMap.prototype.setPoint   = function(marker,LatLng)
      {
         this.image.setLatLng(marker,LatLng);
      }

    VMap.prototype.getBounds   = function()
      {
         var bounds = new IBounds();
         var point = new IPoint(0,0)  
         this.image.PointDeviceToWorld(point);
         var top = this.image.Point2LatLng(point);
         var point = new IPoint(this.image.Width,this.image.Height)  
         this.image.PointDeviceToWorld(point);
         var bottom = this.image.Point2LatLng(point);
         
         bounds.Top    = top; 
         bounds.Bottom = bottom; 
         return bounds;
      }

      map.image.moveend = mapMove;

      map.image.ZoomScale = 1/800000;
      map.setCenter(new ILatLng(53.5366316666667, 49.350445), 10);
//      map.setCenter(53.5366316666667, 49.350445, 10);
//      map.setCenter(new GLatLng(53.5366316666667, 49.350445), 12, typeMap);
//      map.setCenter(21000,35000,1/200000);

      setTimeout("map.image.ScrollDelta(1,1)",1000);
//      setTimeout("map.image.ProjectionChanged()",1000);

}

function testClick(lat,lng)
{
  alert(lat+'  '+lng);
}


function createAMap(type)
{
   if(! GMap2){return;}

   map.original =  new GMap2(map.container);
   
   VMap.prototype.setCenter = function(LL,zoom)
      {
         if(!this.original) return;
         if(!zoom) zoom = this.getZoom();
         this.original.setCenter(LL,zoom);
         appEvents.dispatchEvent('mapZoom','mapZoom');
      }
   VMap.prototype.getCenter = function(a,b,c,d,e,f,g,h,i,g,k)
      {
         if(!this.original) return;
         return this.original.center; 
      }
   VMap.prototype.getZoom = function(a,b,c,d,e,f,g,h,i,g,k)
      {
         if(!this.original) return;
         return this.original.zoom; 
      }
    VMap.prototype.getBounds = function()
    {
         if(!this.original) return;
         return this.original.getBounds(); 
    }

   VMap.prototype.addOverlay = function(a,b,c,d,e,f,g,h,i,g,k)
      {
         if(!this.original) return;
         this.original.addOverlay(a,b,c,d,e,f,g,h,i,g,k); 
      }
   VMap.prototype.removeOverlay = function(a,b,c,d,e,f,g,h,i,g,k)
      {
         if(!this.original) return;
         this.original.removeOverlay(a,b,c,d,e,f,g,h,i,g,k); 
      }

    VMap.prototype.createLatLng   = function(lat,lng)
      {
         return new GLatLng(lat,lng);
      }

    VMap.prototype.createIcon   = function(url,width,height,cursor)
      {
         if(!width) {width  = 24;}
         if(!height){height = 24;}
         var icon = new GIcon(url,width,height);
         icon.image = url;
         if(cursor){icon.style.cursor = cursor;}
         return icon;
      }

    VMap.prototype.createMarker   = function(point,icon)
      {
          var marker = new GMarker(point,icon);
//          marker.div.onclick = function(){marker.openInfoWindow(getDescription(marker));};
//          marker.addListener('click',function(){marker.openInfoWindow(getDescription(marker));});
          marker.addListener('click',function(){markerClick(marker)});
          return marker;
      }

    VMap.prototype.setPoint   = function(marker,LatLng)
      {
         marker.setPoint(LatLng);
      }
    VMap.prototype.setZoom   = function(zoom)
      {
         this.original.setZoom(zoom);
//         this.original.setCenter(this.original.center,zoom);
         mapMove();
      }

    VMap.prototype.left       = function()
      {
         if(!this.original) return;
         this.original.scrollLeft(); 
         mapMove();
      }
    VMap.prototype.right      = function()
      {
         if(!this.original) return;
         this.original.scrollRight(); 
         mapMove();
      }
    VMap.prototype.up         = function()
      {
         if(!this.original) return;
         this.original.scrollDown();; 
         mapMove();
      }
    VMap.prototype.down       = function()
      {
         if(!this.original) return;
         this.original.scrollUp();; 
         mapMove();
      }

   map.original.ondblclick = AMapDblClick;

//   map.setCenter(new GLatLng(53.5366316666667, 49.350445), 12);
   map.setCenter(new GLatLng(53.3366316666667, 49.750445), 9);
}

function AMapDblClick(lat,lng)
{
   var zoom = map.getZoom();
   if(zoom < 17) zoom++;
   map.setCenter(new GLatLng(lat,lng),zoom);
}


function createGMap(type)
{
   if(! GMap2){return;}
   var typeMap;


//   if (GBrowserIsCompatible()) 
//   {
      map.original =  new GMap2(map.container)

   VMap.prototype.setCenter = function(a,b,c,d,e,f,g,h,i,g,k)
      {
         if(!this.original) return;
         this.original.setCenter(a,b,c,d,e,f,g,h,i,g,k); 
      }

    VMap.prototype.addControl = function(d,e,f)
      {
         if(!this.original) return;
         this.original.addControl(d,e,f); 
      }

    VMap.prototype.removeOverlay = function(a,b,c,d,e,f,g,h,i,g,k)
      {
         if(!this.original) return;
         return this.original.removeOverlay(a,b,c,d,e,f,g,h,i,g,k); 
      }
    VMap.prototype.addOverlay    = function(a,b,c,d,e,f,g,h,i,g,k)
      {
         if(!this.original) return;
         return this.original.addOverlay(a,b,c,d,e,f,g,h,i,g,k); 
      }
    VMap.prototype.getCenter     = function(a,b,c,d,e,f,g,h,i,g,k)
      {
         if(!this.original) return;
         return this.original.getCenter(a,b,c,d,e,f,g,h,i,g,k); 
      }
    VMap.prototype.getZoom       = function(a,b,c,d,e,f,g,h,i,g,k)
      {
         if(!this.original) return;
         return this.original.getZoom(a,b,c,d,e,f,g,h,i,g,k); 
      }
    VMap.prototype.getBounds     = function(a,b,c,d,e,f,g,h,i,g,k)
      {
         if(!this.original) return;
         return this.original.getBounds(a,b,c,d,e,f,g,h,i,g,k); 
      }
    VMap.prototype.checkResize   = function(a,b,c,d,e,f,g,h,i,g,k)
      {
         if(!this.original) return;
         return this.original.checkResize(a,b,c,d,e,f,g,h,i,g,k); 
      }
    VMap.prototype.getCurrentMapType   = function(a,b,c,d,e,f,g,h,i,g,k)
      {
         if(!this.original) return;
         return this.original.getCurrentMapType(a,b,c,d,e,f,g,h,i,g,k); 
      }

    VMap.prototype.createMarker   = function(point,icon)
      {
         var marker = new GMarker(point,{icon: icon, draggable: false});  
         GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(getDescription(marker)); });  
         return marker;
      }
    VMap.prototype.createLatLng   = function(lat,lng)
      {
         return new GLatLng(lat,lng);
      }
    VMap.prototype.createIcon   = function(url)
      {
         var icon;

         icon = new GIcon();
         icon.image = "http://gps.vi-tel.ru"+url;
         icon.shadow = "http://gps.vi-tel.ru"+url;
         icon.iconSize = new GSize(24, 24);
         icon.shadowSize = new GSize(24, 24);
         icon.iconAnchor = new GPoint(12, 12);
         icon.infoWindowAnchor = new GPoint(12, 12);
         return icon;
       }
    VMap.prototype.setZoom   = function(zoom)
      {
         var center = this.original.getCenter();
         this.original.setCenter(center,zoom);
      }

    VMap.prototype.up   = function()
      {
         this.original.panDirection(0,1);
      }

    VMap.prototype.down   = function()
      {
         this.original.panDirection(0,-1);
      }

    VMap.prototype.left   = function()
      {
         this.original.panDirection(1,0);
      }

    VMap.prototype.right   = function()
      {
         this.original.panDirection(-1,0);
      }

    VMap.prototype.setPoint   = function(marker,LatLng)
      {
         marker.setPoint(LatLng);
      }

//      control = new GLargeMapControl();
//      map.addControl(control,new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10,100))   );
   

   if(type)
   {
      switch(type)
      {
      case 'G_NORMAL_MAP':
         typeMap = G_NORMAL_MAP;
         break;
      case 'G_SATELLITE_MAP':
         typeMap = G_SATELLITE_MAP;
         break;
      case 'G_HYBRID_MAP':
         typeMap = G_HYBRID_MAP;
         break;
      default:
         typeMap = G_NORMAL_MAP;
      }
   }
   else
   {
      typeMap = G_NORMAL_MAP;
   }

    map.setCenter(new GLatLng(53.5366316666667, 49.350445), 12, typeMap);
    GEvent.addListener(map.original, "moveend", mapMove);

//   }
}

function mapMove()
{
   if(showMode == 1 || showMode == 2)showMode = 0;
   appEvents.dispatchEvent('mapZoom','mapZoom');
}

