function MAP( x_, y_, z_ ){ 
  if( GBrowserIsCompatible() ){    
    if( typeof( x_ ) == 'undefined'  ) x_ = '';
    if( typeof( y_ ) == 'undefined'  ) y_ = '';
    if( typeof( z_ ) == 'undefined'  ) z_ = '';
    var mapdiv = document.getElementById( 'map_canvas' );
    var map = this._ = new GMap2( mapdiv );
    // +++ hidden nodes    
    var x = this.x = document.createElement( 'input' );
    x.type = 'hidden';
    x.name = 'x';
    x.value = x_; 
    document.getElementById( 'show_map' ).parentNode.appendChild( x );
    var y = this.y = document.createElement( 'input' );
    y.type = 'hidden';
    y.name = 'y';
    y.value = y_;
    document.getElementById( 'show_map' ).parentNode.appendChild( y );
    var map_zoom = document.createElement( 'input' );
    map_zoom.type = 'hidden';
    map_zoom.name = 'map_zoom';
    map_zoom.value = z_;
    document.getElementById( 'show_map' ).parentNode.appendChild( map_zoom );
    // --- hidden nodes
    var center = new GLatLng( y_ || MAP_Y, x_ || MAP_X );
    map.setCenter( center, z_ || 7 );
    map.addControl( new GMapTypeControl() );
    map.addControl( new GLargeMapControl() );
    map.addControl( new GOverviewMapControl() );
    var icon = new GIcon(G_DEFAULT_ICON);
    icon.image = "/img/home_marker.png";
    icon.iconSize = new GSize(38, 35);
    icon.shadow = false;
    icon.iconAnchor = new GPoint(14, 32);
    var marker = this.marker = new GMarker( center, {draggable: true, icon:icon} );
    function setInputsVal(){      
      x.value = marker.getPoint().x;
      y.value = marker.getPoint().y;
    } // function setInputsVal(){
    document.getElementById( 'show_map' ).checked = ( x_ && y_ );
    document.getElementById( 'show_map' ).disabled = ! ( x_ && y_ );        
    GEvent.addListener(marker, "dragend", function(){
      setInputsVal();
      map_zoom.value = map.getZoom();
    } );         
    GEvent.addListener( map, "zoomend", function() {
      map_zoom.value = map.getZoom();
    } );
    if( ! document.getElementById( 'show_map' ).onclick ) document.getElementById( 'show_map' ).onclick = function(){
      if( this.checked ){
        marker.show();
        setInputsVal();
      } else {
        marker.hide();
        x.value = '';
        y.value = '';
      }
    };
    var timeout = false;
    GEvent.addListener( map, "click", function() {
      if( timeout ) clearTimeout( timeout );
      var arguments_ = arguments;
      timeout = setTimeout( function(){     
        document.getElementById( 'show_map' ).disabled = false;    
        map_zoom.value = map.getZoom();
        marker.show();
        document.getElementById( 'show_map' ).checked = true;        
        if( arguments_[1] ){        
          marker.setPoint( arguments_[1] );        
          //map.setCenter( arguments[1] );
          setInputsVal();
        } // if( arguments[1] ){
      }, 350 ); // timeout = setTimeout( function(){
    } ); // GEvent.addListener( map, "click", function() {
    GEvent.addListener( map, "dblclick", function() {
      if( timeout ) clearTimeout( timeout );
    } );
    map.enableScrollWheelZoom();
    map.addOverlay( marker );
    if( x_ == '' && y_ == '' ) marker.hide();        
  } // if (GBrowserIsCompatible()) {
};

MAP.prototype = {
  position:function( x, y, z ){
    this.marker.show();
    this.marker.setPoint( new GLatLng( y, x ) );
    this.x.value = x;
    this.y.value = y;
    this._.setCenter( new GLatLng( y, x ) );
    if( typeof( z ) != 'undefined' && z ) this._.setZoom( z ); 
  }
};

function map_show( x_, y_, z_, node_id ){   
  if (GBrowserIsCompatible()) {
    if( typeof( x_ ) == 'undefined'  ) x_ = '';
    if( typeof( y_ ) == 'undefined'  ) y_ = '';
    if( typeof( z_ ) == 'undefined'  ) z_ = '';
    this._ = new GMap2( document.getElementById( node_id ) );
    if( node_id == 'map_canvas' ){
      this._.addControl( new GSmallMapControl() );
    } else {
      this._.addControl( new GMapTypeControl() );
      this._.addControl( new GLargeMapControl() );
      this._.enableScrollWheelZoom();
    }
    var center = new GLatLng( y_ || MAP_Y, x_ || MAP_X );
    this._.setCenter( center, z_ || 14 );
    var icon = new GIcon(G_DEFAULT_ICON);
    icon.image = "/img/home_marker.png";
    icon.iconSize = new GSize(38, 35);
    icon.shadow = false;
    icon.iconAnchor = new GPoint(14, 32);
    this.marker = new GMarker( center, {draggable: false, icon:icon} );
    this._.addOverlay( this.marker );
  } // if (GBrowserIsCompatible()) {
};

