function MapCreate( ctrl, lat, lng, z, makeStatic, minZoom, maxZoom ) {
  var map = new google.maps.Map2( ctrl );
  if ( map != null ) {
    this.map = map;
    
    // Position and zoom
    this.setPosition( lat, lng, z );

    // Standard controls
    if ( makeStatic == true ) {
      map.disableDragging();
      map.disableDoubleClickZoom();
    }
    else {
      map.addControl( new google.maps.LargeMapControl() );
      
      // get array of map types 
      var mapTypes = map.getMapTypes(); 
      // overwrite the getMinimumResolution() and getMaximumResolution() methods for each map type 
      for (var i=0; i<mapTypes.length; i++) { 
        if (minZoom!=0) mapTypes[i].getMinimumResolution = function() {return minZoom;} 
        if (maxZoom!=0) mapTypes[i].getMaximumResolution = function() {return maxZoom;} 
      }
    }

    
    // Events
    google.maps.Event.bind( map, "dragend", this, function() { this.hideMsgCtrl(); this.updateMap(false); } );
    google.maps.Event.bind( map, "zoomend", this, function( oldZ, newZ ) { this.hideMsgCtrl(); this.updateMap((oldZ<newZ)); } );
  }
}

function MapDestroy() {
  google.maps.Unload();
  
  this.map = null;
}

function MapSetPosition( lat, lng, z ) {
  this.z = z;
  this.position = new google.maps.LatLng( lat, lng );
  this.map.setCenter( this.position, z );
  this.map.setZoom( z );
}

function MapUpdate(bZoomIn) {
  if ( typeof DoMapUpdate != "undefined") DoMapUpdate( true, bZoomIn, false );
}

function MapShowMarker( mapMarker ) {
  if (mapMarker != null) {
  	var marker = mapMarker.getMarker();
  	if (marker == null) {
      var mLatLng = mapMarker.getLatLng();
      var mIcon = mapMarker.getIcon();
      var mName = mapMarker.getName();

      if (mIcon != null) {
        marker = new google.maps.Marker( mLatLng, { icon: mIcon, title: mName } );
      }
      else
        marker = new google.maps.Marker( mLatLng, { title: mName } );
      marker.title = mName;

      mapMarker.setMarker( marker );
      google.maps.Event.bind( marker, "click", mapMarker, mapMarker.onclick );
      google.maps.Event.bind( marker, "mouseover", mapMarker, mapMarker.onmouseover);
      google.maps.Event.bind( marker, "mouseout", mapMarker, mapMarker.onmouseout);
      google.maps.Event.bind( marker, "infowindowopen", mapMarker, function () { mapMarker.showingInfo = true; });
      var closeFunc = function () { 
        mapMarker.showingInfo = false; 
        document.markerLoading = null; 
      };
      google.maps.Event.bind( marker, "infowindowclose", mapMarker, closeFunc );

      this.map.addOverlay( marker );
    }
  }
}

function MapHideMarker(mapMarker) {
  if ( mapMarker == null ) return;

  var marker = mapMarker.getMarker();
  if ( marker != null ) {
    this.map.removeOverlay( marker );
    mapMarker.setMarker( null );
    delete marker;
  }
}

function MapShowWRMarker( mapMarker ) {
  if (mapMarker != null) {
  	var marker = mapMarker.getMarker();
  	if (marker == null) {
      var mLatLng = mapMarker.getLatLng();
      var mIcon = mapMarker.getIcon();
      var mName = mapMarker.getName();

//      if (mIcon != null) {
//        marker = new google.maps.Marker( mLatLng, { icon: mIcon, title: mName } );
//      }
//      else
//        marker = new google.maps.Marker( mLatLng, { title: mName } );
      marker = new MarkerLight( mLatLng, mapMarker.html );
      marker.title = mName;

      mapMarker.setMarker( marker );
      google.maps.Event.bind( marker, "click", mapMarker, mapMarker.onclick );
      google.maps.Event.bind( marker, "mouseover", mapMarker, mapMarker.onmouseover);
      google.maps.Event.bind( marker, "mouseout", mapMarker, mapMarker.onmouseout);
      google.maps.Event.bind( marker, "infowindowopen", mapMarker, function () { mapMarker.showingInfo = true; });
      var closeFunc = function () { 
        mapMarker.showingInfo = false; 
        document.markerLoading = null; 
      };
      google.maps.Event.bind( marker, "infowindowclose", mapMarker, closeFunc );

      this.map.addOverlay( marker );
    }
  }
}

function MapHideWRMarker(mapMarker) {
  if ( mapMarker == null ) return;

  var marker = mapMarker.getMarker();
  if ( marker != null ) {
    this.map.removeOverlay( marker );
    mapMarker.setMarker( null );
    delete marker;
  }
}

function MapRPCStart()
{
  if ( !this.activeRPC ) {
    this.activeRPC = true;
    
    this.hideMsgCtrl();

    if ( this.waitVisible == false ) {
      this.map.addControl( this.waitCtrl );
      this.waitVisible = true;
    }
  }
}

function MapRPCEnd() {
  if ( this.activeRPC ) {
    this.activeRPC = false;

    if  ( this.waitVisible == true ) {
      this.map.removeControl( this.waitCtrl );
      this.waitVisible = false;
    }
  }
}


function MapShowMsgCtrl()
{
  if ( this.msgCtrl != null ) {
    this.map.addControl( this.msgCtrl );
  }
}

function MapHideMsgCtrl()
{
  if ( this.msgCtrl != null ) {
    this.map.removeControl( this.msgCtrl );
    this.msgCtrl = null;
  }
}



function Map( fUpdate ) {
  this.map = null;
  this.position = null;
  this.z = null;
  
  this.updateCtrl = null;
  this.updateVisible = false;
  this.waitCtrl = null;
  this.waitVisible = false;
  this.msgCtrl = null;
  
  this.create = MapCreate;
  this.destroy = MapDestroy;
  this.updateMap = MapUpdate;
  this.setPosition = MapSetPosition;
  this.update = fUpdate;
  
  this.showMarker = MapShowMarker;
  this.hideMarker = MapHideMarker;
  this.showWRMarker = MapShowWRMarker;
  this.hideWRMarker = MapHideWRMarker;
  this.RPCStart = MapRPCStart;
  this.RPCEnd = MapRPCEnd;
  
  this.showMsgCtrl = MapShowMsgCtrl;
  this.hideMsgCtrl = MapHideMsgCtrl;
  
  this.activeRPC = false;
}



    function showPopupMap() {
        var mapCtrl = document.getElementById("map");
        var modalCtrl = $find(document.containerId + "_popMap");
        modalCtrl.show();
        //if (document.map == null)
        return false;
    }

    function btnOK() {
        var modalCtrl = $find(document.containerId + "_popMap");
        modalCtrl.hide();
        return false;
    }