function map_show_info( x_, y_, z_ ){
  InfoLite.open({text:'<div id="map_canvas_" style="height:520px;width:100%;"></div>', width:550, height:550});
  new map_show( x_, y_, z_, 'map_canvas_' );
  return false;
}

function large_map( par ){  
  var this_ = this;
  if( ! par.data ) par.data = [];  
  function sort_by_size( a, b ){
    if( a.s < b.s ) return 1;
    if( a.s > b.s ) return -1;
    return 0;
  }
  par.data.sort( sort_by_size );
  var map = this._ = new GMap2( par.node );
  this._.addControl( new GMapTypeControl() );
  this._.addControl( new GLargeMapControl() );
  this._.addControl( new GOverviewMapControl() );
  this._.enableScrollWheelZoom();
  GMap2.prototype.setZoomAndCenter = function( x1, y1, x2, y2 ){
    //map.addOverlay( new GMarker( new GLatLng( y1,  x1 ) ) );
    //map.addOverlay( new GMarker( new GLatLng( y2,  x2 ) ) );
    var mySpace = new GLatLngBounds( new GLatLng( y1, x1 ), new GLatLng( y2, x2 ) );    
    var newZoom = this.getBoundsZoomLevel(mySpace);
    var newCenter = mySpace.getCenter();
    this.setCenter(newCenter,newZoom ); 
  };
  function re_mapdata_mmxy( re ){
    if( typeof re == 'string' ) eval( "var re = " + re );
    if( re.max && re.mix && re.may && re.miy ){
      map.setZoomAndCenter( re.mix, re.miy, re.max, re.may );
    }
    if( re.x && re.y && re.z ){
      map.setCenter( new GLatLng( re.y, re.x ), re.z );      
    }
  }  
  /*  
  if( $('#group2_id').val() ){
    $.get( '/rq/mapdata.php?search=' + search, '', re_mapdata_mmxy );    
  } else if( $('#group_id').val() ){
    $.get( '/rq/mapdata.php?search=' + search, '', re_mapdata_mmxy );
  }
  */
  if( par.mmxy && ( par.mmxy.x || par.mmxy.mix ) ){
    re_mapdata_mmxy( par.mmxy );
  } else {
    var center = new GLatLng( par.y || MAP_Y, par.x || MAP_X );
    this._.setCenter( center, par.z || 8 );
  }
  this.markers = {};  
  function attalums_ok( markers, r ){        
    var min_dxdy_zoom = {
       0:7,
       1:4,
       2:1.706,
       3:1.136,
       4:0.295,
       5:0.249,
       6:0.105,
       7:0.081,
       8:0.036,
       9:0.025,
      10:0.015,
      11:0.0048,
      12:0.0023,
      13:0.00123,
      14:0.00046,
      15:0.00029,
      16:0.00017,
      17:0.0001,
      18:0.00001,
      19:0.000001        
    };
    var k, dx, dy;
    if( ! min_dxdy_zoom[ map.getZoom() ] ) return false;
    for( k in markers ){
      if( ! markers[ k ].dxdy[ r.i ] ){      
        dx = r.x - markers[ k ].r.x;
        dy = r.y - markers[ k ].r.y;
        markers[ k ].dxdy[ r.i ] = Math.sqrt( dx * dx + dy * dy );
      }
      if( markers[ k ].dxdy[ r.i ] < min_dxdy_zoom[ map.getZoom() ] ) return false;            
    }
    return true;
  };
  
  // +++++++++++++++++++++ my_overlay ++++++++++++++++++++  
  this.my_overlay = function( point ){
    this.point_ = point;    
  };
  this.my_overlay.prototype= new GOverlay();
  this.my_overlay.prototype.initialize = function( map ){
    map.mkE = true; 
    this.node = DOM.make( {
      tag:'div',
      className:'map_zip_ad_overlay',
      prop:{
        map:map,
        prop:{
          onclick:function(){
            adPopup( '/show/index.php?m=' + this.marker.r.i );
          }
        }
      }
    } ).Append( map.getPane(G_MAP_MARKER_PANE) );
    this.Show();    
    //map.getPane(G_MAP_MAP_PANE).appendChild(this.node);
  };
  this.my_overlay.prototype.remove = function(){
    this.node.Remove();
  };  
  this.my_overlay.prototype.redraw = function( force ){    
    this.moveTo( this.point_ );
  };
  this.my_overlay.prototype.moveTo = function( point ){
    this.point_ = point;
    var c1 = this.node.map.fromLatLngToDivPixel( point );    
    this.node.style.left = c1.x - 125 + "px";
    this.node.style.top = c1.y - 5 + "px";    
  };
  this.my_overlay.prototype.Hide = function(){
    this.node.style.display = 'none';
  };
  this.my_overlay.prototype.Show = function(){
    this.node.style.display = '';    
  };
  // -------------------- my_overlay ----------------------
  var my_overlay = false;   
  function marker_mouseover(){
    if( ! this.ad_data ) return false;
    if( my_overlay.timeout ) clearTimeout( my_overlay.timeout );  
    if( ! my_overlay ){
      my_overlay = new this_.my_overlay( this.getPoint() );      
      map.addOverlay( my_overlay );     
    } else {      
      my_overlay.Show();        
      my_overlay.moveTo( this.getPoint() );
    }
    my_overlay.node.marker = this;        
    my_overlay.node.className = 'map_zip_ad_overlay' + ( this.r.s == 2 ? ' map_zip_ad_overlay_izc' : '' );
    my_overlay.node.Clear();
    var loading, img;
    DOM.make( {
      tag:'table',
      els:[
        DOM.make( {
          tag:'tbody',
          els:[
            DOM.make( {
              tag:'tr',
              els:[
                DOM.make( {
                  tag:'td',
                  prop:{
                    rowSpan:'4'
                  },
                  els:( this.ad_data.pic ? [
                    img = DOM.make( {
                      tag:'img',
                      prop:{                        
                        onload:function(){                          
                          this.style.display = '';
                          loading.style.display = 'none';
                        },                        
                        style:{
                          display:'none'
                        }
                      }
                    } ),
                    loading = DOM.make( {
                      tag:'img',
                      prop:{
                        src:'/img/load2.gif'
                      }
                    } ) 
                  ] : [] )
                } ),
                DOM.make( {
                  tag:'td',
                  className:'t1',
                  prop:{                    
                    innerHTML:this.ad_data.t1
                  }
                } )                
              ]
            } ),
            DOM.make( {
              tag:'tr',
              className:'lt',
              els:[
                DOM.make( {
                  tag:'td',
                  prop:{
                    innerHTML:( typeof this.ad_data.lt == 'undefined' ? '' : this.ad_data.lt )
                  }
                } )
              ]
            } ),
            DOM.make( {
              tag:'tr',
              els:[
                DOM.make( {
                  tag:'td',
                  prop:{
                    innerHTML:( typeof this.ad_data.rt == 'undefined' ? '' : this.ad_data.rt )
                  }
                } )
              ]
            } ),
            DOM.make( {
              tag:'tr',
              els:[
                DOM.make( {
                  tag:'td',
                  className:'t2',
                  prop:{                    
                    innerHTML:this.ad_data.t2
                  }
                } )
              ]
            } )
          ]
        } )
      ]
    } ).Append( my_overlay.node );   
   if( this.ad_data.pic ) img.src = this.ad_data.pic;
  };
  function marker_mouseout(){
    my_overlay.timeout = setTimeout( function(){
      if( my_overlay ) my_overlay.Hide();
    }, 700 );
  }; 
  function marker_click(){
    adPopup( '/show/index.php?m=' + this.r.i );
  };    
  var icon = new GIcon(G_DEFAULT_ICON);
  icon.image = "/img/home_marker.png";
  icon.iconSize = new GSize(38, 35);
  icon.shadow = false;
  icon.iconAnchor = new GPoint(14, 32);   
  var iconf = new GIcon(G_DEFAULT_ICON);
  iconf.image = "/img/home_marker_fav.png";
  iconf.iconSize = new GSize(38, 35);
  iconf.shadow = false;
  iconf.iconAnchor = new GPoint(14, 32);
  var icon_izc = new GIcon(G_DEFAULT_ICON);
  icon_izc.image = "/img/home_marker_izc.png";
  icon_izc.iconSize = new GSize(38, 35);
  icon_izc.shadow = false;
  icon_izc.iconAnchor = new GPoint(14, 32);
  var iconf_izc = new GIcon(G_DEFAULT_ICON);
  iconf_izc.image = "/img/home_marker_izc_fav.png";
  iconf_izc.iconSize = new GSize(38, 35);
  iconf_izc.shadow = false;
  iconf_izc.iconAnchor = new GPoint(14, 32);
  this.on_zoom_and_move = function(){
    var bounds = map.getBounds();
    var southWest = bounds.getSouthWest();
    var northEast = bounds.getNorthEast();    
    var r, k;    
    for( k in this_.markers ){
      this_.markers[ k ].ad_id_ = false;      
    }
    var j = 0, markers = [];
    for( var i = 0; i < par.data.length; i ++ ){
      r = par.data[ i ];      
      if( r.x >= southWest.lng() && r.x <= northEast.lng() && r.y >= southWest.lat() && r.y <= northEast.lat() ){
        if( attalums_ok( markers, r ) ){
          if( ! this_.markers[ r.i ] ){
            var fav = ( typeof r.f == 'undefined' ? false : true )
            this_.markers[ r.i ] = new GMarker( new GLatLng( r.y, r.x ), {draggable: false, icon:( r.s == 2 ? ( fav ? iconf_izc : icon_izc ) : ( fav ? iconf : icon )  ) } );
            this_.markers[ r.i ].ad_id = r.i;
            this_.markers[ r.i ].dxdy = {};
            this_.markers[ r.i ].r = r;          
            map.addOverlay( this_.markers[ r.i ] );
            GEvent.addListener( this_.markers[ r.i ], "mouseover", marker_mouseover );
            GEvent.addListener( this_.markers[ r.i ], "mouseout", marker_mouseout );
            GEvent.addListener( this_.markers[ r.i ], "click", marker_click );
          } else {
            if( this_.markers[ r.i ].isHidden ) this_.markers[ r.i ].show();
          }          
          this_.markers[ r.i ].ad_id_ = true;
          markers.push( this_.markers[ r.i ] );
          if( j ++ == 100 ) break; 
        }        
      }      
    }
    for( var k in this_.markers ){
      if( ! this_.markers[ k ].ad_id_ ){
        this_.markers[ k ].hide();
        //delete this_.markers[ k ];
      }      
    }
    var ad_get_data = [];
    for( var k in markers ){
      if( ! markers[ k ].ad_data ) ad_get_data.push( markers[ k ].ad_id );
    }
    if( ad_get_data.length ){
      $.get( '/rq/mapdata.php?i=' + ad_get_data.join( ',' ), '', function( text ){
        eval( 'var re = {' + text + '}' );        
        for( var key in re.data ){          
          this_.markers[ re.data[ key ].i ].ad_data = re.data[ key ];
        }       
      } );
    }    
  };
  this.on_zoom_and_move();
  GEvent.addListener( map, "moveend", this.on_zoom_and_move );         
  GEvent.addListener( map, "zoomend", this.on_zoom_and_move );
  
};


//////

/* Constructor for an extended Marker class */
function LabeledMarker(latlng, options){
    this.latlng = latlng;
    this.labelText = options.labelText || "";
    this.labelClass = options.labelClass || "markerLabel";
    this.labelOffset = options.labelOffset || new GSize(0, 0);
    GMarker.apply(this, arguments);
}


/* It's a limitation of JavaScript inheritance that we can't conveniently
   extend GMarker without having to run its constructor. In order for the
   constructor to run, it requires some dummy GLatLng. 
LabeledMarker.prototype = new GMarker(new GLatLng(0, 0));


// Creates the text div that goes over the marker.
LabeledMarker.prototype.initialize = function(map) {
        // Do the GMarker constructor first.
        GMarker.prototype.initialize.call(this, map);

        var div = document.createElement("div");
        div.className = this.labelClass;
        div.innerHTML = this.labelText;
        div.style.position = "absolute";
        map.getPane(G_MAP_MARKER_PANE).appendChild(div);
        
        this.map = map;
        this.div = div;
}

// Redraw the rectangle based on the current projection and zoom level
LabeledMarker.prototype.redraw = function(force) {
        GMarker.prototype.redraw.call(this, map);
        
        // We only need to do anything if the coordinate system has changed
        if (!force) return;
        
        // Calculate the DIV coordinates of two opposite corners of our bounds to
        // get the size and position of our rectangle
        var p = this.map.fromLatLngToDivPixel(this.latlng);
        var z = GOverlay.getZIndex(this.latlng.lat());
        
        // Now position our DIV based on the DIV coordinates of our bounds
        this.div.style.left = (p.x + this.labelOffset.width) + "px";
        this.div.style.top = (p.y + this.labelOffset.height) + "px";
        this.div.style.zIndex = z + 1; // in front of the marker
}

// Remove the main DIV from the map pane
LabeledMarker.prototype.remove = function() {
  this.div.parentNode.removeChild(this.div);
  this.div = null;
  GMarker.prototype.remove.call(this);
}*/
